Added birthday to wi #401

This commit is contained in:
2023-10-10 18:50:20 +02:00
parent c88e07d743
commit b7ff070676
27 changed files with 449 additions and 148 deletions

View File

@@ -0,0 +1,84 @@
from bot_core.logging.database_logger import DatabaseLogger
from bot_data.abc.migration_abc import MigrationABC
from bot_data.db_context import DBContext
class BirthdayMigration(MigrationABC):
name = "1.2.0_BirthdayMigration"
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"""
ALTER TABLE Users
ADD Birthday DATE NULL AFTER MessageCount;
"""
)
)
self._cursor.execute(
str(
f"""
ALTER TABLE UsersHistory
ADD Birthday DATE NULL AFTER MessageCount;
"""
)
)
self._exec(__file__, "users.sql")
self._cursor.execute(
str(
f"""
ALTER TABLE CFG_Server
ADD XpForBirthday BIGINT(20) NOT NULL DEFAULT 0 AFTER XpPerAchievement;
"""
)
)
self._cursor.execute(
str(
f"""
ALTER TABLE CFG_ServerHistory
ADD XpForBirthday BIGINT(20) NOT NULL DEFAULT 0 AFTER XpPerAchievement;
"""
)
)
self._exec(__file__, "config/server.sql")
def downgrade(self):
self._cursor.execute(
str(
f"""
ALTER TABLE Users DROP COLUMN Birthday;
"""
)
)
self._cursor.execute(
str(
f"""
ALTER TABLE UsersHistory DROP COLUMN Birthday;
"""
)
)
self._cursor.execute(
str(
f"""
ALTER TABLE CFG_Server DROP COLUMN XpForBirthday;
"""
)
)
self._cursor.execute(
str(
f"""
ALTER TABLE CFG_ServerHistory DROP COLUMN XpForBirthday;
"""
)
)

View File

@@ -6,13 +6,16 @@ ALTER TABLE `Users`
CREATE TABLE IF NOT EXISTS `UsersHistory`
(
`Id` BIGINT(20) NOT NULL,
`DiscordId` BIGINT(20) NOT NULL,
`XP` BIGINT(20) NOT NULL DEFAULT 0,
`ServerId` BIGINT(20) DEFAULT NULL,
`Deleted` BOOL DEFAULT FALSE,
`DateFrom` DATETIME(6) NOT NULL,
`DateTo` DATETIME(6) NOT NULL
`Id` BIGINT(20) NOT NULL,
`DiscordId` BIGINT(20) NOT NULL,
`XP` BIGINT(20) NOT NULL DEFAULT 0,
`ReactionCount` BIGINT(20) NOT NULL DEFAULT 0,
`MessageCount` BIGINT(20) NOT NULL DEFAULT 0,
`Birthday` DATE NULL,
`ServerId` BIGINT(20) DEFAULT NULL,
`Deleted` BOOL DEFAULT FALSE,
`DateFrom` DATETIME(6) NOT NULL,
`DateTo` DATETIME(6) NOT NULL
);
DROP TRIGGER IF EXISTS `TR_UsersUpdate`;
@@ -22,12 +25,10 @@ CREATE TRIGGER `TR_UsersUpdate`
ON `Users`
FOR EACH ROW
BEGIN
INSERT INTO `UsersHistory` (
`Id`, `DiscordId`, `XP`, `ServerId`, `DateFrom`, `DateTo`
)
VALUES (
OLD.UserId, OLD.DiscordId, OLD.XP, OLD.ServerId, OLD.LastModifiedAt, CURRENT_TIMESTAMP(6)
);
INSERT INTO `UsersHistory` (`Id`, `DiscordId`, `XP`, `ReactionCount`, `MessageCount`, `Birthday`, `ServerId`,
`DateFrom`, `DateTo`)
VALUES (OLD.UserId, OLD.DiscordId, OLD.XP, OLD.ReactionCount, OLD.MessageCount, OLD.Birthday, OLD.ServerId,
OLD.LastModifiedAt, CURRENT_TIMESTAMP(6));
END;
DROP TRIGGER IF EXISTS `TR_UsersDelete`;
@@ -37,10 +38,8 @@ CREATE TRIGGER `TR_UsersDelete`
ON `Users`
FOR EACH ROW
BEGIN
INSERT INTO `UsersHistory` (
`Id`, `DiscordId`, `XP`, `ServerId`, `Deleted`, `DateFrom`, `DateTo`
)
VALUES (
OLD.UserId, OLD.DiscordId, OLD.XP, OLD.ServerId, TRUE, OLD.LastModifiedAt, CURRENT_TIMESTAMP(6)
);
INSERT INTO `UsersHistory` (`Id`, `DiscordId`, `XP`, `ReactionCount`, `MessageCount`, `Birthday`, `ServerId`,
`Deleted`, `DateFrom`, `DateTo`)
VALUES (OLD.UserId, OLD.DiscordId, OLD.XP, OLD.ReactionCount, OLD.MessageCount, OLD.Birthday, OLD.ServerId, TRUE,
OLD.LastModifiedAt, CURRENT_TIMESTAMP(6));
END;

