diff --git a/kdb-bot/src/bot_data/abc/migration_abc.py b/kdb-bot/src/bot_data/abc/migration_abc.py index 8d6ebb0f..67ed5e96 100644 --- a/kdb-bot/src/bot_data/abc/migration_abc.py +++ b/kdb-bot/src/bot_data/abc/migration_abc.py @@ -24,8 +24,8 @@ class MigrationABC(ABC): def downgrade(self): pass - def _exec(self, file: str): - path = f"{os.path.dirname(os.path.realpath(__file__))}/db_history_scripts" + def _exec(self, self_file: str, file: str): + path = f"{os.path.dirname(os.path.realpath(self_file))}/db_history_scripts" sql = open(f"{path}/{file}").read() for statement in sql.split("\n\n"): diff --git a/kdb-bot/src/bot_data/migration/config_migration.py b/kdb-bot/src/bot_data/migration/config_migration.py index 7ff33855..d65ac06d 100644 --- a/kdb-bot/src/bot_data/migration/config_migration.py +++ b/kdb-bot/src/bot_data/migration/config_migration.py @@ -16,12 +16,12 @@ class ConfigMigration(MigrationABC): self._server_upgrade() self._technician_upgrade() - self._exec("config/server.sql") - self._exec("config/server_afk_channels.sql") - self._exec("config/server_team_roles.sql") - self._exec("config/technician.sql") - self._exec("config/technician_ids.sql") - self._exec("config/technician_ping_urls.sql") + self._exec(__file__, "config/server.sql") + self._exec(__file__, "config/server_afk_channels.sql") + self._exec(__file__, "config/server_team_roles.sql") + self._exec(__file__, "config/technician.sql") + self._exec(__file__, "config/technician_ids.sql") + self._exec(__file__, "config/technician_ping_urls.sql") def _server_upgrade(self): self._cursor.execute( 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 e7322ad9..5452d9e5 100644 --- a/kdb-bot/src/bot_data/migration/db_history_migration.py +++ b/kdb-bot/src/bot_data/migration/db_history_migration.py @@ -1,5 +1,3 @@ -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 @@ -15,33 +13,26 @@ class DBHistoryMigration(MigrationABC): self._db = db self._cursor = db.cursor - def _exec(self, file: str): - path = f"{os.path.dirname(os.path.realpath(__file__))}/db_history_scripts" - sql = open(f"{path}/{file}").read() - - for statement in sql.split("\n\n"): - self._cursor.execute(statement + ";") - def upgrade(self): self._logger.debug(__name__, "Running upgrade") - self._exec("api_keys.sql") - self._exec("auth_users.sql") - self._exec("auth_user_users_relation.sql") - self._exec("auto_role_rules.sql") - self._exec("auto_roles.sql") - self._exec("clients.sql") - self._exec("game_servers.sql") - self._exec("known_users.sql") - self._exec("levels.sql") - self._exec("servers.sql") - self._exec("user_game_idents.sql") - self._exec("user_joined_game_servers.sql") - self._exec("user_joined_servers.sql") - self._exec("user_joined_voice_channel.sql") - self._exec("user_message_count_per_hour.sql") - self._exec("users.sql") - self._exec("user_warnings.sql") + self._exec(__file__, "api_keys.sql") + self._exec(__file__, "auth_users.sql") + self._exec(__file__, "auth_user_users_relation.sql") + self._exec(__file__, "auto_role_rules.sql") + self._exec(__file__, "auto_roles.sql") + self._exec(__file__, "clients.sql") + self._exec(__file__, "game_servers.sql") + self._exec(__file__, "known_users.sql") + self._exec(__file__, "levels.sql") + self._exec(__file__, "servers.sql") + self._exec(__file__, "user_game_idents.sql") + self._exec(__file__, "user_joined_game_servers.sql") + self._exec(__file__, "user_joined_servers.sql") + self._exec(__file__, "user_joined_voice_channel.sql") + self._exec(__file__, "user_message_count_per_hour.sql") + self._exec(__file__, "users.sql") + self._exec(__file__, "user_warnings.sql") self._logger.debug(__name__, "Finished history upgrade") 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 5f5b7f72..fb64f239 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,8 +1,9 @@ CREATE TABLE IF NOT EXISTS `ShortRoleNamesHistory` ( `Id` BIGINT(20) NOT NULL, - `Name` VARCHAR(64) DEFAULT NULL, + `ShortName` VARCHAR(64) DEFAULT NULL, `DiscordRoleId` BIGINT(20) NOT NULL, + `ServerId` BIGINT(20) DEFAULT NULL, `Deleted` BOOL DEFAULT FALSE, `DateFrom` DATETIME(6) NOT NULL, `DateTo` DATETIME(6) NOT NULL @@ -15,8 +16,8 @@ CREATE TRIGGER `TR_ShortRoleNamesUpdate` ON `ShortRoleNames` FOR EACH ROW BEGIN - INSERT INTO `ShortRoleNamesHistory` (`Id`, `Name`, `DiscordRoleId`, `DateFrom`, `DateTo`) - VALUES (OLD.Id, OLD.Name, OLD.DiscordRoleId, OLD.LastModifiedAt, CURRENT_TIMESTAMP(6)); + INSERT INTO `ShortRoleNamesHistory` (`Id`, `ShortName`, `DiscordRoleId`, `ServerId`, `DateFrom`, `DateTo`) + VALUES (OLD.Id, OLD.ShortName, OLD.DiscordRoleId, OLD.ServerId, OLD.LastModifiedAt, CURRENT_TIMESTAMP(6)); END; DROP TRIGGER IF EXISTS `TR_ShortRoleNamesDelete`; @@ -26,8 +27,8 @@ CREATE TRIGGER `TR_ShortRoleNamesDelete` ON `ShortRoleNames` FOR EACH ROW BEGIN - INSERT INTO `ShortRoleNamesHistory` (`Id`, `Name`, `DiscordRoleId`, `Deleted`, `DateFrom`, + INSERT INTO `ShortRoleNamesHistory` (`Id`, `ShortName`, `DiscordRoleId`, `ServerId`, `Deleted`, `DateFrom`, `DateTo`) - VALUES (OLD.Id, OLD.Name, OLD.DiscordRoleId, TRUE, OLD.LastModifiedAt, + VALUES (OLD.Id, OLD.ShortName, OLD.DiscordRoleId, OLD.ServerId, TRUE, OLD.LastModifiedAt, CURRENT_TIMESTAMP(6)); END; \ No newline at end of file diff --git a/kdb-bot/src/bot_data/migration/fix_updates_migration.py b/kdb-bot/src/bot_data/migration/fix_updates_migration.py index 7ccc96d0..80f846a1 100644 --- a/kdb-bot/src/bot_data/migration/fix_updates_migration.py +++ b/kdb-bot/src/bot_data/migration/fix_updates_migration.py @@ -15,8 +15,8 @@ class FixUpdatesMigration(MigrationABC): def upgrade(self): self._logger.debug(__name__, "Running upgrade") - self._exec("config/server.sql") - self._exec("config/technician.sql") + self._exec(__file__, "config/server.sql") + self._exec(__file__, "config/technician.sql") def downgrade(self): pass 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 de63ea97..7d7d88ca 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 @@ -32,7 +32,8 @@ class ShortRoleNameMigration(MigrationABC): ) ) - self._exec("short_rule_names.sql") + self._exec(__file__, "short_rule_names.sql") def downgrade(self): self._cursor.execute("DROP TABLE `ShortRoleNames`;") + self._cursor.execute("DROP TABLE `ShortRoleNamesHistory`;") diff --git a/kdb-bot/src/bot_data/model/short_role_name.py b/kdb-bot/src/bot_data/model/short_role_name.py new file mode 100644 index 00000000..52f68e75 --- /dev/null +++ b/kdb-bot/src/bot_data/model/short_role_name.py @@ -0,0 +1,118 @@ +from datetime import datetime + +from cpl_core.database import TableABC +from cpl_core.dependency_injection import ServiceProviderABC +from cpl_discord.service import DiscordBotServiceABC + +from bot_data.model.server import Server + + +class ShortRoleName(TableABC): + def __init__( + self, + name: str, + discord_role_id: int, + server: Server, + created_at: datetime = None, + modified_at: datetime = None, + id=0, + ): + self._id = id + self._name = name + self._discord_role_id = discord_role_id + self._server = server + + TableABC.__init__(self) + self._created_at = created_at if created_at is not None else self._created_at + self._modified_at = modified_at if modified_at is not None else self._modified_at + + @property + def id(self) -> int: + return self._id + + @property + def name(self) -> str: + return self._name + + @name.setter + def name(self, value: str): + self._name = value + + @property + def role_id(self) -> int: + return self._discord_role_id + + @role_id.setter + def role_id(self, value: int): + self._discord_role_id = value + + @property + @ServiceProviderABC.inject + def role_name(self, bot: DiscordBotServiceABC) -> str: + guild = bot.get_guild(self._server.discord_id) + return guild.get_role(self.role_id).name + + @property + def server(self) -> Server: + return self._server + + @staticmethod + def get_select_all_string() -> str: + return str( + f""" + SELECT * FROM `ShortRoleNames`; + """ + ) + + @staticmethod + def get_select_by_id_string(id: int) -> str: + return str( + f""" + SELECT * FROM `ShortRoleNames` + WHERE `Id` = {id}; + """ + ) + + @staticmethod + def get_select_by_server_id_string(id: int) -> str: + return str( + f""" + SELECT * FROM `ShortRoleNames` + WHERE `ServerId` = {id}; + """ + ) + + @property + def insert_string(self) -> str: + return str( + f""" + INSERT INTO `ShortRoleNames` ( + `Name`, `DiscordRoleId`, `ServerId` + ) VALUES ( + '{self._name}', + {self._discord_role_id} + {self._server.id}, + ); + """ + ) + + @property + def udpate_string(self) -> str: + return str( + f""" + UPDATE `ShortRoleNames` + SET `Name` = '{self._name}', + `DiscordRoleId` = {self._discord_role_id}, + `ServerId` = {self._server.id} + WHERE `Id` = {self._id}; + """ + ) + + @property + def delete_string(self) -> str: + return str( + f""" + DELETE FROM `ShortRoleNames` + WHERE `Id` = {self._id}; + """ + ) diff --git a/kdb-bot/src/bot_data/model/short_role_name_history.py b/kdb-bot/src/bot_data/model/short_role_name_history.py new file mode 100644 index 00000000..57d7e1b1 --- /dev/null +++ b/kdb-bot/src/bot_data/model/short_role_name_history.py @@ -0,0 +1,56 @@ +from cpl_core.dependency_injection import ServiceProviderABC +from cpl_discord.service import DiscordBotServiceABC + +from bot_data.abc.history_table_abc import HistoryTableABC + + +class AutoRoleRuleHistory(HistoryTableABC): + def __init__( + self, + name: str, + discord_role_id: int, + server: int, + deleted: bool, + date_from: str, + date_to: str, + id=0, + ): + HistoryTableABC.__init__(self) + self._id = id + self._name = name + self._discord_role_id = discord_role_id + self._server = server + + self._deleted = deleted + self._date_from = date_from + self._date_to = date_to + + @property + def id(self) -> int: + return self._id + + @property + def name(self) -> str: + return self._name + + @name.setter + def name(self, value: str): + self._name = value + + @property + def role_id(self) -> int: + return self._discord_role_id + + @role_id.setter + def role_id(self, value: int): + self._discord_role_id = value + + @property + @ServiceProviderABC.inject + def role_name(self, bot: DiscordBotServiceABC) -> str: + guild = bot.get_guild(self._server.discord_id) + return guild.get_role(self.role_id).name + + @property + def server(self) -> int: + return self._server