From 0b767fcb6863323ee28f222b5df49efb6d58637b Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Thu, 28 Sep 2023 09:32:09 +0200 Subject: [PATCH] Improved data model #378 --- .../db_history_scripts/short_rule_names.sql | 17 ++++++++++------- .../migration/short_role_name_migration.py | 1 + kdb-bot/src/bot_data/model/short_role_name.py | 11 +++++++++++ .../model/short_role_name_position_enum.py | 6 ++++++ 4 files changed, 28 insertions(+), 7 deletions(-) create mode 100644 kdb-bot/src/bot_data/model/short_role_name_position_enum.py 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 index fb64f239..06168a34 100644 --- 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 @@ -1,12 +1,13 @@ CREATE TABLE IF NOT EXISTS `ShortRoleNamesHistory` ( - `Id` BIGINT(20) NOT NULL, + `Id` BIGINT(20) NOT NULL, `ShortName` VARCHAR(64) DEFAULT NULL, - `DiscordRoleId` BIGINT(20) NOT NULL, + `DiscordRoleId` BIGINT(20) NOT NULL, + `Position` ENUM ('Before', 'After') NOT NULL, `ServerId` BIGINT(20) DEFAULT NULL, `Deleted` BOOL DEFAULT FALSE, - `DateFrom` DATETIME(6) NOT NULL, - `DateTo` DATETIME(6) NOT NULL + `DateFrom` DATETIME(6) NOT NULL, + `DateTo` DATETIME(6) NOT NULL ); DROP TRIGGER IF EXISTS `TR_ShortRoleNamesUpdate`; @@ -16,8 +17,10 @@ CREATE TRIGGER `TR_ShortRoleNamesUpdate` ON `ShortRoleNames` FOR EACH ROW BEGIN - INSERT INTO `ShortRoleNamesHistory` (`Id`, `ShortName`, `DiscordRoleId`, `ServerId`, `DateFrom`, `DateTo`) - VALUES (OLD.Id, OLD.ShortName, OLD.DiscordRoleId, OLD.ServerId, OLD.LastModifiedAt, CURRENT_TIMESTAMP(6)); + INSERT INTO `ShortRoleNamesHistory` (`Id`, `ShortName`, `DiscordRoleId`, `Position`, `ServerId`, `DateFrom`, + `DateTo`) + VALUES (OLD.Id, OLD.ShortName, OLD.DiscordRoleId, OLD.Position, OLD.ServerId, OLD.LastModifiedAt, + CURRENT_TIMESTAMP(6)); END; DROP TRIGGER IF EXISTS `TR_ShortRoleNamesDelete`; @@ -29,6 +32,6 @@ CREATE TRIGGER `TR_ShortRoleNamesDelete` BEGIN INSERT INTO `ShortRoleNamesHistory` (`Id`, `ShortName`, `DiscordRoleId`, `ServerId`, `Deleted`, `DateFrom`, `DateTo`) - VALUES (OLD.Id, OLD.ShortName, OLD.DiscordRoleId, OLD.ServerId, TRUE, OLD.LastModifiedAt, + VALUES (OLD.Id, OLD.ShortName, OLD.DiscordRoleId, OLD.Position, OLD.ServerId, 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 index 7d7d88ca..0d4ae6ff 100644 --- a/kdb-bot/src/bot_data/migration/short_role_name_migration.py +++ b/kdb-bot/src/bot_data/migration/short_role_name_migration.py @@ -22,6 +22,7 @@ class ShortRoleNameMigration(MigrationABC): `Id` BIGINT NOT NULL AUTO_INCREMENT, `ShortName` VARCHAR(255) NOT NULL, `DiscordRoleId` BIGINT NOT NULL, + `Position` ENUM('Before', 'After') 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), diff --git a/kdb-bot/src/bot_data/model/short_role_name.py b/kdb-bot/src/bot_data/model/short_role_name.py index 52f68e75..fab3c711 100644 --- a/kdb-bot/src/bot_data/model/short_role_name.py +++ b/kdb-bot/src/bot_data/model/short_role_name.py @@ -5,6 +5,7 @@ from cpl_core.dependency_injection import ServiceProviderABC from cpl_discord.service import DiscordBotServiceABC from bot_data.model.server import Server +from bot_data.model.short_role_name_position_enum import ShortRoleNamePositionEnum class ShortRoleName(TableABC): @@ -12,6 +13,7 @@ class ShortRoleName(TableABC): self, name: str, discord_role_id: int, + position: ShortRoleNamePositionEnum, server: Server, created_at: datetime = None, modified_at: datetime = None, @@ -20,6 +22,7 @@ class ShortRoleName(TableABC): self._id = id self._name = name self._discord_role_id = discord_role_id + self._position = position self._server = server TableABC.__init__(self) @@ -52,6 +55,14 @@ class ShortRoleName(TableABC): guild = bot.get_guild(self._server.discord_id) return guild.get_role(self.role_id).name + @property + def position(self) -> ShortRoleNamePositionEnum: + return self._position + + @position.setter + def position(self, value: ShortRoleNamePositionEnum): + self._position = value + @property def server(self) -> Server: return self._server diff --git a/kdb-bot/src/bot_data/model/short_role_name_position_enum.py b/kdb-bot/src/bot_data/model/short_role_name_position_enum.py new file mode 100644 index 00000000..1dfca2f8 --- /dev/null +++ b/kdb-bot/src/bot_data/model/short_role_name_position_enum.py @@ -0,0 +1,6 @@ +from enum import Enum + + +class ShortRoleNamePositionEnum(Enum): + before = "Before" + after = "After"