From 05a2ea9b1885cd9df50c348d093dee3775bfd60e Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Wed, 19 Jul 2023 10:49:37 +0200 Subject: [PATCH] Added migration for config in wi #127 --- kdb-bot/.cpl/schematic_migration.py | 55 +++++++++++++++++++ kdb-bot/.cpl/schematic_query.py | 2 +- .../src/bot/startup_migration_extension.py | 2 + .../configuration/feature_flags_enum.py | 1 + .../configuration/feature_flags_settings.py | 1 + .../bot_data/migration/config_migration.py | 33 +++++++++++ 6 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 kdb-bot/.cpl/schematic_migration.py create mode 100644 kdb-bot/src/bot_data/migration/config_migration.py diff --git a/kdb-bot/.cpl/schematic_migration.py b/kdb-bot/.cpl/schematic_migration.py new file mode 100644 index 00000000..16c0fe5f --- /dev/null +++ b/kdb-bot/.cpl/schematic_migration.py @@ -0,0 +1,55 @@ +from cpl_cli.abc.generate_schematic_abc import GenerateSchematicABC + + +class Migration(GenerateSchematicABC): + def __init__(self, *args: str): + GenerateSchematicABC.__init__(self, *args) + + def get_code(self) -> str: + import textwrap + + code = textwrap.dedent( + """\ + from bot_core.logging.database_logger import DatabaseLogger + from bot_data.abc.migration_abc import MigrationABC + from bot_data.db_context import DBContext + + + class $ClassName(MigrationABC): + name = "1.0_$ClassName" + + 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 `$TableName` ( + `Id` BIGINT NOT NULL AUTO_INCREMENT, + `CreatedAt` DATETIME(6), + `LastModifiedAt` DATETIME(6), + PRIMARY KEY(`Id`) + ); + \""" + ) + ) + + def downgrade(self): + self._cursor.execute("DROP TABLE `$TableName`;") + """ + ) + return self.build_code_str( + code, + ClassName=self._class_name, + TableName=self._class_name.split("Migration")[0], + ) + + @classmethod + def register(cls): + GenerateSchematicABC.register(cls, "migration", []) diff --git a/kdb-bot/.cpl/schematic_query.py b/kdb-bot/.cpl/schematic_query.py index e34a3b6e..767cd77d 100644 --- a/kdb-bot/.cpl/schematic_query.py +++ b/kdb-bot/.cpl/schematic_query.py @@ -1,7 +1,7 @@ from cpl_cli.abc.generate_schematic_abc import GenerateSchematicABC -class query(GenerateSchematicABC): +class Query(GenerateSchematicABC): def __init__(self, *args: str): GenerateSchematicABC.__init__(self, *args) diff --git a/kdb-bot/src/bot/startup_migration_extension.py b/kdb-bot/src/bot/startup_migration_extension.py index a2d639fa..796b8257 100644 --- a/kdb-bot/src/bot/startup_migration_extension.py +++ b/kdb-bot/src/bot/startup_migration_extension.py @@ -9,6 +9,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.config_migration import ConfigMigration 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 @@ -44,3 +45,4 @@ class StartupMigrationExtension(StartupExtensionABC): services.add_transient(MigrationABC, UserWarningMigration) # 21.02.2023 #35 - 1.0.0 services.add_transient(MigrationABC, DBHistoryMigration) # 06.03.2023 #246 - 1.0.0 services.add_transient(MigrationABC, AchievementsMigration) # 14.06.2023 #268 - 1.1.0 + services.add_transient(MigrationABC, ConfigMigration) # 19.07.2023 #127 - 1.1.0 diff --git a/kdb-bot/src/bot_core/configuration/feature_flags_enum.py b/kdb-bot/src/bot_core/configuration/feature_flags_enum.py index 37a579fe..c2a0cf6c 100644 --- a/kdb-bot/src/bot_core/configuration/feature_flags_enum.py +++ b/kdb-bot/src/bot_core/configuration/feature_flags_enum.py @@ -20,3 +20,4 @@ class FeatureFlagsEnum(Enum): api_only = "ApiOnly" presence = "Presence" version_in_presence = "VersionInPresence" + config_in_wi = "ConfigInWI" diff --git a/kdb-bot/src/bot_core/configuration/feature_flags_settings.py b/kdb-bot/src/bot_core/configuration/feature_flags_settings.py index b0d297b8..469532bc 100644 --- a/kdb-bot/src/bot_core/configuration/feature_flags_settings.py +++ b/kdb-bot/src/bot_core/configuration/feature_flags_settings.py @@ -28,6 +28,7 @@ class FeatureFlagsSettings(ConfigurationModelABC): FeatureFlagsEnum.api_only.value: False, # 13.10.2022 #70 FeatureFlagsEnum.presence.value: True, # 03.10.2022 #56 FeatureFlagsEnum.version_in_presence.value: False, # 21.03.2023 #253 + FeatureFlagsEnum.config_in_wi.value: True, # 19.07.2023 #127 } def get_flag(self, key: FeatureFlagsEnum) -> bool: diff --git a/kdb-bot/src/bot_data/migration/config_migration.py b/kdb-bot/src/bot_data/migration/config_migration.py new file mode 100644 index 00000000..abdb6e5b --- /dev/null +++ b/kdb-bot/src/bot_data/migration/config_migration.py @@ -0,0 +1,33 @@ +from bot_core.logging.database_logger import DatabaseLogger +from bot_data.abc.migration_abc import MigrationABC +from bot_data.db_context import DBContext + + +class ConfigMigration(MigrationABC): + name = "1.1.0_ConfigMigration" + + 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 `Config` ( + # `Id` BIGINT NOT NULL AUTO_INCREMENT, + # `CreatedAt` DATETIME(6), + # `LastModifiedAt` DATETIME(6), + # PRIMARY KEY(`Id`) + # ); + # """ + # ) + # ) + + def downgrade(self): + self._logger.debug(__name__, "Running downgrade") + # self._cursor.execute("DROP TABLE `Config`;")