feat: add tests, CI and P0 reliability features
This commit is contained in:
1 parent
0754065dae
commit
626650e44a
22 files changed
+1576
-49
No files matched your search
+38
-1
@@ -6,6 +6,7 @@ from datetime import datetime, timezone
|
||||
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
DEFAULT_DB = os.path.join(BASE_DIR, "steam.db")
|
||||
SCHEMA_PATH = os.path.join(BASE_DIR, "tools", "schema.sql")
|
||||
SCHEMA_VERSION = 1
|
||||
|
||||
MOOD_PRESETS = {
|
||||
"adventure-rpg-party": {
|
||||
@@ -233,10 +234,46 @@ def _ensure_column(con, table, column, declaration):
|
||||
con.execute(f"ALTER TABLE {table} ADD COLUMN {column} {declaration}")
|
||||
|
||||
|
||||
def _get_schema_version(con):
|
||||
try:
|
||||
row = con.execute("SELECT value FROM schema_meta WHERE key = 'schema_version'").fetchone()
|
||||
except sqlite3.OperationalError:
|
||||
return 0
|
||||
if not row or row[0] is None:
|
||||
return 0
|
||||
try:
|
||||
return int(row[0])
|
||||
except (TypeError, ValueError):
|
||||
return 0
|
||||
|
||||
|
||||
def _migration_1(con):
|
||||
"""Add game_details.source_appid, introduced before schema versioning."""
|
||||
_ensure_column(con, "game_details", "source_appid", "INTEGER")
|
||||
|
||||
|
||||
MIGRATIONS = {
|
||||
1: _migration_1,
|
||||
}
|
||||
|
||||
|
||||
def init(con):
|
||||
"""Create or upgrade the database schema.
|
||||
|
||||
Migrations run in order from the stored ``schema_meta.schema_version``.
|
||||
A database created by a newer release is rejected instead of being
|
||||
silently modified by an older one.
|
||||
"""
|
||||
version = _get_schema_version(con)
|
||||
if version > SCHEMA_VERSION:
|
||||
raise RuntimeError(
|
||||
f"database schema version {version} is newer than supported {SCHEMA_VERSION}")
|
||||
with open(SCHEMA_PATH, encoding="utf-8") as f:
|
||||
con.executescript(f.read())
|
||||
_ensure_column(con, "game_details", "source_appid", "INTEGER")
|
||||
for target in range(version + 1, SCHEMA_VERSION + 1):
|
||||
MIGRATIONS[target](con)
|
||||
con.execute("INSERT OR REPLACE INTO schema_meta (key, value) VALUES ('schema_version', ?)",
|
||||
(str(SCHEMA_VERSION),))
|
||||
con.commit()
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user