From 2c7a617ac89294cde1adf6434e2b5f88863b39fe Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Wed, 19 Jul 2023 13:03:38 +0200 Subject: [PATCH] Added config migration #127 --- .../bot_data/migration/config_migration.py | 146 ++++++++++++++++-- .../db_history_scripts/config/server.sql | 117 ++++++++++++++ .../config/server_afk_channels.sql | 57 +++++++ .../config/server_team_roles.sql | 62 ++++++++ .../db_history_scripts/config/technician.sql | 67 ++++++++ .../config/technician_ids.sql | 52 +++++++ .../config/technician_ping_urls.sql | 52 +++++++ 7 files changed, 540 insertions(+), 13 deletions(-) create mode 100644 kdb-bot/src/bot_data/migration/db_history_scripts/config/server.sql create mode 100644 kdb-bot/src/bot_data/migration/db_history_scripts/config/server_afk_channels.sql create mode 100644 kdb-bot/src/bot_data/migration/db_history_scripts/config/server_team_roles.sql create mode 100644 kdb-bot/src/bot_data/migration/db_history_scripts/config/technician.sql create mode 100644 kdb-bot/src/bot_data/migration/db_history_scripts/config/technician_ids.sql create mode 100644 kdb-bot/src/bot_data/migration/db_history_scripts/config/technician_ping_urls.sql diff --git a/kdb-bot/src/bot_data/migration/config_migration.py b/kdb-bot/src/bot_data/migration/config_migration.py index abdb6e5b..0370cbb4 100644 --- a/kdb-bot/src/bot_data/migration/config_migration.py +++ b/kdb-bot/src/bot_data/migration/config_migration.py @@ -1,3 +1,5 @@ +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,22 +14,140 @@ class ConfigMigration(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._server_upgrade() + self._technician_upgrade() - # self._cursor.execute( - # str( - # f""" - # CREATE TABLE IF NOT EXISTS `Config` ( - # `Id` BIGINT NOT NULL AUTO_INCREMENT, - # `CreatedAt` DATETIME(6), - # `LastModifiedAt` DATETIME(6), - # PRIMARY KEY(`Id`) - # ); - # """ - # ) - # ) + self._exec("config/server.sql") + self._exec("config/server_afk_channels.sql") + self._exec("config/server_team_roles.sql") + self._exec("config/technician.sql") + self._exec("config/technician_ids.sql") + self._exec("config/technician_ping_urls.sql") + + def _server_upgrade(self): + self._cursor.execute( + str( + f""" + CREATE TABLE IF NOT EXISTS `CFG_Server` ( + `Id` BIGINT NOT NULL AUTO_INCREMENT, + `MessageDeleteTimer` BIGINT NOT NULL DEFAULT 6, + `NotificationChatId` BIGINT NOT NULL, + `MaxVoiceStateHours` BIGINT NOT NULL DEFAULT 6, + `XpPerMessage` BIGINT NOT NULL DEFAULT 1, + `XpPerReaction` BIGINT NOT NULL DEFAULT 1, + `MaxMessageXpPerHour` BIGINT NOT NULL DEFAULT 20, + `XpPerOntimeHour` BIGINT NOT NULL DEFAULT 10, + `XpPerEventParticipation` BIGINT NOT NULL DEFAULT 10, + `XpPerAchievement` BIGINT NOT NULL DEFAULT 10, + `AFKCommandChannelId` BIGINT NOT NULL, + `HelpVoiceChannelId` BIGINT NOT NULL, + `TeamChannelId` BIGINT NOT NULL, + `LoginMessageChannelId` BIGINT NOT NULL, + `ServerId` BIGINT NOT NULL, + `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._cursor.execute( + str( + f""" + CREATE TABLE IF NOT EXISTS `CFG_ServerAFKChannelIds` ( + `Id` BIGINT NOT NULL AUTO_INCREMENT, + `ChannelId` BIGINT NOT NULL, + `ServerId` BIGINT NOT NULL, + `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._cursor.execute( + str( + f""" + CREATE TABLE IF NOT EXISTS `CFG_ServerTeamRoleIds` ( + `Id` BIGINT NOT NULL AUTO_INCREMENT, + `RoleId` BIGINT NOT NULL, + `TeamMemberType` ENUM('Moderator', 'Admin') NOT NULL, + `ServerId` BIGINT NOT NULL, + `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`) + ); + """ + ) + ) + + def _technician_upgrade(self): + self._cursor.execute( + str( + f""" + CREATE TABLE IF NOT EXISTS `CFG_Technician` ( + `Id` BIGINT NOT NULL AUTO_INCREMENT, + `HelpCommandReferenceUrl` VARCHAR(255) NOT NULL, + `WaitForRestart` BIGINT NOT NULL DEFAULT 8, + `WaitForShutdown` BIGINT NOT NULL DEFAULT 8, + `CacheMaxMessages` BIGINT NOT NULL DEFAULT 1000000, + `CreatedAt` DATETIME(6) NULL DEFAULT CURRENT_TIMESTAMP(6), + `LastModifiedAt` DATETIME(6) NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), + PRIMARY KEY(`Id`) + ); + """ + ) + ) + + self._cursor.execute( + str( + f""" + CREATE TABLE IF NOT EXISTS `CFG_TechnicianPingUrls` ( + `Id` BIGINT NOT NULL AUTO_INCREMENT, + `URL` VARCHAR(255) NOT NULL, + `CreatedAt` DATETIME(6) NULL DEFAULT CURRENT_TIMESTAMP(6), + `LastModifiedAt` DATETIME(6) NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), + PRIMARY KEY(`Id`) + ); + """ + ) + ) + + self._cursor.execute( + str( + f""" + CREATE TABLE IF NOT EXISTS `CFG_TechnicianIds` ( + `Id` BIGINT NOT NULL AUTO_INCREMENT, + `TechnicianId` BIGINT NOT NULL, + `CreatedAt` DATETIME(6) NULL DEFAULT CURRENT_TIMESTAMP(6), + `LastModifiedAt` DATETIME(6) NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), + PRIMARY KEY(`Id`) + ); + """ + ) + ) def downgrade(self): self._logger.debug(__name__, "Running downgrade") - # self._cursor.execute("DROP TABLE `Config`;") + + def _server_downgrade(self): + self._cursor.execute("DROP TABLE `CFG_Server`;") + + def _technician_downgrade(self): + self._cursor.execute("DROP TABLE `CFG_Technician`;") + self._cursor.execute("DROP TABLE `CFG_TechnicianPingUrls`;") + self._cursor.execute("DROP TABLE `CFG_TechnicianIds`;") diff --git a/kdb-bot/src/bot_data/migration/db_history_scripts/config/server.sql b/kdb-bot/src/bot_data/migration/db_history_scripts/config/server.sql new file mode 100644 index 00000000..ed2be14a --- /dev/null +++ b/kdb-bot/src/bot_data/migration/db_history_scripts/config/server.sql @@ -0,0 +1,117 @@ +CREATE TABLE IF NOT EXISTS `CFG_ServerHistory` +( + `Id` BIGINT(20) NOT NULL, + `MessageDeleteTimer` BIGINT NOT NULL DEFAULT 6, + `NotificationChatId` BIGINT NOT NULL, + `MaxVoiceStateHours` BIGINT NOT NULL DEFAULT 6, + `XpPerMessage` BIGINT NOT NULL DEFAULT 1, + `XpPerReaction` BIGINT NOT NULL DEFAULT 1, + `MaxMessageXpPerHour` BIGINT NOT NULL DEFAULT 20, + `XpPerOntimeHour` BIGINT NOT NULL DEFAULT 10, + `XpPerEventParticipation` BIGINT NOT NULL DEFAULT 10, + `XpPerAchievement` BIGINT NOT NULL DEFAULT 10, + `AFKCommandChannelId` BIGINT NOT NULL, + `HelpVoiceChannelId` BIGINT NOT NULL, + `TeamChannelId` BIGINT NOT NULL, + `LoginMessageChannelId` BIGINT NOT NULL, + `ServerId` BIGINT NOT NULL, + `Deleted` BOOL DEFAULT FALSE, + `DateFrom` DATETIME(6) NOT NULL, + `DateTo` DATETIME(6) NOT NULL +); + +DROP TRIGGER IF EXISTS `TR_CFG_ServerUpdate`; + +CREATE TRIGGER `TR_CFG_ServerUpdate` + AFTER UPDATE + ON `CFG_Server` + FOR EACH ROW +BEGIN + INSERT INTO `CFG_ServerHistory` ( + `Id`, + `MessageDeleteTimer`, + `NotificationChatId`, + `MaxVoiceStateHours`, + `XpPerMessage`, + `XpPerReaction`, + `MaxMessageXpPerHour`, + `XpPerOntimeHour`, + `XpPerEventParticipation`, + `XpPerAchievement`, + `AFKCommandChannelId`, + `HelpVoiceChannelId`, + `TeamChannelId`, + `LoginMessageChannelId`, + `ServerId`, + `DateFrom`, + `DateTo` + ) + VALUES ( + OLD.UserId, + OLD.MessageDeleteTimer, + OLD.NotificationChatId, + OLD.MaxVoiceStateHours, + OLD.XpPerMessage, + OLD.XpPerReaction, + OLD.MaxMessageXpPerHour, + OLD.XpPerOntimeHour, + OLD.XpPerEventParticipation, + OLD.XpPerAchievement, + OLD.AFKCommandChannelId, + OLD.HelpVoiceChannelId, + OLD.TeamChannelId, + OLD.LoginMessageChannelId, + OLD.ServerId, + OLD.LastModifiedAt, + CURRENT_TIMESTAMP(6) + ); +END; + +DROP TRIGGER IF EXISTS `TR_CFG_ServerDelete`; + +CREATE TRIGGER `TR_CFG_ServerDelete` + AFTER DELETE + ON `CFG_Server` + FOR EACH ROW +BEGIN + INSERT INTO `CFG_ServerHistory` ( + `Id`, + `MessageDeleteTimer`, + `NotificationChatId`, + `MaxVoiceStateHours`, + `XpPerMessage`, + `XpPerReaction`, + `MaxMessageXpPerHour`, + `XpPerOntimeHour`, + `XpPerEventParticipation`, + `XpPerAchievement`, + `AFKCommandChannelId`, + `HelpVoiceChannelId`, + `TeamChannelId`, + `LoginMessageChannelId`, + `ServerId`, + `Deleted`, + `DateFrom`, + `DateTo` + ) + VALUES ( + OLD.UserId, + OLD.MessageDeleteTimer, + OLD.NotificationChatId, + OLD.MaxVoiceStateHours, + OLD.XpPerMessage, + OLD.XpPerReaction, + OLD.MaxMessageXpPerHour, + OLD.XpPerOntimeHour, + OLD.XpPerEventParticipation, + OLD.XpPerAchievement, + OLD.AFKCommandChannelId, + OLD.HelpVoiceChannelId, + OLD.TeamChannelId, + OLD.LoginMessageChannelId, + OLD.ServerId, + TRUE, + OLD.LastModifiedAt, + CURRENT_TIMESTAMP(6) + ); +END; \ No newline at end of file diff --git a/kdb-bot/src/bot_data/migration/db_history_scripts/config/server_afk_channels.sql b/kdb-bot/src/bot_data/migration/db_history_scripts/config/server_afk_channels.sql new file mode 100644 index 00000000..fbd14d7b --- /dev/null +++ b/kdb-bot/src/bot_data/migration/db_history_scripts/config/server_afk_channels.sql @@ -0,0 +1,57 @@ +CREATE TABLE IF NOT EXISTS `CFG_ServerAFKChannelIdsHistory` +( + `Id` BIGINT(20) NOT NULL, + `ChannelId` BIGINT NOT NULL, + `ServerId` BIGINT NOT NULL, + `Deleted` BOOL DEFAULT FALSE, + `DateFrom` DATETIME(6) NOT NULL, + `DateTo` DATETIME(6) NOT NULL +); + +DROP TRIGGER IF EXISTS `TR_CFG_ServerAFKChannelIdsUpdate`; + +CREATE TRIGGER `TR_CFG_ServerAFKChannelIdsUpdate` + AFTER UPDATE + ON `CFG_ServerAFKChannelIds` + FOR EACH ROW +BEGIN + INSERT INTO `CFG_ServerAFKChannelIdsHistory` ( + `Id`, + `ChannelId`, + `ServerId`, + `DateFrom`, + `DateTo` + ) + VALUES ( + OLD.UserId, + OLD.ChannelId, + OLD.ServerId, + OLD.LastModifiedAt, + CURRENT_TIMESTAMP(6) + ); +END; + +DROP TRIGGER IF EXISTS `TR_CFG_ServerAFKChannelIdsDelete`; + +CREATE TRIGGER `TR_CFG_ServerAFKChannelIdsDelete` + AFTER DELETE + ON `CFG_ServerAFKChannelIds` + FOR EACH ROW +BEGIN + INSERT INTO `CFG_ServerAFKChannelIdsHistory` ( + `Id`, + `ChannelId`, + `ServerId`, + `Deleted`, + `DateFrom`, + `DateTo` + ) + VALUES ( + OLD.UserId, + OLD.ChannelId, + OLD.ServerId, + TRUE, + OLD.LastModifiedAt, + CURRENT_TIMESTAMP(6) + ); +END; \ No newline at end of file diff --git a/kdb-bot/src/bot_data/migration/db_history_scripts/config/server_team_roles.sql b/kdb-bot/src/bot_data/migration/db_history_scripts/config/server_team_roles.sql new file mode 100644 index 00000000..bbc1aed3 --- /dev/null +++ b/kdb-bot/src/bot_data/migration/db_history_scripts/config/server_team_roles.sql @@ -0,0 +1,62 @@ +CREATE TABLE IF NOT EXISTS `CFG_ServerTeamRoleIdsHistory` +( + `Id` BIGINT(20) NOT NULL, + `RoleId` BIGINT NOT NULL, + `TeamMemberType` ENUM('Moderator', 'Admin') NOT NULL, + `ServerId` BIGINT NOT NULL, + `Deleted` BOOL DEFAULT FALSE, + `DateFrom` DATETIME(6) NOT NULL, + `DateTo` DATETIME(6) NOT NULL +); + +DROP TRIGGER IF EXISTS `TR_CFG_ServerTeamRoleIdsUpdate`; + +CREATE TRIGGER `TR_CFG_ServerTeamRoleIdsUpdate` + AFTER UPDATE + ON `CFG_ServerTeamRoleIds` + FOR EACH ROW +BEGIN + INSERT INTO `CFG_ServerTeamRoleIdsHistory` ( + `Id`, + `RoleId`, + `TeamMemberType`, + `ServerId`, + `DateFrom`, + `DateTo` + ) + VALUES ( + OLD.UserId, + OLD.RoleId, + OLD.TeamMemberType, + OLD.ServerId, + OLD.LastModifiedAt, + CURRENT_TIMESTAMP(6) + ); +END; + +DROP TRIGGER IF EXISTS `TR_CFG_ServerTeamRoleIdsDelete`; + +CREATE TRIGGER `TR_CFG_ServerTeamRoleIdsDelete` + AFTER DELETE + ON `CFG_ServerTeamRoleIds` + FOR EACH ROW +BEGIN + INSERT INTO `CFG_ServerTeamRoleIdsHistory` ( + `Id`, + `RoleId`, + `TeamMemberType`, + `ServerId`, + `Deleted`, + `DateFrom`, + `DateTo` + ) + VALUES ( + OLD.UserId, + OLD.RoleId, + OLD.TeamMemberType, + OLD.ServerId, + TRUE, + OLD.LastModifiedAt, + CURRENT_TIMESTAMP(6) + ); +END; \ No newline at end of file diff --git a/kdb-bot/src/bot_data/migration/db_history_scripts/config/technician.sql b/kdb-bot/src/bot_data/migration/db_history_scripts/config/technician.sql new file mode 100644 index 00000000..2ebf8500 --- /dev/null +++ b/kdb-bot/src/bot_data/migration/db_history_scripts/config/technician.sql @@ -0,0 +1,67 @@ +CREATE TABLE IF NOT EXISTS `CFG_TechnicianHistory` +( + `Id` BIGINT(20) NOT NULL, + `HelpCommandReferenceUrl` VARCHAR(255) NOT NULL, + `WaitForRestart` BIGINT NOT NULL DEFAULT 8, + `WaitForShutdown` BIGINT NOT NULL DEFAULT 8, + `CacheMaxMessages` BIGINT NOT NULL DEFAULT 1000000, + `Deleted` BOOL DEFAULT FALSE, + `DateFrom` DATETIME(6) NOT NULL, + `DateTo` DATETIME(6) NOT NULL +); + +DROP TRIGGER IF EXISTS `TR_CFG_TechnicianUpdate`; + +CREATE TRIGGER `TR_CFG_TechnicianUpdate` + AFTER UPDATE + ON `CFG_Technician` + FOR EACH ROW +BEGIN + INSERT INTO `CFG_TechnicianHistory` ( + `Id`, + `HelpCommandReferenceUrl`, + `WaitForRestart`, + `WaitForShutdown`, + `CacheMaxMessages`, + `DateFrom`, + `DateTo` + ) + VALUES ( + OLD.UserId, + OLD.HelpCommandReferenceUrl, + OLD.WaitForRestart, + OLD.WaitForShutdown, + OLD.CacheMaxMessages, + OLD.LastModifiedAt, + CURRENT_TIMESTAMP(6) + ); +END; + +DROP TRIGGER IF EXISTS `TR_CFG_TechnicianDelete`; + +CREATE TRIGGER `TR_CFG_TechnicianDelete` + AFTER DELETE + ON `CFG_Technician` + FOR EACH ROW +BEGIN + INSERT INTO `CFG_TechnicianHistory` ( + `Id`, + `HelpCommandReferenceUrl`, + `WaitForRestart`, + `WaitForShutdown`, + `CacheMaxMessages`, + `Deleted`, + `DateFrom`, + `DateTo` + ) + VALUES ( + OLD.UserId, + OLD.HelpCommandReferenceUrl, + OLD.WaitForRestart, + OLD.WaitForShutdown, + OLD.CacheMaxMessages, + TRUE, + OLD.LastModifiedAt, + CURRENT_TIMESTAMP(6) + ); +END; \ No newline at end of file diff --git a/kdb-bot/src/bot_data/migration/db_history_scripts/config/technician_ids.sql b/kdb-bot/src/bot_data/migration/db_history_scripts/config/technician_ids.sql new file mode 100644 index 00000000..3d13de75 --- /dev/null +++ b/kdb-bot/src/bot_data/migration/db_history_scripts/config/technician_ids.sql @@ -0,0 +1,52 @@ +CREATE TABLE IF NOT EXISTS `CFG_TechnicianIdsHistory` +( + `Id` BIGINT(20) NOT NULL, + `TechnicianId` BIGINT NOT NULL, + `Deleted` BOOL DEFAULT FALSE, + `DateFrom` DATETIME(6) NOT NULL, + `DateTo` DATETIME(6) NOT NULL +); + +DROP TRIGGER IF EXISTS `TR_CFG_TechnicianIdsUpdate`; + +CREATE TRIGGER `TR_CFG_TechnicianIdsUpdate` + AFTER UPDATE + ON `CFG_TechnicianIds` + FOR EACH ROW +BEGIN + INSERT INTO `CFG_TechnicianIdsHistory` ( + `Id`, + `TechnicianId`, + `DateFrom`, + `DateTo` + ) + VALUES ( + OLD.UserId, + OLD.TechnicianId, + OLD.LastModifiedAt, + CURRENT_TIMESTAMP(6) + ); +END; + +DROP TRIGGER IF EXISTS `TR_CFG_TechnicianIdsDelete`; + +CREATE TRIGGER `TR_CFG_TechnicianIdsDelete` + AFTER DELETE + ON `CFG_TechnicianIds` + FOR EACH ROW +BEGIN + INSERT INTO `CFG_TechnicianIdsHistory` ( + `Id`, + `TechnicianId`, + `Deleted`, + `DateFrom`, + `DateTo` + ) + VALUES ( + OLD.UserId, + OLD.TechnicianId, + TRUE, + OLD.LastModifiedAt, + CURRENT_TIMESTAMP(6) + ); +END; \ No newline at end of file diff --git a/kdb-bot/src/bot_data/migration/db_history_scripts/config/technician_ping_urls.sql b/kdb-bot/src/bot_data/migration/db_history_scripts/config/technician_ping_urls.sql new file mode 100644 index 00000000..fdab3226 --- /dev/null +++ b/kdb-bot/src/bot_data/migration/db_history_scripts/config/technician_ping_urls.sql @@ -0,0 +1,52 @@ +CREATE TABLE IF NOT EXISTS `CFG_TechnicianPingUrlsHistory` +( + `Id` BIGINT(20) NOT NULL, + `URL` VARCHAR(255) NOT NULL, + `Deleted` BOOL DEFAULT FALSE, + `DateFrom` DATETIME(6) NOT NULL, + `DateTo` DATETIME(6) NOT NULL +); + +DROP TRIGGER IF EXISTS `TR_CFG_TechnicianPingUrlsUpdate`; + +CREATE TRIGGER `TR_CFG_TechnicianPingUrlsUpdate` + AFTER UPDATE + ON `CFG_TechnicianPingUrls` + FOR EACH ROW +BEGIN + INSERT INTO `CFG_TechnicianPingUrlsHistory` ( + `Id`, + `URL`, + `DateFrom`, + `DateTo` + ) + VALUES ( + OLD.UserId, + OLD.URL, + OLD.LastModifiedAt, + CURRENT_TIMESTAMP(6) + ); +END; + +DROP TRIGGER IF EXISTS `TR_CFG_TechnicianPingUrlsDelete`; + +CREATE TRIGGER `TR_CFG_TechnicianPingUrlsDelete` + AFTER DELETE + ON `CFG_TechnicianPingUrls` + FOR EACH ROW +BEGIN + INSERT INTO `CFG_TechnicianPingUrlsHistory` ( + `Id`, + `URL`, + `Deleted`, + `DateFrom`, + `DateTo` + ) + VALUES ( + OLD.UserId, + OLD.URL, + TRUE, + OLD.LastModifiedAt, + CURRENT_TIMESTAMP(6) + ); +END; \ No newline at end of file