View File

@@ -0,0 +1,45 @@
from bot_core.logging.database_logger import DatabaseLogger
from bot_data.abc.migration_abc import MigrationABC
from bot_data.db_context import DBContext
class FixUserHistoryMigration(MigrationABC):
name = "1.2.0_FixUserHistoryMigration"
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")
# fix 1.1.0_AchievementsMigration
self._cursor.execute(
str(
f"""ALTER TABLE UsersHistory ADD COLUMN IF NOT EXISTS ReactionCount BIGINT NOT NULL DEFAULT 0 AFTER XP;"""
)
)
self._cursor.execute(
str(
f"""ALTER TABLE UsersHistory ADD COLUMN IF NOT EXISTS MessageCount BIGINT NOT NULL DEFAULT 0 AFTER ReactionCount;"""
)
)
self._exec(__file__, "users.sql")
def downgrade(self):
self._cursor.execute(
str(
f"""
ALTER TABLE UsersHistory DROP COLUMN MessageCount;
"""
)
)
self._cursor.execute(
str(
f"""
ALTER TABLE UsersHistory DROP COLUMN ReactionCount;
"""
)
)

View File

@@ -24,6 +24,7 @@ class ServerConfig(TableABC, ConfigurationModelABC):
xp_per_ontime_hour: int,
xp_per_event_participation: int,
xp_per_achievement: int,
xp_for_birthday: int,
afk_command_channel_id: int,
help_voice_channel_id: int,
team_channel_id: int,
@@ -48,6 +49,7 @@ class ServerConfig(TableABC, ConfigurationModelABC):
self._xp_per_ontime_hour = xp_per_ontime_hour
self._xp_per_event_participation = xp_per_event_participation
self._xp_per_achievement = xp_per_achievement
self._xp_for_birthday = xp_for_birthday
self._afk_command_channel_id = afk_command_channel_id
self._help_voice_channel_id = help_voice_channel_id
self._team_channel_id = team_channel_id
@@ -76,6 +78,7 @@ class ServerConfig(TableABC, ConfigurationModelABC):
10,
10,
10,
10,
guild.system_channel.id,
guild.system_channel.id,
guild.system_channel.id,
@@ -164,6 +167,14 @@ class ServerConfig(TableABC, ConfigurationModelABC):
def xp_per_achievement(self, value: int):
self._xp_per_achievement = value
@property
def xp_for_birthday(self) -> int:
return self._xp_for_birthday
@xp_for_birthday.setter
def xp_for_birthday(self, value: int):
self._xp_for_birthday = value
@property
def afk_command_channel_id(self) -> int:
return self._afk_command_channel_id
@@ -280,6 +291,7 @@ class ServerConfig(TableABC, ConfigurationModelABC):
`XpPerOntimeHour`,
`XpPerEventParticipation`,
`XpPerAchievement`,
`XpForBirthday`,
`AFKCommandChannelId`,
`HelpVoiceChannelId`,
`TeamChannelId`,
@@ -298,6 +310,7 @@ class ServerConfig(TableABC, ConfigurationModelABC):
{self._xp_per_ontime_hour},
{self._xp_per_event_participation},
{self._xp_per_achievement},
'{self._xp_for_birthday}',
{self._afk_command_channel_id},
{self._help_voice_channel_id},
{self._team_channel_id},
@@ -324,6 +337,7 @@ class ServerConfig(TableABC, ConfigurationModelABC):
`XpPerOntimeHour` = {self._xp_per_ontime_hour},
`XpPerEventParticipation` = {self._xp_per_event_participation},
`XpPerAchievement` = {self._xp_per_achievement},
`XpForBirthday` = {self._xp_for_birthday},
`AFKCommandChannelId` = {self._afk_command_channel_id},
`HelpVoiceChannelId` = {self._help_voice_channel_id},
`TeamChannelId` = {self._team_channel_id},

