Fixed migration #378

This commit is contained in:
Sven Heidemann 2023-09-28 09:23:15 +02:00
parent 5e9cca5b1d
commit a303108da2
8 changed files with 209 additions and 42 deletions

View File

@ -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"):

View File

@ -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(

View File

@ -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")

View File

@ -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;

View File

@ -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

View File

@ -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`;")

View File

@ -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};
"""
)

View File

@ -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