lnbits-legend/lnbits/commands.py

92 lines
2.9 KiB
Python
Raw Normal View History

import asyncio
import importlib
2020-09-15 15:54:05 -03:00
import os
2022-07-16 14:23:03 +02:00
import re
import warnings
2022-07-16 14:23:03 +02:00
import click
from loguru import logger
2022-12-02 14:52:31 +00:00
from lnbits.settings import settings
2022-07-16 14:23:03 +02:00
from .core import db as core_db
from .core import migrations as core_migrations
from .core.crud import USER_ID_ALL, get_dbversions, get_inactive_extensions
from .core.helpers import migrate_extension_database, run_migration
2022-07-16 14:23:03 +02:00
from .db import COCKROACH, POSTGRES, SQLITE
from .helpers import (
get_css_vendored,
get_js_vendored,
2022-07-16 14:23:03 +02:00
get_valid_extensions,
url_for_vendored,
)
@click.command("migrate")
def db_migrate():
asyncio.create_task(migrate_databases())
2020-09-15 15:54:05 -03:00
@click.command("assets")
def handle_assets():
transpile_scss()
bundle_vendored()
def transpile_scss():
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:
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
async def migrate_databases():
"""Creates the necessary databases if they don't exist already; or migrates them."""
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(
"SELECT * FROM information_schema.tables WHERE table_schema= 'public' AND table_name = 'dbversions'"
2021-06-21 23:22:52 -03:00
)
if not exists:
await core_migrations.m000_create_migrations_table(conn)
current_versions = await get_dbversions(conn)
core_version = current_versions.get("core", 0)
await run_migration(conn, core_migrations, core_version)
2021-06-21 23:22:52 -03:00
for ext in get_valid_extensions():
current_version = current_versions.get(ext.code, 0)
await migrate_extension_database(ext, current_version)
logger.info("✔️ All migrations done.")
2021-06-21 23:22:52 -03:00
async def load_disabled_extension_list() -> None:
"""Update list of extensions that have been explicitly disabled"""
inactive_extensions = await get_inactive_extensions(user_id=USER_ID_ALL)
settings.lnbits_disabled_extensions += inactive_extensions