Add startup script

This commit is contained in:
Esa Kataja
2025-02-28 12:27:39 +02:00
parent 132460a2f8
commit d744eb647e
2 changed files with 34 additions and 0 deletions
View File
+34
View File
@@ -0,0 +1,34 @@
from pathlib import Path
import duckdb
import bcrypt
from settings import settings
def generate_db() -> None:
settings.database_file.parent.mkdir(parents=True, exist_ok=True)
db_conn = duckdb.connect(str(settings.database_file))
# Read the SQL file
sql_file_path = Path(__file__).parent / "assets" / "database.sql"
with open(sql_file_path, "r") as sql_file:
sql_script = sql_file.read()
# Execute the SQL script
db_conn.execute(sql_script)
# Add admin user
hashed_password = bcrypt.hashpw(settings.admin_password, bcrypt.gensalt())
db_conn.execute(
"INSERT INTO users (username, password, is_admin) VALUES (?, ?, 1)",
[settings.admin_username, hashed_password],
)
db_conn.close()
def init_app() -> None:
if not settings.database_file.exists():
generate_db()
if not settings.logs_dir.exists():
settings.logs_dir.mkdir(parents=True, exist_ok=True)