sprava uzivatelu
This commit is contained in:
@@ -1,8 +1,29 @@
|
||||
from app.db.database import get_connection
|
||||
|
||||
|
||||
def _table_columns(con, table_name: str) -> set[str]:
|
||||
rows = con.execute(f"PRAGMA table_info({table_name})").fetchall()
|
||||
return {row["name"] for row in rows}
|
||||
|
||||
|
||||
def ensure_user_columns(con) -> None:
|
||||
columns = _table_columns(con, "users")
|
||||
if not columns:
|
||||
return
|
||||
|
||||
if "is_enabled" not in columns:
|
||||
default_expression = "COALESCE(is_active, 1)" if "is_active" in columns else "1"
|
||||
con.execute("ALTER TABLE users ADD COLUMN is_enabled INTEGER NOT NULL DEFAULT 1")
|
||||
con.execute(f"UPDATE users SET is_enabled = {default_expression} WHERE is_enabled IS NULL OR is_enabled = 1")
|
||||
|
||||
if "updated_at" not in columns:
|
||||
con.execute("ALTER TABLE users ADD COLUMN updated_at TEXT")
|
||||
con.execute("UPDATE users SET updated_at = COALESCE(created_at, CURRENT_TIMESTAMP) WHERE updated_at IS NULL")
|
||||
|
||||
|
||||
def run_migrations():
|
||||
con = get_connection()
|
||||
ensure_user_columns(con)
|
||||
|
||||
con.execute(
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user