Added last migration parts #246

This commit is contained in:
Sven Heidemann 2023-03-06 20:21:47 +01:00
parent c3ef7a746f
commit 5df0501505
9 changed files with 37 additions and 66 deletions

View File

@ -18,7 +18,7 @@
"Dependencies": [ "Dependencies": [
"cpl-core==2022.12.1.post3", "cpl-core==2022.12.1.post3",
"cpl-translation==2022.12.1", "cpl-translation==2022.12.1",
"cpl-query==2022.12.2.post1", "cpl-query==2022.12.2.post2",
"Flask==2.2.2", "Flask==2.2.2",
"Flask-Classful==0.14.2", "Flask-Classful==0.14.2",
"Flask-Cors==3.0.10", "Flask-Cors==3.0.10",

View File

@ -3,6 +3,7 @@ from abc import ABC, abstractmethod
class MigrationABC(ABC): class MigrationABC(ABC):
name = None name = None
prio = 0
@abstractmethod @abstractmethod
def __init__(self): def __init__(self):

View File

@ -4,7 +4,7 @@ from bot_data.db_context import DBContext
class ApiKeyMigration(MigrationABC): class ApiKeyMigration(MigrationABC):
name = "1.0_ApiKeyMigration" name = "1.0.0_ApiKeyMigration"
def __init__(self, logger: DatabaseLogger, db: DBContext): def __init__(self, logger: DatabaseLogger, db: DBContext):
MigrationABC.__init__(self) MigrationABC.__init__(self)

View File

@ -7,6 +7,7 @@ from bot_data.db_context import DBContext
class DBHistoryMigration(MigrationABC): class DBHistoryMigration(MigrationABC):
name = "1.0.0_DBHistoryMigration" name = "1.0.0_DBHistoryMigration"
prio = 1
def __init__(self, logger: DatabaseLogger, db: DBContext): def __init__(self, logger: DatabaseLogger, db: DBContext):
MigrationABC.__init__(self) MigrationABC.__init__(self)
@ -14,66 +15,35 @@ class DBHistoryMigration(MigrationABC):
self._db = db self._db = db
self._cursor = db.cursor 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): def upgrade(self):
self._logger.debug(__name__, "Running upgrade") self._logger.debug(__name__, "Running upgrade")
self._cursor.execute( self._exec("api_keys.sql")
open(f"{os.path.dirname(os.path.realpath(__file__))}/db_history_scripts/api_keys.sql").read() self._exec("auth_users.sql")
) self._exec("auth_user_users_relation.sql")
self._cursor.execute( self._exec("auto_role_rules.sql")
open(f"{os.path.dirname(os.path.realpath(__file__))}/db_history_scripts/auth_users.sql").read() self._exec("auto_roles.sql")
) self._exec("clients.sql")
self._cursor.execute( self._exec("game_servers.sql")
open( self._exec("known_users.sql")
f"{os.path.dirname(os.path.realpath(__file__))}/db_history_scripts/auth_user_users_relation.sql" self._exec("levels.sql")
).read() self._exec("servers.sql")
) self._exec("user_game_idents.sql")
self._cursor.execute( self._exec("user_joined_game_servers.sql")
open(f"{os.path.dirname(os.path.realpath(__file__))}/db_history_scripts/auto_role_rules.sql").read() self._exec("user_joined_servers.sql")
) self._exec("user_joined_voice_channel.sql")
self._cursor.execute( self._exec("user_message_count_per_hour.sql")
open(f"{os.path.dirname(os.path.realpath(__file__))}/db_history_scripts/auto_roles.sql").read() self._exec("users.sql")
) self._exec("user_warnings.sql")
self._cursor.execute(
open(f"{os.path.dirname(os.path.realpath(__file__))}/db_history_scripts/clients.sql").read() self._logger.debug(__name__, "Finished history upgrade")
)
self._cursor.execute(
open(f"{os.path.dirname(os.path.realpath(__file__))}/db_history_scripts/game_servers.sql").read()
)
self._cursor.execute(
open(f"{os.path.dirname(os.path.realpath(__file__))}/db_history_scripts/known_users.sql").read()
)
self._cursor.execute(
open(f"{os.path.dirname(os.path.realpath(__file__))}/db_history_scripts/levels.sql").read()
)
self._cursor.execute(
open(f"{os.path.dirname(os.path.realpath(__file__))}/db_history_scripts/servers.sql").read()
)
self._cursor.execute(
open(f"{os.path.dirname(os.path.realpath(__file__))}/db_history_scripts/user_game_idents.sql").read()
)
self._cursor.execute(
open(
f"{os.path.dirname(os.path.realpath(__file__))}/db_history_scripts/user_joined_game_servers.sql"
).read()
)
self._cursor.execute(
open(f"{os.path.dirname(os.path.realpath(__file__))}/db_history_scripts/user_joined_servers.sql").read()
)
self._cursor.execute(
open(
f"{os.path.dirname(os.path.realpath(__file__))}/db_history_scripts/user_joined_voice_channel.sql"
).read()
)
self._cursor.execute(
open(
f"{os.path.dirname(os.path.realpath(__file__))}/db_history_scripts/user_message_count_per_hour.sql"
).read()
)
self._cursor.execute(open(f"{os.path.dirname(os.path.realpath(__file__))}/db_history_scripts/users.sql").read())
self._cursor.execute(
open(f"{os.path.dirname(os.path.realpath(__file__))}/db_history_scripts/user_warnings.sql").read()
)
def downgrade(self): def downgrade(self):
self._cursor.execute("DROP TABLE `ApiKeysHistory`;") self._cursor.execute("DROP TABLE `ApiKeysHistory`;")

View File

@ -4,7 +4,7 @@ from bot_data.db_context import DBContext
class RemoveStatsMigration(MigrationABC): class RemoveStatsMigration(MigrationABC):
name = "1.0_RemoveStatsMigration" name = "1.0.0_RemoveStatsMigration"
def __init__(self, logger: DatabaseLogger, db: DBContext): def __init__(self, logger: DatabaseLogger, db: DBContext):
MigrationABC.__init__(self) MigrationABC.__init__(self)

View File

@ -4,7 +4,7 @@ from bot_data.db_context import DBContext
class UserJoinedGameServerMigration(MigrationABC): class UserJoinedGameServerMigration(MigrationABC):
name = "1.0_UserJoinedGameServerMigration" name = "1.0.0_UserJoinedGameServerMigration"
def __init__(self, logger: DatabaseLogger, db: DBContext): def __init__(self, logger: DatabaseLogger, db: DBContext):
MigrationABC.__init__(self) MigrationABC.__init__(self)

View File

@ -4,7 +4,7 @@ from bot_data.db_context import DBContext
class UserWarningMigration(MigrationABC): class UserWarningMigration(MigrationABC):
name = "1.0_UserWarningMigration" name = "1.0.0_UserWarningMigration"
def __init__(self, logger: DatabaseLogger, db: DBContext): def __init__(self, logger: DatabaseLogger, db: DBContext):
MigrationABC.__init__(self) MigrationABC.__init__(self)

View File

@ -147,7 +147,7 @@ class Client(TableABC):
`DeletedMessageCount`, `DeletedMessageCount`,
`ReceivedCommandsCount`, `ReceivedCommandsCount`,
`MovedUsersCount`, `MovedUsersCount`,
`ServerId`, `ServerId`
) VALUES ( ) VALUES (
{self._discord_client_id}, {self._discord_client_id},
{self._sent_message_count}, {self._sent_message_count},

View File

@ -1,5 +1,3 @@
from typing import Type
from cpl_core.database.context import DatabaseContextABC from cpl_core.database.context import DatabaseContextABC
from cpl_core.dependency_injection import ServiceProviderABC from cpl_core.dependency_injection import ServiceProviderABC
from cpl_query.extension import List from cpl_query.extension import List
@ -22,7 +20,9 @@ class MigrationService:
self._db = db self._db = db
self._cursor = db.cursor self._cursor = db.cursor
self._migrations = List(type, MigrationABC.__subclasses__()).order_by(lambda x: x.name) self._migrations: List[MigrationABC] = (
List(type, MigrationABC.__subclasses__()).order_by(lambda x: x.name).order_by(lambda x: x.prio)
)
def migrate(self): def migrate(self):
self._logger.info(__name__, f"Running Migrations") self._logger.info(__name__, f"Running Migrations")