diff --git a/kdb-bot/src/bot/bot.json b/kdb-bot/src/bot/bot.json index 887c87bd..3b9deda7 100644 --- a/kdb-bot/src/bot/bot.json +++ b/kdb-bot/src/bot/bot.json @@ -18,7 +18,7 @@ "Dependencies": [ "cpl-core==2022.12.1.post3", "cpl-translation==2022.12.1", - "cpl-query==2022.12.2.post1", + "cpl-query==2022.12.2.post2", "Flask==2.2.2", "Flask-Classful==0.14.2", "Flask-Cors==3.0.10", diff --git a/kdb-bot/src/bot_data/abc/migration_abc.py b/kdb-bot/src/bot_data/abc/migration_abc.py index f6af458c..e44e0bc2 100644 --- a/kdb-bot/src/bot_data/abc/migration_abc.py +++ b/kdb-bot/src/bot_data/abc/migration_abc.py @@ -3,6 +3,7 @@ from abc import ABC, abstractmethod class MigrationABC(ABC): name = None + prio = 0 @abstractmethod def __init__(self): diff --git a/kdb-bot/src/bot_data/migration/api_key_migration.py b/kdb-bot/src/bot_data/migration/api_key_migration.py index 76004f13..0712974b 100644 --- a/kdb-bot/src/bot_data/migration/api_key_migration.py +++ b/kdb-bot/src/bot_data/migration/api_key_migration.py @@ -4,7 +4,7 @@ from bot_data.db_context import DBContext class ApiKeyMigration(MigrationABC): - name = "1.0_ApiKeyMigration" + name = "1.0.0_ApiKeyMigration" def __init__(self, logger: DatabaseLogger, db: DBContext): MigrationABC.__init__(self) diff --git a/kdb-bot/src/bot_data/migration/db_history_migration.py b/kdb-bot/src/bot_data/migration/db_history_migration.py index 2706dcec..e7322ad9 100644 --- a/kdb-bot/src/bot_data/migration/db_history_migration.py +++ b/kdb-bot/src/bot_data/migration/db_history_migration.py @@ -7,6 +7,7 @@ from bot_data.db_context import DBContext class DBHistoryMigration(MigrationABC): name = "1.0.0_DBHistoryMigration" + prio = 1 def __init__(self, logger: DatabaseLogger, db: DBContext): MigrationABC.__init__(self) @@ -14,66 +15,35 @@ class DBHistoryMigration(MigrationABC): self._db = db self._cursor = db.cursor + def _exec(self, file: str): + path = f"{os.path.dirname(os.path.realpath(__file__))}/db_history_scripts" + sql = open(f"{path}/{file}").read() + + for statement in sql.split("\n\n"): + self._cursor.execute(statement + ";") + def upgrade(self): self._logger.debug(__name__, "Running upgrade") - self._cursor.execute( - open(f"{os.path.dirname(os.path.realpath(__file__))}/db_history_scripts/api_keys.sql").read() - ) - self._cursor.execute( - open(f"{os.path.dirname(os.path.realpath(__file__))}/db_history_scripts/auth_users.sql").read() - ) - self._cursor.execute( - open( - f"{os.path.dirname(os.path.realpath(__file__))}/db_history_scripts/auth_user_users_relation.sql" - ).read() - ) - self._cursor.execute( - open(f"{os.path.dirname(os.path.realpath(__file__))}/db_history_scripts/auto_role_rules.sql").read() - ) - self._cursor.execute( - open(f"{os.path.dirname(os.path.realpath(__file__))}/db_history_scripts/auto_roles.sql").read() - ) - self._cursor.execute( - open(f"{os.path.dirname(os.path.realpath(__file__))}/db_history_scripts/clients.sql").read() - ) - self._cursor.execute( - open(f"{os.path.dirname(os.path.realpath(__file__))}/db_history_scripts/game_servers.sql").read() - ) - self._cursor.execute( - open(f"{os.path.dirname(os.path.realpath(__file__))}/db_history_scripts/known_users.sql").read() - ) - self._cursor.execute( - open(f"{os.path.dirname(os.path.realpath(__file__))}/db_history_scripts/levels.sql").read() - ) - self._cursor.execute( - open(f"{os.path.dirname(os.path.realpath(__file__))}/db_history_scripts/servers.sql").read() - ) - self._cursor.execute( - open(f"{os.path.dirname(os.path.realpath(__file__))}/db_history_scripts/user_game_idents.sql").read() - ) - self._cursor.execute( - open( - f"{os.path.dirname(os.path.realpath(__file__))}/db_history_scripts/user_joined_game_servers.sql" - ).read() - ) - self._cursor.execute( - open(f"{os.path.dirname(os.path.realpath(__file__))}/db_history_scripts/user_joined_servers.sql").read() - ) - self._cursor.execute( - open( - f"{os.path.dirname(os.path.realpath(__file__))}/db_history_scripts/user_joined_voice_channel.sql" - ).read() - ) - self._cursor.execute( - open( - f"{os.path.dirname(os.path.realpath(__file__))}/db_history_scripts/user_message_count_per_hour.sql" - ).read() - ) - self._cursor.execute(open(f"{os.path.dirname(os.path.realpath(__file__))}/db_history_scripts/users.sql").read()) - self._cursor.execute( - open(f"{os.path.dirname(os.path.realpath(__file__))}/db_history_scripts/user_warnings.sql").read() - ) + self._exec("api_keys.sql") + self._exec("auth_users.sql") + self._exec("auth_user_users_relation.sql") + self._exec("auto_role_rules.sql") + self._exec("auto_roles.sql") + self._exec("clients.sql") + self._exec("game_servers.sql") + self._exec("known_users.sql") + self._exec("levels.sql") + self._exec("servers.sql") + self._exec("user_game_idents.sql") + self._exec("user_joined_game_servers.sql") + self._exec("user_joined_servers.sql") + self._exec("user_joined_voice_channel.sql") + self._exec("user_message_count_per_hour.sql") + self._exec("users.sql") + self._exec("user_warnings.sql") + + self._logger.debug(__name__, "Finished history upgrade") def downgrade(self): self._cursor.execute("DROP TABLE `ApiKeysHistory`;") diff --git a/kdb-bot/src/bot_data/migration/remove_stats_migration.py b/kdb-bot/src/bot_data/migration/remove_stats_migration.py index bbc029b5..e5e621a5 100644 --- a/kdb-bot/src/bot_data/migration/remove_stats_migration.py +++ b/kdb-bot/src/bot_data/migration/remove_stats_migration.py @@ -4,7 +4,7 @@ from bot_data.db_context import DBContext class RemoveStatsMigration(MigrationABC): - name = "1.0_RemoveStatsMigration" + name = "1.0.0_RemoveStatsMigration" def __init__(self, logger: DatabaseLogger, db: DBContext): MigrationABC.__init__(self) diff --git a/kdb-bot/src/bot_data/migration/user_joined_game_server_migration.py b/kdb-bot/src/bot_data/migration/user_joined_game_server_migration.py index 22302a9f..bef8e5aa 100644 --- a/kdb-bot/src/bot_data/migration/user_joined_game_server_migration.py +++ b/kdb-bot/src/bot_data/migration/user_joined_game_server_migration.py @@ -4,7 +4,7 @@ from bot_data.db_context import DBContext class UserJoinedGameServerMigration(MigrationABC): - name = "1.0_UserJoinedGameServerMigration" + name = "1.0.0_UserJoinedGameServerMigration" def __init__(self, logger: DatabaseLogger, db: DBContext): MigrationABC.__init__(self) diff --git a/kdb-bot/src/bot_data/migration/user_warning_migration.py b/kdb-bot/src/bot_data/migration/user_warning_migration.py index 074cf929..06e446a0 100644 --- a/kdb-bot/src/bot_data/migration/user_warning_migration.py +++ b/kdb-bot/src/bot_data/migration/user_warning_migration.py @@ -4,7 +4,7 @@ from bot_data.db_context import DBContext class UserWarningMigration(MigrationABC): - name = "1.0_UserWarningMigration" + name = "1.0.0_UserWarningMigration" def __init__(self, logger: DatabaseLogger, db: DBContext): MigrationABC.__init__(self) diff --git a/kdb-bot/src/bot_data/model/client.py b/kdb-bot/src/bot_data/model/client.py index ba6f264d..b6c163c9 100644 --- a/kdb-bot/src/bot_data/model/client.py +++ b/kdb-bot/src/bot_data/model/client.py @@ -147,7 +147,7 @@ class Client(TableABC): `DeletedMessageCount`, `ReceivedCommandsCount`, `MovedUsersCount`, - `ServerId`, + `ServerId` ) VALUES ( {self._discord_client_id}, {self._sent_message_count}, diff --git a/kdb-bot/src/bot_data/service/migration_service.py b/kdb-bot/src/bot_data/service/migration_service.py index 7b924f90..dc6436a7 100644 --- a/kdb-bot/src/bot_data/service/migration_service.py +++ b/kdb-bot/src/bot_data/service/migration_service.py @@ -1,5 +1,3 @@ -from typing import Type - from cpl_core.database.context import DatabaseContextABC from cpl_core.dependency_injection import ServiceProviderABC from cpl_query.extension import List @@ -22,7 +20,9 @@ class MigrationService: self._db = db self._cursor = db.cursor - self._migrations = List(type, MigrationABC.__subclasses__()).order_by(lambda x: x.name) + self._migrations: List[MigrationABC] = ( + List(type, MigrationABC.__subclasses__()).order_by(lambda x: x.name).order_by(lambda x: x.prio) + ) def migrate(self): self._logger.info(__name__, f"Running Migrations")