From d1c79c95b2994e39faae3ba3b932e45ba925ee65 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Thu, 28 Sep 2023 08:56:59 +0200 Subject: [PATCH] Added short role name migration #378 --- .../src/bot/startup_migration_extension.py | 2 + kdb-bot/src/bot_data/abc/migration_abc.py | 20 +++++++++- .../bot_data/migration/config_migration.py | 10 ----- .../db_history_scripts/short_rule_names.sql | 33 ++++++++++++++++ .../migration/short_role_name_migration.py | 38 +++++++++++++++++++ 5 files changed, 91 insertions(+), 12 deletions(-) create mode 100644 kdb-bot/src/bot_data/migration/db_history_scripts/short_rule_names.sql create mode 100644 kdb-bot/src/bot_data/migration/short_role_name_migration.py diff --git a/kdb-bot/src/bot/startup_migration_extension.py b/kdb-bot/src/bot/startup_migration_extension.py index b8f9eece..3434344f 100644 --- a/kdb-bot/src/bot/startup_migration_extension.py +++ b/kdb-bot/src/bot/startup_migration_extension.py @@ -16,6 +16,7 @@ from bot_data.migration.default_role_migration import DefaultRoleMigration from bot_data.migration.initial_migration import InitialMigration from bot_data.migration.level_migration import LevelMigration from bot_data.migration.remove_stats_migration import RemoveStatsMigration +from bot_data.migration.short_role_name_migration import ShortRoleNameMigration from bot_data.migration.stats_migration import StatsMigration from bot_data.migration.user_joined_game_server_migration import UserJoinedGameServerMigration from bot_data.migration.user_message_count_per_hour_migration import ( @@ -50,3 +51,4 @@ class StartupMigrationExtension(StartupExtensionABC): services.add_transient(MigrationABC, ConfigMigration) # 19.07.2023 #127 - 1.1.0 services.add_transient(MigrationABC, ConfigFeatureFlagsMigration) # 15.08.2023 #334 - 1.1.0 services.add_transient(MigrationABC, DefaultRoleMigration) # 24.09.2023 #360 - 1.1.3 + services.add_transient(MigrationABC, ShortRoleNameMigration) # 28.09.2023 #378 - 1.1.7 diff --git a/kdb-bot/src/bot_data/abc/migration_abc.py b/kdb-bot/src/bot_data/abc/migration_abc.py index e44e0bc2..d09c52ac 100644 --- a/kdb-bot/src/bot_data/abc/migration_abc.py +++ b/kdb-bot/src/bot_data/abc/migration_abc.py @@ -1,13 +1,20 @@ +import os from abc import ABC, abstractmethod +from cpl_core.dependency_injection import ServiceProviderABC +from mysql.connector.cursor import MySQLCursorBuffered + +from bot_data.db_context import DBContext + class MigrationABC(ABC): name = None prio = 0 @abstractmethod - def __init__(self): - pass + @ServiceProviderABC.inject + def __init__(self, db: DBContext): + self._cursor: MySQLCursorBuffered = db.cursor @abstractmethod def upgrade(self): @@ -16,3 +23,12 @@ class MigrationABC(ABC): @abstractmethod def downgrade(self): pass + + + + 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 + ";") diff --git a/kdb-bot/src/bot_data/migration/config_migration.py b/kdb-bot/src/bot_data/migration/config_migration.py index 556b396f..7ff33855 100644 --- a/kdb-bot/src/bot_data/migration/config_migration.py +++ b/kdb-bot/src/bot_data/migration/config_migration.py @@ -1,5 +1,3 @@ -import os - from bot_core.logging.database_logger import DatabaseLogger from bot_data.abc.migration_abc import MigrationABC from bot_data.db_context import DBContext @@ -12,14 +10,6 @@ class ConfigMigration(MigrationABC): MigrationABC.__init__(self) self._logger = logger 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") diff --git a/kdb-bot/src/bot_data/migration/db_history_scripts/short_rule_names.sql b/kdb-bot/src/bot_data/migration/db_history_scripts/short_rule_names.sql new file mode 100644 index 00000000..5f5b7f72 --- /dev/null +++ b/kdb-bot/src/bot_data/migration/db_history_scripts/short_rule_names.sql @@ -0,0 +1,33 @@ +CREATE TABLE IF NOT EXISTS `ShortRoleNamesHistory` +( + `Id` BIGINT(20) NOT NULL, + `Name` VARCHAR(64) DEFAULT NULL, + `DiscordRoleId` BIGINT(20) NOT NULL, + `Deleted` BOOL DEFAULT FALSE, + `DateFrom` DATETIME(6) NOT NULL, + `DateTo` DATETIME(6) NOT NULL +); + +DROP TRIGGER IF EXISTS `TR_ShortRoleNamesUpdate`; + +CREATE TRIGGER `TR_ShortRoleNamesUpdate` + AFTER UPDATE + ON `ShortRoleNames` + FOR EACH ROW +BEGIN + INSERT INTO `ShortRoleNamesHistory` (`Id`, `Name`, `DiscordRoleId`, `DateFrom`, `DateTo`) + VALUES (OLD.Id, OLD.Name, OLD.DiscordRoleId, OLD.LastModifiedAt, CURRENT_TIMESTAMP(6)); +END; + +DROP TRIGGER IF EXISTS `TR_ShortRoleNamesDelete`; + +CREATE TRIGGER `TR_ShortRoleNamesDelete` + AFTER DELETE + ON `ShortRoleNames` + FOR EACH ROW +BEGIN + INSERT INTO `ShortRoleNamesHistory` (`Id`, `Name`, `DiscordRoleId`, `Deleted`, `DateFrom`, + `DateTo`) + VALUES (OLD.Id, OLD.Name, OLD.DiscordRoleId, TRUE, OLD.LastModifiedAt, + CURRENT_TIMESTAMP(6)); +END; \ No newline at end of file diff --git a/kdb-bot/src/bot_data/migration/short_role_name_migration.py b/kdb-bot/src/bot_data/migration/short_role_name_migration.py new file mode 100644 index 00000000..4a427b25 --- /dev/null +++ b/kdb-bot/src/bot_data/migration/short_role_name_migration.py @@ -0,0 +1,38 @@ +from bot_core.logging.database_logger import DatabaseLogger +from bot_data.abc.migration_abc import MigrationABC +from bot_data.db_context import DBContext + + +class ShortRoleNameMigration(MigrationABC): + name = "1.1.7_ShortRoleNameMigration" + + def __init__(self, logger: DatabaseLogger, db: DBContext): + MigrationABC.__init__(self) + self._logger = logger + self._db = db + self._cursor = db.cursor + + def upgrade(self): + self._logger.debug(__name__, "Running upgrade") + + self._cursor.execute( + str( + f""" + CREATE TABLE IF NOT EXISTS `ShortRoleNames` ( + `Id` BIGINT NOT NULL AUTO_INCREMENT, + `ShortName` VARCHAR(255) NOT NULL, + `DiscordRoleId` BIGINT NOT NULL, + `ServerId` BIGINT, + `CreatedAt` DATETIME(6) NULL DEFAULT CURRENT_TIMESTAMP(6), + `LastModifiedAt` DATETIME(6) NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), + PRIMARY KEY(`Id`), + FOREIGN KEY (`ServerId`) REFERENCES `Servers`(`ServerId`) + ); + """ + ) + ) + + self._exec("config/short_rule_names.sql") + + def downgrade(self): + self._cursor.execute("DROP TABLE `ShortRoleNames`;")