From e018fdcbdf4bafafe2af8712726c122d56b4e94f Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Fri, 10 Nov 2023 23:34:07 +0100 Subject: [PATCH] Updated and added script order #428 --- .../bot_data/scripts/0.1.0/Initial_down.sql | 12 ---- bot/src/bot_data/scripts/0.1.0/Initial_up.sql | 70 ------------------- .../bot_data/scripts/0.2.2/AutoRole_down.sql | 4 -- .../bot_data/scripts/0.2.2/AutoRole_up.sql | 24 ------- bot/src/bot_data/scripts/0.3.0/Api_down.sql | 4 -- bot/src/bot_data/scripts/0.3.0/Api_up.sql | 31 -------- .../scripts/0.3.0/AutoRoleFix_down.sql | 4 -- .../bot_data/scripts/0.3.0/AutoRoleFix_up.sql | 4 -- bot/src/bot_data/scripts/0.3.0/Level_down.sql | 2 - bot/src/bot_data/scripts/0.3.0/Level_up.sql | 14 ---- bot/src/bot_data/scripts/0.3.0/Stats_down.sql | 2 - bot/src/bot_data/scripts/0.3.0/Stats_up.sql | 13 ---- .../0.3.1/UserMessageCountPerHour_down.sql | 2 - .../0.3.1/UserMessageCountPerHour_up.sql | 13 ---- .../mock/migration_service.py | 30 +++++--- .../migration_to_sql/mock/mock_db_context.py | 21 +++++- 16 files changed, 40 insertions(+), 210 deletions(-) delete mode 100644 bot/src/bot_data/scripts/0.1.0/Initial_down.sql delete mode 100644 bot/src/bot_data/scripts/0.1.0/Initial_up.sql delete mode 100644 bot/src/bot_data/scripts/0.2.2/AutoRole_down.sql delete mode 100644 bot/src/bot_data/scripts/0.2.2/AutoRole_up.sql delete mode 100644 bot/src/bot_data/scripts/0.3.0/Api_down.sql delete mode 100644 bot/src/bot_data/scripts/0.3.0/Api_up.sql delete mode 100644 bot/src/bot_data/scripts/0.3.0/AutoRoleFix_down.sql delete mode 100644 bot/src/bot_data/scripts/0.3.0/AutoRoleFix_up.sql delete mode 100644 bot/src/bot_data/scripts/0.3.0/Level_down.sql delete mode 100644 bot/src/bot_data/scripts/0.3.0/Level_up.sql delete mode 100644 bot/src/bot_data/scripts/0.3.0/Stats_down.sql delete mode 100644 bot/src/bot_data/scripts/0.3.0/Stats_up.sql delete mode 100644 bot/src/bot_data/scripts/0.3.1/UserMessageCountPerHour_down.sql delete mode 100644 bot/src/bot_data/scripts/0.3.1/UserMessageCountPerHour_up.sql diff --git a/bot/src/bot_data/scripts/0.1.0/Initial_down.sql b/bot/src/bot_data/scripts/0.1.0/Initial_down.sql deleted file mode 100644 index c1c80bab..00000000 --- a/bot/src/bot_data/scripts/0.1.0/Initial_down.sql +++ /dev/null @@ -1,12 +0,0 @@ -DROP TABLE `Servers`; - -DROP TABLE `Users`; - -DROP TABLE `Clients`; - -DROP TABLE `KnownUsers`; - -DROP TABLE `UserJoinedServers`; - -DROP TABLE `UserJoinedVoiceChannel`; - diff --git a/bot/src/bot_data/scripts/0.1.0/Initial_up.sql b/bot/src/bot_data/scripts/0.1.0/Initial_up.sql deleted file mode 100644 index 7c5b0be2..00000000 --- a/bot/src/bot_data/scripts/0.1.0/Initial_up.sql +++ /dev/null @@ -1,70 +0,0 @@ -CREATE TABLE IF NOT EXISTS `Servers` -( - `ServerId` BIGINT NOT NULL AUTO_INCREMENT, - `DiscordServerId` BIGINT NOT NULL, - `CreatedAt` DATETIME(6), - `LastModifiedAt` DATETIME(6), - PRIMARY KEY (`ServerId`) -); - -CREATE TABLE IF NOT EXISTS `Users` -( - `UserId` BIGINT NOT NULL AUTO_INCREMENT, - `DiscordId` BIGINT NOT NULL, - `XP` BIGINT NOT NULL DEFAULT 0, - `ServerId` BIGINT, - `CreatedAt` DATETIME(6), - `LastModifiedAt` DATETIME(6), - FOREIGN KEY (`ServerId`) REFERENCES Servers (`ServerId`), - PRIMARY KEY (`UserId`) -); - -CREATE TABLE IF NOT EXISTS `Clients` -( - `ClientId` BIGINT NOT NULL AUTO_INCREMENT, - `DiscordClientId` BIGINT NOT NULL, - `SentMessageCount` BIGINT NOT NULL DEFAULT 0, - `ReceivedMessageCount` BIGINT NOT NULL DEFAULT 0, - `DeletedMessageCount` BIGINT NOT NULL DEFAULT 0, - `ReceivedCommandsCount` BIGINT NOT NULL DEFAULT 0, - `MovedUsersCount` BIGINT NOT NULL DEFAULT 0, - `ServerId` BIGINT, - `CreatedAt` DATETIME(6), - `LastModifiedAt` DATETIME(6), - FOREIGN KEY (`ServerId`) REFERENCES Servers (`ServerId`), - PRIMARY KEY (`ClientId`) -); - -CREATE TABLE IF NOT EXISTS `KnownUsers` -( - `KnownUserId` BIGINT NOT NULL AUTO_INCREMENT, - `DiscordId` BIGINT NOT NULL, - `CreatedAt` DATETIME(6), - `LastModifiedAt` DATETIME(6), - PRIMARY KEY (`KnownUserId`) -); - -CREATE TABLE IF NOT EXISTS `UserJoinedServers` -( - `JoinId` BIGINT NOT NULL AUTO_INCREMENT, - `UserId` BIGINT NOT NULL, - `JoinedOn` DATETIME(6) NOT NULL, - `LeavedOn` DATETIME(6), - `CreatedAt` DATETIME(6), - `LastModifiedAt` DATETIME(6), - FOREIGN KEY (`UserId`) REFERENCES Users (`UserId`), - PRIMARY KEY (`JoinId`) -); - -CREATE TABLE IF NOT EXISTS `UserJoinedVoiceChannel` -( - `JoinId` BIGINT NOT NULL AUTO_INCREMENT, - `UserId` BIGINT NOT NULL, - `DiscordChannelId` BIGINT NOT NULL, - `JoinedOn` DATETIME(6) NOT NULL, - `LeavedOn` DATETIME(6), - `CreatedAt` DATETIME(6), - `LastModifiedAt` DATETIME(6), - FOREIGN KEY (`UserId`) REFERENCES Users (`UserId`), - PRIMARY KEY (`JoinId`) -); diff --git a/bot/src/bot_data/scripts/0.2.2/AutoRole_down.sql b/bot/src/bot_data/scripts/0.2.2/AutoRole_down.sql deleted file mode 100644 index c211f43c..00000000 --- a/bot/src/bot_data/scripts/0.2.2/AutoRole_down.sql +++ /dev/null @@ -1,4 +0,0 @@ -DROP TABLE `AutoRoles`; - -DROP TABLE `AutoRoleRules`; - diff --git a/bot/src/bot_data/scripts/0.2.2/AutoRole_up.sql b/bot/src/bot_data/scripts/0.2.2/AutoRole_up.sql deleted file mode 100644 index 4f132be9..00000000 --- a/bot/src/bot_data/scripts/0.2.2/AutoRole_up.sql +++ /dev/null @@ -1,24 +0,0 @@ -CREATE TABLE IF NOT EXISTS `AutoRoles` -( - `AutoRoleId` BIGINT NOT NULL AUTO_INCREMENT, - `ServerId` BIGINT, - `DiscordMessageId` BIGINT NOT NULL, - `CreatedAt` DATETIME(6), - `LastModifiedAt` DATETIME(6), - PRIMARY KEY (`AutoRoleId`), - FOREIGN KEY (`ServerId`) REFERENCES `Servers` (`ServerId`) -); - -CREATE TABLE IF NOT EXISTS `AutoRoleRules` -( - `AutoRoleRuleId` BIGINT NOT NULL AUTO_INCREMENT, - `AutoRoleId` BIGINT, - `DiscordEmojiName` VARCHAR(64), - `DiscordRoleId` BIGINT NOT NULL, - `CreatedAt` DATETIME(6), - `LastModifiedAt` DATETIME(6), - PRIMARY KEY (`AutoRoleRuleId`), - FOREIGN KEY (`AutoRoleId`) REFERENCES `AutoRoles` (`AutoRoleId`) -); - - diff --git a/bot/src/bot_data/scripts/0.3.0/Api_down.sql b/bot/src/bot_data/scripts/0.3.0/Api_down.sql deleted file mode 100644 index 49473912..00000000 --- a/bot/src/bot_data/scripts/0.3.0/Api_down.sql +++ /dev/null @@ -1,4 +0,0 @@ -DROP TABLE `AuthUsers`; - -DROP TABLE `AuthUserUsersRelations`; - diff --git a/bot/src/bot_data/scripts/0.3.0/Api_up.sql b/bot/src/bot_data/scripts/0.3.0/Api_up.sql deleted file mode 100644 index 20708881..00000000 --- a/bot/src/bot_data/scripts/0.3.0/Api_up.sql +++ /dev/null @@ -1,31 +0,0 @@ - -CREATE TABLE IF NOT EXISTS `AuthUsers` ( - `Id` BIGINT NOT NULL AUTO_INCREMENT, - `FirstName` VARCHAR(255), - `LastName` VARCHAR(255), - `EMail` VARCHAR(255), - `Password` VARCHAR(255), - `PasswordSalt` VARCHAR(255), - `RefreshToken` VARCHAR(255), - `ConfirmationId` VARCHAR(255) DEFAULT NULL, - `ForgotPasswordId` VARCHAR(255) DEFAULT NULL, - `OAuthId` VARCHAR(255) DEFAULT NULL, - `RefreshTokenExpiryTime` DATETIME(6) NOT NULL, - `AuthRole` INT NOT NULL DEFAULT 0, - `CreatedAt` DATETIME(6) NOT NULL, - `LastModifiedAt` DATETIME(6) NOT NULL, - PRIMARY KEY(`Id`) -); - -CREATE TABLE IF NOT EXISTS `AuthUserUsersRelations`( - `Id` BIGINT NOT NULL AUTO_INCREMENT, - `AuthUserId` BIGINT DEFAULT NULL, - `UserId` BIGINT DEFAULT NULL, - `CreatedAt` DATETIME(6) NOT NULL, - `LastModifiedAt` DATETIME(6) NOT NULL, - PRIMARY KEY(`Id`), - FOREIGN KEY (`AuthUserId`) REFERENCES `AuthUsers`(`Id`), - FOREIGN KEY (`UserId`) REFERENCES `Users`(`UserId`) -); - - diff --git a/bot/src/bot_data/scripts/0.3.0/AutoRoleFix_down.sql b/bot/src/bot_data/scripts/0.3.0/AutoRoleFix_down.sql deleted file mode 100644 index 60e77292..00000000 --- a/bot/src/bot_data/scripts/0.3.0/AutoRoleFix_down.sql +++ /dev/null @@ -1,4 +0,0 @@ - -ALTER TABLE AutoRoles DROP COLUMN DiscordChannelId; - - diff --git a/bot/src/bot_data/scripts/0.3.0/AutoRoleFix_up.sql b/bot/src/bot_data/scripts/0.3.0/AutoRoleFix_up.sql deleted file mode 100644 index f025af10..00000000 --- a/bot/src/bot_data/scripts/0.3.0/AutoRoleFix_up.sql +++ /dev/null @@ -1,4 +0,0 @@ - -ALTER TABLE AutoRoles ADD DiscordChannelId BIGINT NOT NULL AFTER ServerId; - - diff --git a/bot/src/bot_data/scripts/0.3.0/Level_down.sql b/bot/src/bot_data/scripts/0.3.0/Level_down.sql deleted file mode 100644 index 301478b2..00000000 --- a/bot/src/bot_data/scripts/0.3.0/Level_down.sql +++ /dev/null @@ -1,2 +0,0 @@ -DROP TABLE `Levels`; - diff --git a/bot/src/bot_data/scripts/0.3.0/Level_up.sql b/bot/src/bot_data/scripts/0.3.0/Level_up.sql deleted file mode 100644 index e982e7b3..00000000 --- a/bot/src/bot_data/scripts/0.3.0/Level_up.sql +++ /dev/null @@ -1,14 +0,0 @@ -CREATE TABLE IF NOT EXISTS `Levels` ( - `Id` BIGINT NOT NULL AUTO_INCREMENT, - `Name` VARCHAR(255) NOT NULL, - `Color` VARCHAR(8) NOT NULL, - `MinXp` BIGINT NOT NULL, - `PermissionInt` BIGINT NOT NULL, - `ServerId` BIGINT, - `CreatedAt` DATETIME(6), - `LastModifiedAt` DATETIME(6), - PRIMARY KEY(`Id`), - FOREIGN KEY (`ServerId`) REFERENCES `Servers`(`ServerId`) -); - - diff --git a/bot/src/bot_data/scripts/0.3.0/Stats_down.sql b/bot/src/bot_data/scripts/0.3.0/Stats_down.sql deleted file mode 100644 index 36efa156..00000000 --- a/bot/src/bot_data/scripts/0.3.0/Stats_down.sql +++ /dev/null @@ -1,2 +0,0 @@ -DROP TABLE `Statistics`; - diff --git a/bot/src/bot_data/scripts/0.3.0/Stats_up.sql b/bot/src/bot_data/scripts/0.3.0/Stats_up.sql deleted file mode 100644 index 5f3bafa7..00000000 --- a/bot/src/bot_data/scripts/0.3.0/Stats_up.sql +++ /dev/null @@ -1,13 +0,0 @@ -CREATE TABLE IF NOT EXISTS `Statistics` ( - `Id` BIGINT NOT NULL AUTO_INCREMENT, - `Name` VARCHAR(255) NOT NULL, - `Description` VARCHAR(255) NOT NULL, - `Code` LONGTEXT NOT NULL, - `ServerId` BIGINT, - `CreatedAt` DATETIME(6), - `LastModifiedAt` DATETIME(6), - PRIMARY KEY(`Id`), - FOREIGN KEY (`ServerId`) REFERENCES `Servers`(`ServerId`) -); - - diff --git a/bot/src/bot_data/scripts/0.3.1/UserMessageCountPerHour_down.sql b/bot/src/bot_data/scripts/0.3.1/UserMessageCountPerHour_down.sql deleted file mode 100644 index 3de96768..00000000 --- a/bot/src/bot_data/scripts/0.3.1/UserMessageCountPerHour_down.sql +++ /dev/null @@ -1,2 +0,0 @@ -DROP TABLE `UserMessageCountPerHour`; - diff --git a/bot/src/bot_data/scripts/0.3.1/UserMessageCountPerHour_up.sql b/bot/src/bot_data/scripts/0.3.1/UserMessageCountPerHour_up.sql deleted file mode 100644 index 024bed4b..00000000 --- a/bot/src/bot_data/scripts/0.3.1/UserMessageCountPerHour_up.sql +++ /dev/null @@ -1,13 +0,0 @@ -CREATE TABLE IF NOT EXISTS `UserMessageCountPerHour` ( - `Id` BIGINT NOT NULL AUTO_INCREMENT, - `Date` DATETIME(6) NOT NULL, - `Hour` BIGINT, - `XPCount` BIGINT, - `UserId` BIGINT, - `CreatedAt` DATETIME(6), - `LastModifiedAt` DATETIME(6), - PRIMARY KEY(`Id`), - FOREIGN KEY (`UserId`) REFERENCES `Users`(`UserId`) -); - - diff --git a/bot/tools/migration_to_sql/mock/migration_service.py b/bot/tools/migration_to_sql/mock/migration_service.py index 9843f15e..84d6d7e6 100644 --- a/bot/tools/migration_to_sql/mock/migration_service.py +++ b/bot/tools/migration_to_sql/mock/migration_service.py @@ -1,3 +1,6 @@ +import os +import shutil + from cpl_core.console import Console from cpl_core.dependency_injection import ServiceProviderABC from cpl_query.extension import List @@ -18,24 +21,31 @@ class MigrationService: self._db = db self._cursor = db.cursor - self._migrations: List[MigrationABC] = ( - List(type, MigrationABC.__subclasses__()).order_by(lambda x: x.name.split("_")[0]).then_by(lambda x: x.prio) - ) + # self._migrations: List[MigrationABC] = ( + # List(type, MigrationABC.__subclasses__()).order_by(lambda x: x.name.split("_")[0]).then_by(lambda x: x.prio) + # ) def migrate(self): - for migration in self._migrations: - migration_id = migration.__name__ + path = f"../../src/bot_data/scripts" + if not os.path.exists(path): + os.makedirs(path) + else: + shutil.rmtree(path) + os.makedirs(path) + + for migration in self._services.get_services(MigrationABC): + migration_id = type(migration).__name__ try: - migration_as_service: MigrationABC = self._services.get_service(migration) + # migration_as_service: MigrationABC = self._services.get_service(migration) # save upgrade scripts - self._db.set_migration(migration_as_service.name, True) - migration_as_service.upgrade() + self._db.set_migration(migration.name, True) + migration.upgrade() self._cursor.execute(MigrationHistory(migration_id).insert_string) self._db.save_changes() # save downgrade scripts - self._db.set_migration(migration_as_service.name) - migration_as_service.downgrade() + self._db.set_migration(migration.name) + migration.downgrade() self._cursor.execute(MigrationHistory(migration_id).insert_string) self._db.save_changes() diff --git a/bot/tools/migration_to_sql/mock/mock_db_context.py b/bot/tools/migration_to_sql/mock/mock_db_context.py index aa898700..c7f7c94e 100644 --- a/bot/tools/migration_to_sql/mock/mock_db_context.py +++ b/bot/tools/migration_to_sql/mock/mock_db_context.py @@ -15,6 +15,11 @@ class MockDBContext(DatabaseContextABC): self._migration_name: Optional[str] = None self._migration_script: Optional[str] = None + self._old_version: Optional[str] = None + self._old_name: Optional[str] = None + + self._index: int = 0 + @property def cursor(self) -> MockCursor: cursor = MockCursor() @@ -43,11 +48,25 @@ class MockDBContext(DatabaseContextABC): if not os.path.exists(path): os.makedirs(path) + if ( + self._old_name is not None + and self._migration_name is not None + and self._old_name.split("_")[0] == self._migration_name.split("_")[0] + ): + pass + elif self._old_version == self._migration_version: + self._index += 1 + else: + self._index = 1 + script = textwrap.dedent(self._migration_script) - with open(f"{path}/{self._migration_name}.sql", "w+") as f: + with open(f"{path}/{self._index}_{self._migration_name}.sql", "w+") as f: f.write(script) f.close() + self._old_version = self._migration_version + self._old_name = self._migration_name + self._migration_name = None self._migration_version = None self._migration_script = None