View File

@@ -1,4 +1,4 @@
from datetime import datetime
from datetime import datetime, date
from typing import Optional
from cpl_core.database import TableABC
@@ -17,6 +17,7 @@ class User(TableABC):
xp: int,
reaction_count: int,
message_count: int,
birthday: Optional[date],
server: Optional[Server],
created_at: datetime = None,
modified_at: datetime = None,
@@ -27,6 +28,7 @@ class User(TableABC):
self._xp = xp
self._reaction_count = reaction_count
self._message_count = message_count
self._birthday = birthday
self._server = server
TableABC.__init__(self)
@@ -79,6 +81,14 @@ class User(TableABC):
def reaction_count(self, value: int):
self._reaction_count = value
@property
def birthday(self) -> Optional[datetime]:
return self._birthday
@birthday.setter
def birthday(self, value: Optional[datetime]):
self._birthday = value
@property
@ServiceProviderABC.inject
def ontime(self, services: ServiceProviderABC) -> float:
@@ -171,12 +181,13 @@ class User(TableABC):
return str(
f"""
INSERT INTO `Users` (
`DiscordId`, `XP`, `MessageCount`, `ReactionCount`, `ServerId`
`DiscordId`, `XP`, `MessageCount`, `ReactionCount`, `Birthday`, `ServerId`
) VALUES (
{self._discord_id},
{self._xp},
{self._message_count},
{self._reaction_count},
'{self._birthday}',
{self._server.id}
);
"""
@@ -189,7 +200,8 @@ class User(TableABC):
UPDATE `Users`
SET `XP` = {self._xp},
`MessageCount` = {self._message_count},
`ReactionCount` = {self._reaction_count}
`ReactionCount` = {self._reaction_count},
`Birthday` = '{self._birthday}'
WHERE `UserId` = {self._user_id};
"""
)

View File

@@ -66,12 +66,13 @@ class ServerConfigRepositoryService(ServerConfigRepositoryABC):
result[13],
result[14],
result[15],
json.loads(result[16]),
self._servers.get_server_by_id(result[17]),
self._get_afk_channel_ids(result[17]),
self._get_team_role_ids(result[17]),
result[18],
result[16],
json.loads(result[17]),
self._servers.get_server_by_id(result[18]),
self._get_afk_channel_ids(result[18]),
self._get_team_role_ids(result[18]),
result[19],
result[20],
id=result[0],
)

View File

@@ -29,9 +29,10 @@ class UserRepositoryService(UserRepositoryABC):
result[2],
result[3],
result[4],
self._servers.get_server_by_id(result[5]),
result[6],
result[5].strftime("%d.%m.%Y") if result[5] is not None else None,
self._servers.get_server_by_id(result[6]),
result[7],
result[8],
id=result[0],
)