Improved data model #378

This commit is contained in:
Sven Heidemann 2023-09-28 09:32:09 +02:00
parent a303108da2
commit 0b767fcb68
4 changed files with 28 additions and 7 deletions

View File

@ -3,6 +3,7 @@ CREATE TABLE IF NOT EXISTS `ShortRoleNamesHistory`
`Id` BIGINT(20) NOT NULL, `Id` BIGINT(20) NOT NULL,
`ShortName` VARCHAR(64) DEFAULT 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, `ServerId` BIGINT(20) DEFAULT NULL,
`Deleted` BOOL DEFAULT FALSE, `Deleted` BOOL DEFAULT FALSE,
`DateFrom` DATETIME(6) NOT NULL, `DateFrom` DATETIME(6) NOT NULL,
@ -16,8 +17,10 @@ CREATE TRIGGER `TR_ShortRoleNamesUpdate`
ON `ShortRoleNames` ON `ShortRoleNames`
FOR EACH ROW FOR EACH ROW
BEGIN BEGIN
INSERT INTO `ShortRoleNamesHistory` (`Id`, `ShortName`, `DiscordRoleId`, `ServerId`, `DateFrom`, `DateTo`) INSERT INTO `ShortRoleNamesHistory` (`Id`, `ShortName`, `DiscordRoleId`, `Position`, `ServerId`, `DateFrom`,
VALUES (OLD.Id, OLD.ShortName, OLD.DiscordRoleId, OLD.ServerId, OLD.LastModifiedAt, CURRENT_TIMESTAMP(6)); `DateTo`)
VALUES (OLD.Id, OLD.ShortName, OLD.DiscordRoleId, OLD.Position, OLD.ServerId, OLD.LastModifiedAt,
CURRENT_TIMESTAMP(6));
END; END;
DROP TRIGGER IF EXISTS `TR_ShortRoleNamesDelete`; DROP TRIGGER IF EXISTS `TR_ShortRoleNamesDelete`;
@ -29,6 +32,6 @@ CREATE TRIGGER `TR_ShortRoleNamesDelete`
BEGIN BEGIN
INSERT INTO `ShortRoleNamesHistory` (`Id`, `ShortName`, `DiscordRoleId`, `ServerId`, `Deleted`, `DateFrom`, INSERT INTO `ShortRoleNamesHistory` (`Id`, `ShortName`, `DiscordRoleId`, `ServerId`, `Deleted`, `DateFrom`,
`DateTo`) `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)); CURRENT_TIMESTAMP(6));
END; END;

View File

@ -22,6 +22,7 @@ class ShortRoleNameMigration(MigrationABC):
`Id` BIGINT NOT NULL AUTO_INCREMENT, `Id` BIGINT NOT NULL AUTO_INCREMENT,
`ShortName` VARCHAR(255) NOT NULL, `ShortName` VARCHAR(255) NOT NULL,
`DiscordRoleId` BIGINT NOT NULL, `DiscordRoleId` BIGINT NOT NULL,
`Position` ENUM('Before', 'After') NOT NULL,
`ServerId` BIGINT, `ServerId` BIGINT,
`CreatedAt` DATETIME(6) NULL DEFAULT CURRENT_TIMESTAMP(6), `CreatedAt` DATETIME(6) NULL DEFAULT CURRENT_TIMESTAMP(6),
`LastModifiedAt` DATETIME(6) NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), `LastModifiedAt` DATETIME(6) NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),

View File

@ -5,6 +5,7 @@ from cpl_core.dependency_injection import ServiceProviderABC
from cpl_discord.service import DiscordBotServiceABC from cpl_discord.service import DiscordBotServiceABC
from bot_data.model.server import Server from bot_data.model.server import Server
from bot_data.model.short_role_name_position_enum import ShortRoleNamePositionEnum
class ShortRoleName(TableABC): class ShortRoleName(TableABC):
@ -12,6 +13,7 @@ class ShortRoleName(TableABC):
self, self,
name: str, name: str,
discord_role_id: int, discord_role_id: int,
position: ShortRoleNamePositionEnum,
server: Server, server: Server,
created_at: datetime = None, created_at: datetime = None,
modified_at: datetime = None, modified_at: datetime = None,
@ -20,6 +22,7 @@ class ShortRoleName(TableABC):
self._id = id self._id = id
self._name = name self._name = name
self._discord_role_id = discord_role_id self._discord_role_id = discord_role_id
self._position = position
self._server = server self._server = server
TableABC.__init__(self) TableABC.__init__(self)
@ -52,6 +55,14 @@ class ShortRoleName(TableABC):
guild = bot.get_guild(self._server.discord_id) guild = bot.get_guild(self._server.discord_id)
return guild.get_role(self.role_id).name 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 @property
def server(self) -> Server: def server(self) -> Server:
return self._server return self._server

View File

@ -0,0 +1,6 @@
from enum import Enum
class ShortRoleNamePositionEnum(Enum):
before = "Before"
after = "After"