2021-08-30 19:55:02 +02:00
|
|
|
import asyncio
|
2020-09-15 15:54:05 -03:00
|
|
|
import os
|
2022-07-16 14:23:03 +02:00
|
|
|
import warnings
|
2020-09-05 08:00:44 +02:00
|
|
|
|
2022-07-16 14:23:03 +02:00
|
|
|
import click
|
2022-07-07 14:30:16 +02:00
|
|
|
from loguru import logger
|
|
|
|
|
2022-12-02 14:52:31 +00:00
|
|
|
from lnbits.settings import settings
|
2022-10-03 16:46:46 +02:00
|
|
|
|
2022-07-16 14:23:03 +02:00
|
|
|
from .core import db as core_db
|
|
|
|
from .core import migrations as core_migrations
|
2023-01-11 19:06:58 +02:00
|
|
|
from .core.crud import get_dbversions, get_inactive_extensions
|
2022-12-22 16:30:37 +02:00
|
|
|
from .core.helpers import migrate_extension_database, run_migration
|
2022-07-16 14:23:03 +02:00
|
|
|
from .db import COCKROACH, POSTGRES, SQLITE
|
2023-01-20 10:06:32 +02:00
|
|
|
from .extension_manager import get_valid_extensions
|
2023-01-11 11:16:21 +02:00
|
|
|
from .helpers import get_css_vendored, get_js_vendored, url_for_vendored
|
2022-10-03 16:46:46 +02:00
|
|
|
|
2020-09-05 08:00:44 +02:00
|
|
|
|
|
|
|
@click.command("migrate")
|
2020-09-13 21:31:05 -03:00
|
|
|
def db_migrate():
|
2021-08-30 19:55:02 +02:00
|
|
|
asyncio.create_task(migrate_databases())
|
2020-09-05 08:00:44 +02:00
|
|
|
|
|
|
|
|
2020-09-15 15:54:05 -03:00
|
|
|
@click.command("assets")
|
|
|
|
def handle_assets():
|
|
|
|
transpile_scss()
|
|
|
|
bundle_vendored()
|
|
|
|
|
|
|
|
|
|
|
|
def transpile_scss():
|
2020-10-06 12:59:06 -03:00
|
|
|
with warnings.catch_warnings():
|
|
|
|
warnings.simplefilter("ignore")
|
|
|
|
from scss.compiler import compile_string # type: ignore
|
|
|
|
|
2022-12-05 12:28:26 +01:00
|
|
|
with open(os.path.join(settings.lnbits_path, "static/scss/base.scss")) as scss:
|
|
|
|
with open(
|
|
|
|
os.path.join(settings.lnbits_path, "static/css/base.css"), "w"
|
|
|
|
) as css:
|
2020-10-06 12:59:06 -03:00
|
|
|
css.write(compile_string(scss.read()))
|
2020-09-15 15:54:05 -03:00
|
|
|
|
|
|
|
|
|
|
|
def bundle_vendored():
|
|
|
|
for getfiles, outputpath in [
|
2022-12-05 12:28:26 +01:00
|
|
|
(get_js_vendored, os.path.join(settings.lnbits_path, "static/bundle.js")),
|
|
|
|
(get_css_vendored, os.path.join(settings.lnbits_path, "static/bundle.css")),
|
2020-09-15 15:54:05 -03:00
|
|
|
]:
|
|
|
|
output = ""
|
|
|
|
for path in getfiles():
|
|
|
|
with open(path) as f:
|
|
|
|
output += "/* " + url_for_vendored(path) + " */\n" + f.read() + ";\n"
|
|
|
|
with open(outputpath, "w") as f:
|
|
|
|
f.write(output)
|
|
|
|
|
2022-09-22 10:46:11 +02:00
|
|
|
|
2020-11-21 18:04:39 -03:00
|
|
|
async def migrate_databases():
|
2020-09-05 08:00:44 +02:00
|
|
|
"""Creates the necessary databases if they don't exist already; or migrates them."""
|
|
|
|
|
2021-03-26 19:10:30 -03:00
|
|
|
async with core_db.connect() as conn:
|
2021-06-21 23:22:52 -03:00
|
|
|
if conn.type == SQLITE:
|
|
|
|
exists = await conn.fetchone(
|
|
|
|
"SELECT * FROM sqlite_master WHERE type='table' AND name='dbversions'"
|
|
|
|
)
|
2021-07-02 18:32:58 -03:00
|
|
|
elif conn.type in {POSTGRES, COCKROACH}:
|
2021-06-21 23:22:52 -03:00
|
|
|
exists = await conn.fetchone(
|
2023-01-20 15:53:27 +02:00
|
|
|
"SELECT * FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'dbversions'"
|
2021-06-21 23:22:52 -03:00
|
|
|
)
|
|
|
|
|
|
|
|
if not exists:
|
2021-03-26 19:10:30 -03:00
|
|
|
await core_migrations.m000_create_migrations_table(conn)
|
|
|
|
|
2022-12-22 16:30:37 +02:00
|
|
|
current_versions = await get_dbversions(conn)
|
|
|
|
core_version = current_versions.get("core", 0)
|
|
|
|
await run_migration(conn, core_migrations, core_version)
|
2021-03-26 19:10:30 -03:00
|
|
|
|
2021-06-21 23:22:52 -03:00
|
|
|
for ext in get_valid_extensions():
|
2022-12-22 16:30:37 +02:00
|
|
|
current_version = current_versions.get(ext.code, 0)
|
|
|
|
await migrate_extension_database(ext, current_version)
|
2022-11-30 16:24:13 +01:00
|
|
|
|
2022-12-22 16:30:37 +02:00
|
|
|
logger.info("✔️ All migrations done.")
|
2021-06-21 23:22:52 -03:00
|
|
|
|
2021-11-09 19:15:43 +01:00
|
|
|
|
2023-01-18 18:35:02 +02:00
|
|
|
async def db_versions():
|
|
|
|
async with core_db.connect() as conn:
|
|
|
|
current_versions = await get_dbversions(conn)
|
|
|
|
return current_versions
|
|
|
|
|
|
|
|
|
2022-12-22 16:30:37 +02:00
|
|
|
async def load_disabled_extension_list() -> None:
|
|
|
|
"""Update list of extensions that have been explicitly disabled"""
|
2023-01-11 19:06:58 +02:00
|
|
|
inactive_extensions = await get_inactive_extensions()
|
2023-01-20 09:39:27 +02:00
|
|
|
settings.lnbits_deactivated_extensions += inactive_extensions
|