Archived
Add startup script
This commit is contained in:
@@ -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)
|
||||
Reference in New Issue
Block a user