Edit of links and database versioning
This commit is contained in:
+19
-3
@@ -13,7 +13,8 @@ AVATARS_DIR.mkdir(parents=True, exist_ok=True)
|
||||
def hash_password(password: str) -> str:
|
||||
return sha256(password.encode('utf-8')).hexdigest()
|
||||
|
||||
SCHEMA = '''
|
||||
MIGRATIONS = [
|
||||
(1, '''
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id TEXT PRIMARY KEY,
|
||||
username TEXT NOT NULL UNIQUE,
|
||||
@@ -70,7 +71,8 @@ CREATE TABLE IF NOT EXISTS user_plugin_config (
|
||||
UNIQUE(user_id, plugin_name),
|
||||
FOREIGN KEY(user_id) REFERENCES users(id)
|
||||
);
|
||||
'''
|
||||
'''),
|
||||
]
|
||||
|
||||
|
||||
def get_connection() -> sqlite3.Connection:
|
||||
@@ -80,9 +82,23 @@ def get_connection() -> sqlite3.Connection:
|
||||
return conn
|
||||
|
||||
|
||||
def get_schema_version(conn: sqlite3.Connection) -> int:
|
||||
return conn.execute('PRAGMA user_version').fetchone()[0]
|
||||
|
||||
|
||||
def apply_migrations(conn: sqlite3.Connection) -> None:
|
||||
current_version = get_schema_version(conn)
|
||||
for version, sql in MIGRATIONS:
|
||||
if version <= current_version:
|
||||
continue
|
||||
conn.executescript(sql)
|
||||
conn.execute(f'PRAGMA user_version = {version}')
|
||||
conn.commit()
|
||||
|
||||
|
||||
def init_db() -> None:
|
||||
with get_connection() as conn:
|
||||
conn.executescript(SCHEMA)
|
||||
apply_migrations(conn)
|
||||
alice_hash = hash_password('secret123')
|
||||
bob_hash = hash_password('secret123')
|
||||
conn.execute(
|
||||
|
||||
Reference in New Issue
Block a user