forked from ma3a/SDH-PlayTime
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Small refactoring on BE side * Fix warnings for FE compilation * Added verify target in make
- Loading branch information
Showing
33 changed files
with
497 additions
and
758 deletions.
There are no files selected for viewing
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
from dataclasses import dataclass | ||
from typing import List | ||
from python.db.sqlite_db import SqlLiteDb | ||
|
||
|
||
@dataclass | ||
class Migration: | ||
version: int | ||
statements: List[str] | ||
|
||
|
||
_migrations = [ | ||
Migration(1, [ | ||
""" | ||
CREATE TABLE play_time( | ||
date_time TEXT, | ||
duration INT, | ||
game_id TEXT | ||
) | ||
""", | ||
""" | ||
CREATE TABLE overall_time( | ||
game_id TEXT PRIMARY KEY, | ||
duration INT | ||
) | ||
""", | ||
""" | ||
CREATE TABLE game_dict( | ||
game_id TEXT PRIMARY KEY, | ||
name TEXT | ||
) | ||
""" | ||
]), | ||
Migration(2, [ | ||
""" | ||
CREATE INDEX play_time_date_time_epoch_idx | ||
ON play_time(UNIXEPOCH(date_time)) | ||
""", | ||
""" | ||
CREATE INDEX play_time_game_id_idx | ||
ON play_time(game_id) | ||
""", | ||
""" | ||
CREATE INDEX overall_time_game_id_idx | ||
ON overall_time(game_id) | ||
""" | ||
]), | ||
Migration(3, [ | ||
"ALTER TABLE play_time ADD COLUMN migrated TEXT" | ||
]) | ||
] | ||
|
||
|
||
class DbMigration: | ||
def __init__(self, db: SqlLiteDb): | ||
self.db = db | ||
|
||
def _current_migration_version(self): | ||
with self.db.transactional() as con: | ||
con.execute( | ||
"CREATE TABLE IF NOT EXISTS migration (id INT PRIMARY KEY);" | ||
) | ||
return con.execute( | ||
"SELECT coalesce(max(id), 0) as max_id FROM migration" | ||
).fetchone()[0] | ||
|
||
def _migration(self, migration: Migration): | ||
version = self._current_migration_version() | ||
if migration.version > version: | ||
with self.db.transactional() as con: | ||
for stm in migration.statements: | ||
con.execute(stm) | ||
con.execute( | ||
"INSERT INTO migration (id) VALUES (?)", | ||
[migration.version] | ||
) | ||
|
||
def migrate(self): | ||
for migration in _migrations: | ||
self._migration(migration) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import contextlib | ||
import sqlite3 | ||
from typing import ContextManager | ||
|
||
|
||
class SqlLiteDb: | ||
|
||
def __init__(self, database_path: str): | ||
self._database_path = database_path | ||
|
||
@contextlib.contextmanager | ||
def transactional(self) -> ContextManager[sqlite3.Connection]: | ||
with sqlite3.connect(self._database_path) as connection: | ||
try: | ||
yield connection | ||
connection.commit() | ||
except Exception as exception: | ||
connection.rollback() | ||
raise exception |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.