From d24d3fa4deb3d3c32d37afd0134806f0815e65fc Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Mon, 6 Mar 2023 17:46:56 +0100 Subject: [PATCH] [WIP] Added more tables #246 --- .../src/bot/startup_migration_extension.py | 2 + .../migration/db_history_migration.py | 306 +++++------------- .../migration/db_history_scripts/api_keys.sql | 49 +++ .../auth_user_users_relation.sql | 49 +++ .../db_history_scripts/auth_users.sql | 65 ++++ .../db_history_scripts/auto_role_rules.sql | 49 +++ .../db_history_scripts/auto_roles.sql | 51 +++ .../migration/db_history_scripts/clients.sql | 57 ++++ kdb-bot/src/bot_data/model/auth_user.py | 11 +- .../model/auth_user_users_relation.py | 9 +- kdb-bot/src/bot_data/model/auto_role.py | 9 +- kdb-bot/src/bot_data/model/auto_role_rule.py | 9 +- kdb-bot/src/bot_data/model/client.py | 6 +- 13 files changed, 424 insertions(+), 248 deletions(-) create mode 100644 kdb-bot/src/bot_data/migration/db_history_scripts/api_keys.sql create mode 100644 kdb-bot/src/bot_data/migration/db_history_scripts/auth_user_users_relation.sql create mode 100644 kdb-bot/src/bot_data/migration/db_history_scripts/auth_users.sql create mode 100644 kdb-bot/src/bot_data/migration/db_history_scripts/auto_role_rules.sql create mode 100644 kdb-bot/src/bot_data/migration/db_history_scripts/auto_roles.sql create mode 100644 kdb-bot/src/bot_data/migration/db_history_scripts/clients.sql diff --git a/kdb-bot/src/bot/startup_migration_extension.py b/kdb-bot/src/bot/startup_migration_extension.py index a41b19cb..5eba461b 100644 --- a/kdb-bot/src/bot/startup_migration_extension.py +++ b/kdb-bot/src/bot/startup_migration_extension.py @@ -8,6 +8,7 @@ from bot_data.migration.api_key_migration import ApiKeyMigration from bot_data.migration.api_migration import ApiMigration from bot_data.migration.auto_role_fix1_migration import AutoRoleFix1Migration from bot_data.migration.auto_role_migration import AutoRoleMigration +from bot_data.migration.db_history_migration import DBHistoryMigration 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 @@ -40,3 +41,4 @@ class StartupMigrationExtension(StartupExtensionABC): services.add_transient(MigrationABC, UserJoinedGameServerMigration) # 12.02.2023 #181 - 1.0.0 services.add_transient(MigrationABC, RemoveStatsMigration) # 19.02.2023 #190 - 1.0.0 services.add_transient(MigrationABC, UserWarningMigration) # 21.02.2023 #35 - 1.0.0 + services.add_transient(MigrationABC, DBHistoryMigration) # 06.03.2023 #246 - 1.0.0 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 580944d4..405f384d 100644 --- a/kdb-bot/src/bot_data/migration/db_history_migration.py +++ b/kdb-bot/src/bot_data/migration/db_history_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,150 +14,17 @@ class DBHistoryMigration(MigrationABC): self._db = db self._cursor = db.cursor - def _api_keys(self): - self._cursor.execute( - f""" - ALTER TABLE `ApiKeys` - MODIFY `LastModifiedAt` DATETIME(6) NULL ON UPDATE CURRENT_TIMESTAMP(6); - - ALTER TABLE `ApiKeys` - ALTER COLUMN `CreatedAt` SET DEFAULT CURRENT_TIMESTAMP(6); - - ALTER TABLE `ApiKeys` - ALTER COLUMN `LastModifiedAt` SET DEFAULT CURRENT_TIMESTAMP(6); - - CREATE TABLE IF NOT EXISTS `ApiKeysHistory` - ( - `Id` BIGINT(20) NOT NULL, - `Identifier` VARCHAR(255) NOT NULL, - `Key` VARCHAR(255) NOT NULL, - `CreatorId` BIGINT(20) DEFAULT NULL, - `Deleted` BOOL DEFAULT FALSE, - `DateFrom` DATETIME(6) NOT NULL, - `DateTo` DATETIME(6) NOT NULL - ); - - DROP TRIGGER IF EXISTS `TR_ApiKeysUpdate`; - - CREATE TRIGGER `TR_ApiKeysUpdate` - AFTER UPDATE - ON `ApiKeys` - FOR EACH ROW - BEGIN - INSERT INTO `ApiKeysHistory` (`Id`, `Identifier`, `Key`, `CreatorId`, `DateFrom`, `DateTo`) - VALUES (OLD.Id, OLD.Identifier, OLD.Key, OLD.CreatorId, OLD.LastModifiedAt, CURRENT_TIMESTAMP(6)); - END; - - DROP TRIGGER IF EXISTS `TR_ApiKeysDelete`; - - CREATE TRIGGER `TR_ApiKeysDelete` - AFTER DELETE - ON `ApiKeys` - FOR EACH ROW - BEGIN - INSERT INTO `ApiKeysHistory` (`Id`, `Identifier`, `Key`, `CreatorId`, `Deleted`, `DateFrom`, `DateTo`) - VALUES (OLD.Id, OLD.Identifier, OLD.Key, OLD.CreatorId, TRUE, OLD.LastModifiedAt, CURRENT_TIMESTAMP(6)); - END; - """ - ) - - def _auth_users(self): - self._cursor.execute( - f""" - CREATE TABLE IF NOT EXISTS `AuthUsersHistory` - ( - `Id` bigint(20) NOT NULL, - `FirstName` varchar(255) DEFAULT NULL, - `LastName` varchar(255) DEFAULT NULL, - `EMail` varchar(255) DEFAULT NULL, - `Password` varchar(255) DEFAULT NULL, - `PasswordSalt` varchar(255) DEFAULT NULL, - `RefreshToken` varchar(255) DEFAULT NULL, - `ConfirmationId` varchar(255) DEFAULT NULL, - `ForgotPasswordId` varchar(255) DEFAULT NULL, - `OAuthId` varchar(255) DEFAULT NULL, - `RefreshTokenExpiryTime` datetime(6) NOT NULL, - `AuthRole` int(11) NOT NULL DEFAULT 0, - `DateFrom` datetime(6) NOT NULL, - `DateTo` datetime(6) NOT NULL - ); - """ - ) - - def _auth_user_users_relation(self): - self._cursor.execute( - f""" - CREATE TABLE IF NOT EXISTS `AuthUserUsersRelationsHistory` - ( - `Id` bigint(20) NOT NULL, - `AuthUserId` bigint(20) DEFAULT NULL, - `UserId` bigint(20) DEFAULT NULL, - `DateFrom` datetime(6) NOT NULL, - `DateTo` datetime(6) NOT NULL - ); - """ - ) - - def _auto_role_rules(self): - self._cursor.execute( - f""" - CREATE TABLE IF NOT EXISTS `AutoRoleRulesHistory` - ( - `AutoRoleRuleId` bigint(20) NOT NULL, - `AutoRoleId` bigint(20) DEFAULT NULL, - `DiscordEmojiName` varchar(64) DEFAULT NULL, - `DiscordRoleId` bigint(20) NOT NULL, - `DateFrom` datetime(6) NOT NULL, - `DateTo` datetime(6) NOT NULL - ); - """ - ) - - def _auto_roles(self): - self._cursor.execute( - f""" - CREATE TABLE IF NOT EXISTS `AutoRolesHistory` - ( - `AutoRoleId` bigint(20) NOT NULL, - `ServerId` bigint(20) DEFAULT NULL, - `DiscordChannelId` bigint(20) NOT NULL, - `DiscordMessageId` bigint(20) NOT NULL, - `DateFrom` datetime(6) NOT NULL, - `DateTo` datetime(6) NOT NULL - ); - """ - ) - - def _clients_history(self): - self._cursor.execute( - f""" - CREATE TABLE IF NOT EXISTS `ClientsHistory` - ( - `ClientId` bigint(20) NOT NULL, - `DiscordClientId` bigint(20) NOT NULL, - `SentMessageCount` bigint(20) NOT NULL DEFAULT 0, - `ReceivedMessageCount` bigint(20) NOT NULL DEFAULT 0, - `DeletedMessageCount` bigint(20) NOT NULL DEFAULT 0, - `ReceivedCommandsCount` bigint(20) NOT NULL DEFAULT 0, - `MovedUsersCount` bigint(20) NOT NULL DEFAULT 0, - `ServerId` bigint(20) DEFAULT NULL, - `DateFrom` datetime(6) NOT NULL, - `DateTo` datetime(6) NOT NULL - ); - """ - ) - def _game_servers(self): self._cursor.execute( f""" CREATE TABLE IF NOT EXISTS `GameServersHistory` ( - `Id` bigint(20) NOT NULL, - `Name` varchar(255) NOT NULL, - `ServerId` bigint(20) NOT NULL, - `ApiKeyId` bigint(20) NOT NULL, - `DateFrom` datetime(6) NOT NULL, - `DateTo` datetime(6) NOT NULL + `Id` BIGINT(20) NOT NULL, + `Name` VARCHAR(255) NOT NULL, + `ServerId` BIGINT(20) NOT NULL, + `ApiKeyId` BIGINT(20) NOT NULL, + `DateFrom` DATETIME(6) NOT NULL, + `DateTo` DATETIME(6) NOT NULL ); """ ) @@ -165,10 +34,10 @@ class DBHistoryMigration(MigrationABC): f""" CREATE TABLE IF NOT EXISTS `KnownUsersHistory` ( - `KnownUserId` bigint(20) NOT NULL, - `DiscordId` bigint(20) NOT NULL, - `DateFrom` datetime(6) NOT NULL, - `DateTo` datetime(6) NOT NULL + `KnownUserId` BIGINT(20) NOT NULL, + `DiscordId` BIGINT(20) NOT NULL, + `DateFrom` DATETIME(6) NOT NULL, + `DateTo` DATETIME(6) NOT NULL ); """ ) @@ -178,14 +47,14 @@ class DBHistoryMigration(MigrationABC): f""" CREATE TABLE IF NOT EXISTS `LevelsHistory` ( - `Id` bigint(20) NOT NULL, - `Name` varchar(255) NOT NULL, - `Color` varchar(8) NOT NULL, - `MinXp` bigint(20) NOT NULL, - `PermissionInt` bigint(20) NOT NULL, - `ServerId` bigint(20) DEFAULT NULL, - `DateFrom` datetime(6) NOT NULL, - `DateTo` datetime(6) NOT NULL + `Id` BIGINT(20) NOT NULL, + `Name` VARCHAR(255) NOT NULL, + `Color` VARCHAR(8) NOT NULL, + `MinXp` BIGINT(20) NOT NULL, + `PermissionInt` BIGINT(20) NOT NULL, + `ServerId` BIGINT(20) DEFAULT NULL, + `DateFrom` DATETIME(6) NOT NULL, + `DateTo` DATETIME(6) NOT NULL ); """ ) @@ -195,10 +64,10 @@ class DBHistoryMigration(MigrationABC): f""" CREATE TABLE IF NOT EXISTS `ServersHistory` ( - `ServerId` bigint(20) NOT NULL, - `DiscordServerId` bigint(20) NOT NULL, - `DateFrom` datetime(6) NOT NULL, - `DateTo` datetime(6) NOT NULL + `ServerId` BIGINT(20) NOT NULL, + `DiscordServerId` BIGINT(20) NOT NULL, + `DateFrom` DATETIME(6) NOT NULL, + `DateTo` DATETIME(6) NOT NULL ); """ ) @@ -208,12 +77,12 @@ class DBHistoryMigration(MigrationABC): f""" CREATE TABLE IF NOT EXISTS `UserGameIdentsHistory` ( - `Id` bigint(20) NOT NULL, - `UserId` bigint(20) NOT NULL, - `GameServerId` bigint(20) NOT NULL, - `Ident` varchar(255) NOT NULL, - `DateFrom` datetime(6) NOT NULL, - `DateTo` datetime(6) NOT NULL + `Id` BIGINT(20) NOT NULL, + `UserId` BIGINT(20) NOT NULL, + `GameServerId` BIGINT(20) NOT NULL, + `Ident` VARCHAR(255) NOT NULL, + `DateFrom` DATETIME(6) NOT NULL, + `DateTo` DATETIME(6) NOT NULL ); """ ) @@ -223,13 +92,13 @@ class DBHistoryMigration(MigrationABC): f""" CREATE TABLE IF NOT EXISTS `UserJoinedGameServerHistory` ( - `Id` bigint(20) NOT NULL, - `UserId` bigint(20) NOT NULL, - `GameServerId` bigint(20) NOT NULL, - `JoinedOn` datetime(6) NOT NULL, - `LeavedOn` datetime(6) DEFAULT NULL, - `DateFrom` datetime(6) NOT NULL, - `DateTo` datetime(6) NOT NULL + `Id` BIGINT(20) NOT NULL, + `UserId` BIGINT(20) NOT NULL, + `GameServerId` BIGINT(20) NOT NULL, + `JoinedOn` DATETIME(6) NOT NULL, + `LeavedOn` DATETIME(6) DEFAULT NULL, + `DateFrom` DATETIME(6) NOT NULL, + `DateTo` DATETIME(6) NOT NULL ); """ ) @@ -239,12 +108,12 @@ class DBHistoryMigration(MigrationABC): f""" CREATE TABLE IF NOT EXISTS `UserJoinedServersHistory` ( - `JoinId` bigint(20) NOT NULL, - `UserId` bigint(20) NOT NULL, - `JoinedOn` datetime(6) NOT NULL, - `LeavedOn` datetime(6) DEFAULT NULL, - `DateFrom` datetime(6) NOT NULL, - `DateTo` datetime(6) NOT NULL + `JoinId` BIGINT(20) NOT NULL, + `UserId` BIGINT(20) NOT NULL, + `JoinedOn` DATETIME(6) NOT NULL, + `LeavedOn` DATETIME(6) DEFAULT NULL, + `DateFrom` DATETIME(6) NOT NULL, + `DateTo` DATETIME(6) NOT NULL ); """ ) @@ -254,13 +123,13 @@ class DBHistoryMigration(MigrationABC): f""" CREATE TABLE IF NOT EXISTS `UserJoinedVoiceChannelHistory` ( - `JoinId` bigint(20) NOT NULL, - `UserId` bigint(20) NOT NULL, - `DiscordChannelId` bigint(20) NOT NULL, - `JoinedOn` datetime(6) NOT NULL, - `LeavedOn` datetime(6) DEFAULT NULL, - `DateFrom` datetime(6) NOT NULL, - `DateTo` datetime(6) NOT NULL + `JoinId` BIGINT(20) NOT NULL, + `UserId` BIGINT(20) NOT NULL, + `DiscordChannelId` BIGINT(20) NOT NULL, + `JoinedOn` DATETIME(6) NOT NULL, + `LeavedOn` DATETIME(6) DEFAULT NULL, + `DateFrom` DATETIME(6) NOT NULL, + `DateTo` DATETIME(6) NOT NULL ); """ ) @@ -270,13 +139,13 @@ class DBHistoryMigration(MigrationABC): f""" CREATE TABLE IF NOT EXISTS `UserMessageCountPerHourHistory` ( - `Id` bigint(20) NOT NULL, - `Date` datetime(6) NOT NULL, - `Hour` bigint(20) DEFAULT NULL, - `XPCount` bigint(20) DEFAULT NULL, - `UserId` bigint(20) DEFAULT NULL, - `DateFrom` datetime(6) NOT NULL, - `DateTo` datetime(6) NOT NULL + `Id` BIGINT(20) NOT NULL, + `Date` DATETIME(6) NOT NULL, + `Hour` BIGINT(20) DEFAULT NULL, + `XPCount` BIGINT(20) DEFAULT NULL, + `UserId` BIGINT(20) DEFAULT NULL, + `DateFrom` DATETIME(6) NOT NULL, + `DateTo` DATETIME(6) NOT NULL ); """ ) @@ -286,12 +155,12 @@ class DBHistoryMigration(MigrationABC): f""" CREATE TABLE IF NOT EXISTS `UsersHistory` ( - `UserId` bigint(20) NOT NULL, - `DiscordId` bigint(20) NOT NULL, - `XP` bigint(20) NOT NULL DEFAULT 0, - `ServerId` bigint(20) DEFAULT NULL, - `DateFrom` datetime(6) NOT NULL, - `DateTo` datetime(6) NOT NULL + `UserId` BIGINT(20) NOT NULL, + `DiscordId` BIGINT(20) NOT NULL, + `XP` BIGINT(20) NOT NULL DEFAULT 0, + `ServerId` BIGINT(20) DEFAULT NULL, + `DateFrom` DATETIME(6) NOT NULL, + `DateTo` DATETIME(6) NOT NULL ); """ ) @@ -301,12 +170,12 @@ class DBHistoryMigration(MigrationABC): f""" CREATE TABLE IF NOT EXISTS `UserWarningsHistory` ( - `Id` bigint(20) NOT NULL, - `Description` varchar(255) NOT NULL, - `UserId` bigint(20) NOT NULL, - `Author` bigint(20) DEFAULT NULL, - `DateFrom` datetime(6) NOT NULL, - `DateTo` datetime(6) NOT NULL + `Id` BIGINT(20) NOT NULL, + `Description` VARCHAR(255) NOT NULL, + `UserId` BIGINT(20) NOT NULL, + `Author` BIGINT(20) DEFAULT NULL, + `DateFrom` DATETIME(6) NOT NULL, + `DateTo` DATETIME(6) NOT NULL ); """ ) @@ -314,23 +183,26 @@ class DBHistoryMigration(MigrationABC): def upgrade(self): self._logger.debug(__name__, "Running upgrade") - self._api_keys() - self._auth_users() - self._auth_user_users_relation() - self._auto_role_rules() - self._auto_roles() - self._clients_history() - self._game_servers() - self._known_users() - self._levels() - self._servers() - self._user_game_idents() - self._user_joined_game_server() - self._user_joined_server() - self._user_joined_voice_channel() - self._user_message_count_per_hour() - self._users() - self._user_warnings() + 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() + ) def downgrade(self): self._cursor.execute("DROP TABLE `ApiKeysHistory`;") diff --git a/kdb-bot/src/bot_data/migration/db_history_scripts/api_keys.sql b/kdb-bot/src/bot_data/migration/db_history_scripts/api_keys.sql new file mode 100644 index 00000000..aba10321 --- /dev/null +++ b/kdb-bot/src/bot_data/migration/db_history_scripts/api_keys.sql @@ -0,0 +1,49 @@ +ALTER TABLE `ApiKeys` + MODIFY `LastModifiedAt` DATETIME(6) NULL ON UPDATE CURRENT_TIMESTAMP(6); + +ALTER TABLE `ApiKeys` + ALTER COLUMN `CreatedAt` SET DEFAULT CURRENT_TIMESTAMP(6); + +ALTER TABLE `ApiKeys` + ALTER COLUMN `LastModifiedAt` SET DEFAULT CURRENT_TIMESTAMP(6); + +CREATE TABLE IF NOT EXISTS `ApiKeysHistory` +( + `Id` BIGINT(20) NOT NULL, + `Identifier` VARCHAR(255) NOT NULL, + `Key` VARCHAR(255) NOT NULL, + `CreatorId` BIGINT(20) DEFAULT NULL, + `Deleted` BOOL DEFAULT FALSE, + `DateFrom` DATETIME(6) NOT NULL, + `DateTo` DATETIME(6) NOT NULL +); + +DROP TRIGGER IF EXISTS `TR_ApiKeysUpdate`; + +CREATE TRIGGER `TR_ApiKeysUpdate` + AFTER UPDATE + ON `ApiKeys` + FOR EACH ROW +BEGIN + INSERT INTO `ApiKeysHistory` ( + `Id`, `Identifier`, `Key`, `CreatorId`, `DateFrom`, `DateTo` + ) + VALUES ( + OLD.Id, OLD.Identifier, OLD.Key, OLD.CreatorId, OLD.LastModifiedAt, CURRENT_TIMESTAMP(6) + ); +END; + +DROP TRIGGER IF EXISTS `TR_ApiKeysDelete`; + +CREATE TRIGGER `TR_ApiKeysDelete` + AFTER DELETE + ON `ApiKeys` + FOR EACH ROW +BEGIN + INSERT INTO `ApiKeysHistory` ( + `Id`, `Identifier`, `Key`, `CreatorId`, `Deleted`, `DateFrom`, `DateTo` + ) + VALUES ( + OLD.Id, OLD.Identifier, OLD.Key, OLD.CreatorId, 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/auth_user_users_relation.sql b/kdb-bot/src/bot_data/migration/db_history_scripts/auth_user_users_relation.sql new file mode 100644 index 00000000..960e180b --- /dev/null +++ b/kdb-bot/src/bot_data/migration/db_history_scripts/auth_user_users_relation.sql @@ -0,0 +1,49 @@ +ALTER TABLE `AuthUserUsersRelations` + MODIFY `LastModifiedAt` DATETIME(6) NULL ON UPDATE CURRENT_TIMESTAMP(6); + +ALTER TABLE `AuthUserUsersRelations` + ALTER COLUMN `CreatedAt` SET DEFAULT CURRENT_TIMESTAMP(6); + +ALTER TABLE `AuthUserUsersRelations` + ALTER COLUMN `LastModifiedAt` SET DEFAULT CURRENT_TIMESTAMP(6); + + +CREATE TABLE IF NOT EXISTS `AuthUserUsersRelationsHistory` +( + `Id` BIGINT(20) NOT NULL, + `AuthUserId` BIGINT(20) DEFAULT NULL, + `UserId` BIGINT(20) DEFAULT NULL, + `Deleted` BOOL DEFAULT FALSE, + `DateFrom` DATETIME(6) NOT NULL, + `DateTo` DATETIME(6) NOT NULL +); + +DROP TRIGGER IF EXISTS `TR_AuthUserUsersRelationsUpdate`; + +CREATE TRIGGER `TR_AuthUserUsersRelationsUpdate` + AFTER UPDATE + ON `AuthUserUsersRelations` + FOR EACH ROW +BEGIN + INSERT INTO `AuthUserUsersRelationsHistory` ( + `Id`, `AuthUserId`, `UserId`, `DateFrom`, `DateTo` + ) + VALUES ( + OLD.Id, OLD.AuthUserId, OLD.UserId, OLD.LastModifiedAt, CURRENT_TIMESTAMP(6) + ); +END; + +DROP TRIGGER IF EXISTS `TR_AuthUserUsersRelationsDelete`; + +CREATE TRIGGER `TR_AuthUserUsersRelationsDelete` + AFTER DELETE + ON `AuthUserUsersRelations` + FOR EACH ROW +BEGIN + INSERT INTO `AuthUserUsersRelationsHistory` ( + `Id`, `AuthUserId`, `UserId`, `Deleted`, `DateFrom`, `DateTo` + ) + VALUES ( + OLD.Id, OLD.AuthUserId, OLD.UserId, 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/auth_users.sql b/kdb-bot/src/bot_data/migration/db_history_scripts/auth_users.sql new file mode 100644 index 00000000..e71467b4 --- /dev/null +++ b/kdb-bot/src/bot_data/migration/db_history_scripts/auth_users.sql @@ -0,0 +1,65 @@ +ALTER TABLE `AuthUsers` + MODIFY `LastModifiedAt` DATETIME(6) NULL ON UPDATE CURRENT_TIMESTAMP(6); + +ALTER TABLE `AuthUsers` + ALTER COLUMN `CreatedAt` SET DEFAULT CURRENT_TIMESTAMP(6); + +ALTER TABLE `AuthUsers` + ALTER COLUMN `LastModifiedAt` SET DEFAULT CURRENT_TIMESTAMP(6); + +CREATE TABLE IF NOT EXISTS `AuthUsersHistory` +( + `Id` BIGINT(20) NOT NULL, + `FirstName` VARCHAR(255) DEFAULT NULL, + `LastName` VARCHAR(255) DEFAULT NULL, + `EMail` VARCHAR(255) DEFAULT NULL, + `Password` VARCHAR(255) DEFAULT NULL, + `PasswordSalt` VARCHAR(255) DEFAULT NULL, + `RefreshToken` VARCHAR(255) DEFAULT NULL, + `ConfirmationId` VARCHAR(255) DEFAULT NULL, + `ForgotPasswordId` VARCHAR(255) DEFAULT NULL, + `OAuthId` VARCHAR(255) DEFAULT NULL, + `RefreshTokenExpiryTime` DATETIME(6) NOT NULL, + `AuthRole` BIGINT(11) NOT NULL DEFAULT 0, + `Deleted` BOOL DEFAULT FALSE, + `DateFrom` DATETIME(6) NOT NULL, + `DateTo` DATETIME(6) NOT NULL +); + +DROP TRIGGER IF EXISTS `TR_AuthUsersUpdate`; + +CREATE TRIGGER `TR_AuthUsersUpdate` + AFTER UPDATE + ON `AuthUsers` + FOR EACH ROW +BEGIN + INSERT INTO `AuthUsersHistory` ( + `Id`, `FirstName`, `LastName`, `EMail`, `Password`, `PasswordSalt`, + `RefreshToken`, `ConfirmationId`, `ForgotPasswordId`, `OAuthId`, + `RefreshTokenExpiryTime`, `AuthRole`, `DateFrom`, `DateTo` + ) + VALUES ( + OLD.Id, OLD.FirstName, OLD.LastName, OLD.EMail, OLD.Password, OLD.PasswordSalt, OLD.RefreshToken, + OLD.ConfirmationId, OLD.ForgotPasswordId, OLD.OAuthId, OLD.RefreshTokenExpiryTime, OLD.AuthRole, + OLD.LastModifiedAt, CURRENT_TIMESTAMP(6) + ); +END; + +DROP TRIGGER IF EXISTS `TR_AuthUsersDelete`; + +CREATE TRIGGER `TR_AuthUsersDelete` + AFTER DELETE + ON `AuthUsers` + FOR EACH ROW +BEGIN + INSERT INTO `AuthUsersHistory` ( + `Id`, `FirstName`, `LastName`, `EMail`, `Password`, `PasswordSalt`, `RefreshToken`, + `ConfirmationId`, `ForgotPasswordId`, `OAuthId`, `RefreshTokenExpiryTime`, + `AuthRole`, `Deleted`, `DateFrom`, `DateTo` + ) + VALUES ( + OLD.Id, OLD.FirstName, OLD.LastName, OLD.EMail, OLD.Password, OLD.PasswordSalt, OLD.RefreshToken, + OLD.ConfirmationId, OLD.ForgotPasswordId, OLD.OAuthId, OLD.RefreshTokenExpiryTime, OLD.AuthRole, 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/auto_role_rules.sql b/kdb-bot/src/bot_data/migration/db_history_scripts/auto_role_rules.sql new file mode 100644 index 00000000..7a0154b5 --- /dev/null +++ b/kdb-bot/src/bot_data/migration/db_history_scripts/auto_role_rules.sql @@ -0,0 +1,49 @@ +ALTER TABLE `AutoRoleRules` + MODIFY `LastModifiedAt` DATETIME(6) NULL ON UPDATE CURRENT_TIMESTAMP(6); + +ALTER TABLE `AutoRoleRules` + ALTER COLUMN `CreatedAt` SET DEFAULT CURRENT_TIMESTAMP(6); + +ALTER TABLE `AutoRoleRules` + ALTER COLUMN `LastModifiedAt` SET DEFAULT CURRENT_TIMESTAMP(6); + +CREATE TABLE IF NOT EXISTS `AutoRoleRulesHistory` +( + `AutoRoleRuleId` BIGINT(20) NOT NULL, + `AutoRoleId` BIGINT(20) DEFAULT NULL, + `DiscordEmojiName` 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_AutoRoleRulesUpdate`; + +CREATE TRIGGER `TR_AutoRoleRulesUpdate` + AFTER UPDATE + ON `AutoRoleRules` + FOR EACH ROW +BEGIN + INSERT INTO `AutoRoleRulesHistory` ( + `AutoRoleRuleId`, `AutoRoleId`, `DiscordEmojiName`, `DiscordRoleId`, `DateFrom`, `DateTo` + ) + VALUES ( + OLD.AutoRoleRuleId, OLD.AutoRoleId, OLD.DiscordEmojiName, OLD.DiscordRoleId, OLD.LastModifiedAt, CURRENT_TIMESTAMP(6) + ); +END; + +DROP TRIGGER IF EXISTS `TR_AutoRoleRulesDelete`; + +CREATE TRIGGER `TR_AutoRoleRulesDelete` + AFTER DELETE + ON `AutoRoleRules` + FOR EACH ROW +BEGIN + INSERT INTO `AutoRoleRulesHistory` ( + `AutoRoleRuleId`, `AutoRoleId`, `DiscordEmojiName`, `DiscordRoleId`, `Deleted`, `DateFrom`, `DateTo` + ) + VALUES ( + OLD.AutoRoleRuleId, OLD.AutoRoleId, OLD.DiscordEmojiName, OLD.DiscordRoleId, 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/auto_roles.sql b/kdb-bot/src/bot_data/migration/db_history_scripts/auto_roles.sql new file mode 100644 index 00000000..1f9d1943 --- /dev/null +++ b/kdb-bot/src/bot_data/migration/db_history_scripts/auto_roles.sql @@ -0,0 +1,51 @@ +ALTER TABLE `AutoRoles` + MODIFY `LastModifiedAt` DATETIME(6) NULL ON UPDATE CURRENT_TIMESTAMP(6); + +ALTER TABLE `AutoRoles` + ALTER COLUMN `CreatedAt` SET DEFAULT CURRENT_TIMESTAMP(6); + +ALTER TABLE `AutoRoles` + ALTER COLUMN `LastModifiedAt` SET DEFAULT CURRENT_TIMESTAMP(6); + +CREATE TABLE IF NOT EXISTS `AutoRolesHistory` +( + `AutoRoleId` BIGINT(20) NOT NULL, + `ServerId` BIGINT(20) DEFAULT NULL, + `DiscordChannelId` BIGINT(20) NOT NULL, + `DiscordMessageId` BIGINT(20) NOT NULL, + `Deleted` BOOL DEFAULT FALSE, + `DateFrom` DATETIME(6) NOT NULL, + `DateTo` DATETIME(6) NOT NULL +); + +DROP TRIGGER IF EXISTS `TR_AutoRolesUpdate`; + +CREATE TRIGGER `TR_AutoRolesUpdate` + AFTER UPDATE + ON `AutoRoles` + FOR EACH ROW +BEGIN + INSERT INTO `AutoRolesHistory` ( + `AutoRoleId`, `ServerId`, `DiscordChannelId`, `DiscordMessageId`, `DateFrom`, `DateTo` + ) + VALUES ( + OLD.AutoRoleId, OLD.ServerId, OLD.DiscordChannelId, OLD.DiscordMessageId, OLD.LastModifiedAt, + CURRENT_TIMESTAMP(6) + ); +END; + +DROP TRIGGER IF EXISTS `TR_AutoRolesDelete`; + +CREATE TRIGGER `TR_AutoRolesDelete` + AFTER DELETE + ON `AutoRoles` + FOR EACH ROW +BEGIN + INSERT INTO `AutoRolesHistory` ( + `AutoRoleId`, `ServerId`, `DiscordChannelId`, `DiscordMessageId`, `Deleted`, `DateFrom`, `DateTo` + ) + VALUES ( + OLD.AutoRoleId, OLD.ServerId, OLD.DiscordChannelId, OLD.DiscordMessageId, 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/clients.sql b/kdb-bot/src/bot_data/migration/db_history_scripts/clients.sql new file mode 100644 index 00000000..1260a59f --- /dev/null +++ b/kdb-bot/src/bot_data/migration/db_history_scripts/clients.sql @@ -0,0 +1,57 @@ +ALTER TABLE `Clients` + MODIFY `LastModifiedAt` DATETIME(6) NULL ON UPDATE CURRENT_TIMESTAMP(6); + +ALTER TABLE `Clients` + ALTER COLUMN `CreatedAt` SET DEFAULT CURRENT_TIMESTAMP(6); + +ALTER TABLE `Clients` + ALTER COLUMN `LastModifiedAt` SET DEFAULT CURRENT_TIMESTAMP(6); + +CREATE TABLE IF NOT EXISTS `ClientsHistory` +( + `ClientId` BIGINT(20) NOT NULL, + `DiscordClientId` BIGINT(20) NOT NULL, + `SentMessageCount` BIGINT(20) NOT NULL DEFAULT 0, + `ReceivedMessageCount` BIGINT(20) NOT NULL DEFAULT 0, + `DeletedMessageCount` BIGINT(20) NOT NULL DEFAULT 0, + `ReceivedCommandsCount` BIGINT(20) NOT NULL DEFAULT 0, + `MovedUsersCount` BIGINT(20) NOT NULL DEFAULT 0, + `ServerId` BIGINT(20) DEFAULT NULL, + `Deleted` BOOL DEFAULT FALSE, + `DateFrom` DATETIME(6) NOT NULL, + `DateTo` DATETIME(6) NOT NULL +); + +DROP TRIGGER IF EXISTS `TR_ClientsUpdate`; + +CREATE TRIGGER `TR_ClientsUpdate` + AFTER UPDATE + ON `Clients` + FOR EACH ROW +BEGIN + INSERT INTO `ClientsHistory` ( + `ClientId`, `DiscordClientId`, `SentMessageCount`, `ReceivedMessageCount`, `DeletedMessageCount`, + `ReceivedCommandsCount`, `MovedUsersCount`, `ServerId`, `DateFrom`, `DateTo` + ) + VALUES ( + OLD.ClientId, OLD.DiscordClientId, OLD.SentMessageCount, OLD.ReceivedMessageCount, OLD.DeletedMessageCount, + OLD.ReceivedCommandsCount, OLD.MovedUsersCount, OLD.ServerId, OLD.LastModifiedAt, CURRENT_TIMESTAMP(6) + ); +END; + +DROP TRIGGER IF EXISTS `TR_ClientsDelete`; + +CREATE TRIGGER `TR_ClientsDelete` + AFTER DELETE + ON `Clients` + FOR EACH ROW +BEGIN + INSERT INTO `ClientsHistory` ( + `ClientId`, `DiscordClientId`, `SentMessageCount`, `ReceivedMessageCount`, `DeletedMessageCount`, + `ReceivedCommandsCount`, `MovedUsersCount`, `ServerId`, `Deleted`, `DateFrom`, `DateTo` + ) + VALUES ( + OLD.ClientId, OLD.DiscordClientId, OLD.SentMessageCount, OLD.ReceivedMessageCount, OLD.DeletedMessageCount, + OLD.ReceivedCommandsCount, OLD.MovedUsersCount, OLD.ServerId, TRUE, OLD.LastModifiedAt, CURRENT_TIMESTAMP(6) + ); +END; \ No newline at end of file diff --git a/kdb-bot/src/bot_data/model/auth_user.py b/kdb-bot/src/bot_data/model/auth_user.py index 9255bede..47bdf41a 100644 --- a/kdb-bot/src/bot_data/model/auth_user.py +++ b/kdb-bot/src/bot_data/model/auth_user.py @@ -220,9 +220,7 @@ class AuthUser(TableABC): `ForgotPasswordId`, `OAuthId`, `RefreshTokenExpiryTime`, - `AuthRole`, - `CreatedAt`, - `LastModifiedAt` + `AuthRole` ) VALUES ( {self._auth_user_id}, '{self._first_name}', @@ -235,9 +233,7 @@ class AuthUser(TableABC): {"NULL" if self._forgot_password_id is None else f"'{self._forgot_password_id}'"}, {"NULL" if self._oauth_id is None else f"'{self._oauth_id}'"}, '{self._refresh_token_expire_time.isoformat()}', - {self._auth_role_id.value}, - '{self._created_at}', - '{self._modified_at}' + {self._auth_role_id.value} ) """ ) @@ -257,8 +253,7 @@ class AuthUser(TableABC): `ForgotPasswordId` = {"NULL" if self._forgot_password_id is None else f"'{self._forgot_password_id}'"}, `OAuthId` = {"NULL" if self._oauth_id is None else f"'{self._oauth_id}'"}, `RefreshTokenExpiryTime` = '{self._refresh_token_expire_time.isoformat()}', - `AuthRole` = {self._auth_role_id.value}, - `LastModifiedAt` = '{self._modified_at}' + `AuthRole` = {self._auth_role_id.value} WHERE `AuthUsers`.`Id` = {self._auth_user_id}; """ ) diff --git a/kdb-bot/src/bot_data/model/auth_user_users_relation.py b/kdb-bot/src/bot_data/model/auth_user_users_relation.py index 6d5f7f30..23b9ceab 100644 --- a/kdb-bot/src/bot_data/model/auth_user_users_relation.py +++ b/kdb-bot/src/bot_data/model/auth_user_users_relation.py @@ -68,12 +68,10 @@ class AuthUserUsersRelation(TableABC): return str( f""" INSERT INTO `AuthUserUsersRelations` ( - `AuthUserId`, `UserId`, `CreatedAt`, `LastModifiedAt` + `AuthUserId`, `UserId` ) VALUES ( {self._auth_user.id}, - {self._user.id}, - '{self._created_at}', - '{self._modified_at}' + {self._user.id} ); """ ) @@ -83,9 +81,8 @@ class AuthUserUsersRelation(TableABC): return str( f""" UPDATE `AuthUserUsersRelations` - SET `AuthUserId` = '{self._auth_user.id}',, + SET `AuthUserId` = '{self._auth_user.id}', `UserId` = '{self._user.id}' - `LastModifiedAt` = '{self._modified_at}' WHERE `AuthUserId` = {self._auth_user.id} AND `UserId` = {self._user.id}; """ diff --git a/kdb-bot/src/bot_data/model/auto_role.py b/kdb-bot/src/bot_data/model/auto_role.py index 28ad489d..a1ff7934 100644 --- a/kdb-bot/src/bot_data/model/auto_role.py +++ b/kdb-bot/src/bot_data/model/auto_role.py @@ -97,13 +97,11 @@ class AutoRole(TableABC): return str( f""" INSERT INTO `AutoRoles` ( - `ServerId`, `DiscordChannelId`, `DiscordMessageId`, `CreatedAt`, `LastModifiedAt` + `ServerId`, `DiscordChannelId`, `DiscordMessageId` ) VALUES ( {self._server.id}, {self._discord_channel_id}, - {self._discord_message_id}, - '{self._created_at}', - '{self._modified_at}' + {self._discord_message_id} ); """ ) @@ -115,8 +113,7 @@ class AutoRole(TableABC): UPDATE `AutoRoles` SET `ServerId` = {self._server.id}, `DiscordChannelId` = {self._discord_channel_id}, - `DiscordMessageId` = {self._discord_message_id}, - `LastModifiedAt` = '{self._modified_at}' + `DiscordMessageId` = {self._discord_message_id} WHERE `AutoRoleId` = {self._auto_role_id}; """ ) diff --git a/kdb-bot/src/bot_data/model/auto_role_rule.py b/kdb-bot/src/bot_data/model/auto_role_rule.py index 34188caa..8729b6ec 100644 --- a/kdb-bot/src/bot_data/model/auto_role_rule.py +++ b/kdb-bot/src/bot_data/model/auto_role_rule.py @@ -87,13 +87,11 @@ class AutoRoleRule(TableABC): return str( f""" INSERT INTO `AutoRoleRules` ( - `AutoRoleId`, `DiscordEmojiName`, `DiscordRoleId`, `CreatedAt`, `LastModifiedAt` + `AutoRoleId`, `DiscordEmojiName`, `DiscordRoleId` ) VALUES ( {self._auto_role.id}, '{self._discord_emoji_name}', - {self._discord_role_id}, - '{self._created_at}', - '{self._modified_at}' + {self._discord_role_id} ); """ ) @@ -105,8 +103,7 @@ class AutoRoleRule(TableABC): UPDATE `AutoRoleRules` SET `AutoRoleId` = {self._auto_role.id}, `DiscordEmojiName` = '{self._discord_emoji_name}', - `DiscordRoleId` = {self._discord_role_id}, - `LastModifiedAt` = '{self._modified_at}' + `DiscordRoleId` = {self._discord_role_id} WHERE `AutoRoleRuleId` = {self._auto_role_rule_id}; """ ) diff --git a/kdb-bot/src/bot_data/model/client.py b/kdb-bot/src/bot_data/model/client.py index 79f5c0fe..e69b4bab 100644 --- a/kdb-bot/src/bot_data/model/client.py +++ b/kdb-bot/src/bot_data/model/client.py @@ -153,8 +153,6 @@ class Client(TableABC): `ReceivedCommandsCount`, `MovedUsersCount`, `ServerId`, - `CreatedAt`, - `LastModifiedAt` ) VALUES ( {self._discord_client_id}, {self._sent_message_count}, @@ -162,9 +160,7 @@ class Client(TableABC): {self._deleted_message_count}, {self._received_message_count}, {self._moved_users_count}, - {self._server.id}, - '{self._created_at}', - '{self._modified_at}' + {self._server.id} ); """ )