diff --git a/CHANGELOG.md b/CHANGELOG.md index 7f64d986..b0c03e23 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,8 @@ ### New features TBD ### Bug fixes -TBD +- Fix migrator issue when jumping versions ([Merge Request](https://gitlab.com/crafty-controller/crafty-4/-/merge_requests/734)) +- Fix backend issue causing error when restoring backups in 4.3.x ([Merge Request](https://gitlab.com/crafty-controller/crafty-4/-/merge_requests/736)) ### Tweaks - Clean up remaining http handler references ([Merge Request](https://gitlab.com/crafty-controller/crafty-4/-/merge_requests/733)) - Remove version disclosure on login page ([Merge Request](https://gitlab.com/crafty-controller/crafty-4/-/merge_requests/737)) diff --git a/app/classes/controllers/server_perms_controller.py b/app/classes/controllers/server_perms_controller.py index f6632cd7..37893e9e 100644 --- a/app/classes/controllers/server_perms_controller.py +++ b/app/classes/controllers/server_perms_controller.py @@ -47,7 +47,7 @@ class ServerPermsController: new_server_id, role.role_id, PermissionsServers.get_permissions_mask( - int(role.role_id), int(old_server_id) + int(role.role_id), old_server_id ), ) # Permissions_Servers.add_role_server( diff --git a/app/classes/shared/main_controller.py b/app/classes/shared/main_controller.py index 47e4f6ce..9c3219ff 100644 --- a/app/classes/shared/main_controller.py +++ b/app/classes/shared/main_controller.py @@ -1131,7 +1131,7 @@ class Controller: server_obj.path = new_local_server_path failed = False for s in self.servers.failed_servers: - if int(s["server_id"]) == int(server.get("server_id")): + if s["server_id"] == server.get("server_id"): failed = True if not failed: self.servers.update_server(server_obj) diff --git a/app/classes/shared/migration.py b/app/classes/shared/migration.py index 6a93802b..287bb4f3 100644 --- a/app/classes/shared/migration.py +++ b/app/classes/shared/migration.py @@ -372,11 +372,11 @@ class MigrationManager(object): Create migrator """ migrator = Migrator(self.database) - # Removing the up_one to prevent running all - # migrations each time we got a new one. - # It's handled by migration.up() function. - # for name in self.done: - # self.up_one(name, migrator, True) + # Running false migrations to retrives the schemes of + # the precedents created tables in the table_dict element + # It's useful to run the new migrations + for name in self.done: + self.up_one(name, migrator, True) return migrator def compile(self, name, migrate="", rollback=""): diff --git a/app/classes/web/panel_handler.py b/app/classes/web/panel_handler.py index d61e3c0e..716671c8 100644 --- a/app/classes/web/panel_handler.py +++ b/app/classes/web/panel_handler.py @@ -1403,7 +1403,7 @@ class PanelHandler(BaseHandler): self.controller.management.add_to_audit_log( exec_user["user_id"], f"Removed user {target_user['username']} (UID:{user_id})", - server_id=0, + server_id=None, source_ip=self.get_remote_ip(), ) self.redirect("/panel/panel_config") diff --git a/app/classes/web/public_handler.py b/app/classes/web/public_handler.py index 467765ea..21e2d495 100644 --- a/app/classes/web/public_handler.py +++ b/app/classes/web/public_handler.py @@ -228,7 +228,7 @@ class PublicHandler(BaseHandler): ) # log this login self.controller.management.add_to_audit_log( - user_data.user_id, "Logged in", 0, self.get_remote_ip() + user_data.user_id, "Logged in", None, self.get_remote_ip() ) return self.finish_json( @@ -254,7 +254,7 @@ class PublicHandler(BaseHandler): ) # log this failed login attempt self.controller.management.add_to_audit_log( - user_data.user_id, "Tried to log in", 0, self.get_remote_ip() + user_data.user_id, "Tried to log in", None, self.get_remote_ip() ) return self.finish_json( 403, diff --git a/app/classes/web/routes/api/auth/login.py b/app/classes/web/routes/api/auth/login.py index 7a27c6f8..7e8131f3 100644 --- a/app/classes/web/routes/api/auth/login.py +++ b/app/classes/web/routes/api/auth/login.py @@ -101,7 +101,7 @@ class ApiAuthLoginHandler(BaseApiHandler): # log this login self.controller.management.add_to_audit_log( - user_data.user_id, "logged in via the API", 0, self.get_remote_ip() + user_data.user_id, "logged in via the API", None, self.get_remote_ip() ) self.finish_json( @@ -119,7 +119,7 @@ class ApiAuthLoginHandler(BaseApiHandler): else: # log this failed login attempt self.controller.management.add_to_audit_log( - user_data.user_id, "Tried to log in", 0, self.get_remote_ip() + user_data.user_id, "Tried to log in", None, self.get_remote_ip() ) self.finish_json( 401, diff --git a/app/classes/web/routes/api/crafty/config/index.py b/app/classes/web/routes/api/crafty/config/index.py index 4a9cbd63..b95d15a0 100644 --- a/app/classes/web/routes/api/crafty/config/index.py +++ b/app/classes/web/routes/api/crafty/config/index.py @@ -128,7 +128,7 @@ class ApiCraftyConfigIndexHandler(BaseApiHandler): self.controller.management.add_to_audit_log( user["user_id"], "edited config.json", - server_id=0, + server_id=None, source_ip=self.get_remote_ip(), ) @@ -225,7 +225,7 @@ class ApiCraftyCustomizeIndexHandler(BaseApiHandler): self.controller.management.add_to_audit_log( user["user_id"], f"customized login photo: {data['photo']}/{data['opacity']}", - server_id=0, + server_id=None, source_ip=self.get_remote_ip(), ) self.controller.management.set_login_opacity(int(data["opacity"])) diff --git a/app/classes/web/routes/api/crafty/config/server_dir.py b/app/classes/web/routes/api/crafty/config/server_dir.py index 91c4cc89..87835d35 100644 --- a/app/classes/web/routes/api/crafty/config/server_dir.py +++ b/app/classes/web/routes/api/crafty/config/server_dir.py @@ -109,7 +109,7 @@ class ApiCraftyConfigServerDirHandler(BaseApiHandler): self.controller.management.add_to_audit_log( auth_data[4]["user_id"], f"updated master servers dir to {new_dir}/servers", - server_id=0, + server_id=None, source_ip=self.get_remote_ip(), ) diff --git a/app/classes/web/routes/api/roles/index.py b/app/classes/web/routes/api/roles/index.py index 266afb23..0f656dbb 100644 --- a/app/classes/web/routes/api/roles/index.py +++ b/app/classes/web/routes/api/roles/index.py @@ -161,7 +161,7 @@ class ApiRolesIndexHandler(BaseApiHandler): self.controller.management.add_to_audit_log( user["user_id"], f"created role {role_name} (RID:{role_id})", - server_id=0, + server_id=None, source_ip=self.get_remote_ip(), ) diff --git a/app/classes/web/routes/api/roles/role/index.py b/app/classes/web/routes/api/roles/role/index.py index 5ed12d69..a45c297f 100644 --- a/app/classes/web/routes/api/roles/role/index.py +++ b/app/classes/web/routes/api/roles/role/index.py @@ -112,7 +112,7 @@ class ApiRolesRoleIndexHandler(BaseApiHandler): self.controller.management.add_to_audit_log( user["user_id"], f"deleted role with ID {role_id}", - server_id=0, + server_id=None, source_ip=self.get_remote_ip(), ) @@ -172,7 +172,7 @@ class ApiRolesRoleIndexHandler(BaseApiHandler): self.controller.management.add_to_audit_log( user["user_id"], f"modified role with ID {role_id}", - server_id=0, + server_id=None, source_ip=self.get_remote_ip(), ) diff --git a/app/classes/web/routes/api/servers/server/backups/backup/index.py b/app/classes/web/routes/api/servers/server/backups/backup/index.py index 05edd3a9..70ceb2b2 100644 --- a/app/classes/web/routes/api/servers/server/backups/backup/index.py +++ b/app/classes/web/routes/api/servers/server/backups/backup/index.py @@ -203,7 +203,7 @@ class ApiServersServerBackupsBackupIndexHandler(BaseApiHandler): except JobLookupError as e: logger.info("No active tasks found for server: {e}") self.controller.remove_server(server_id, True) - except Exception as e: + except (FileNotFoundError, NotADirectoryError) as e: return self.finish_json( 400, {"status": "error", "error": f"NO BACKUP FOUND {e}"} ) diff --git a/app/classes/web/routes/api/users/index.py b/app/classes/web/routes/api/users/index.py index f7341d38..fef154a0 100644 --- a/app/classes/web/routes/api/users/index.py +++ b/app/classes/web/routes/api/users/index.py @@ -177,7 +177,7 @@ class ApiUsersIndexHandler(BaseApiHandler): self.controller.management.add_to_audit_log( user["user_id"], f"added user {username} (UID:{user_id}) with roles {roles}", - server_id=0, + server_id=None, source_ip=self.get_remote_ip(), ) diff --git a/app/classes/web/routes/api/users/user/api.py b/app/classes/web/routes/api/users/user/api.py index 1c7635f2..9bdafadf 100644 --- a/app/classes/web/routes/api/users/user/api.py +++ b/app/classes/web/routes/api/users/user/api.py @@ -43,7 +43,7 @@ class ApiUsersUserKeyHandler(BaseApiHandler): auth_data[4]["user_id"], f"Generated a new API token for the key {key.name} " f"from user with UID: {key.user_id}", - server_id=0, + server_id=None, source_ip=self.get_remote_ip(), ) data_key = self.controller.authentication.generate( @@ -173,7 +173,7 @@ class ApiUsersUserKeyHandler(BaseApiHandler): f"Added API key {data['name']} with crafty permissions " f"{data['crafty_permissions_mask']}" f" and {data['server_permissions_mask']} for user with UID: {user_id}", - server_id=0, + server_id=None, source_ip=self.get_remote_ip(), ) self.finish_json(200, {"status": "ok", "data": {"id": key_id}}) @@ -233,7 +233,7 @@ class ApiUsersUserKeyHandler(BaseApiHandler): auth_data[4]["user_id"], f"Removed API key {target_key} " f"(ID: {key_id}) from user {auth_data[4]['user_id']}", - server_id=0, + server_id=None, source_ip=self.get_remote_ip(), ) diff --git a/app/classes/web/routes/api/users/user/index.py b/app/classes/web/routes/api/users/user/index.py index 1b7f6f91..6efee93e 100644 --- a/app/classes/web/routes/api/users/user/index.py +++ b/app/classes/web/routes/api/users/user/index.py @@ -94,7 +94,7 @@ class ApiUsersUserIndexHandler(BaseApiHandler): self.controller.management.add_to_audit_log( user["user_id"], f"deleted the user {user_id}", - server_id=0, + server_id=None, source_ip=self.get_remote_ip(), ) @@ -283,7 +283,7 @@ class ApiUsersUserIndexHandler(BaseApiHandler): f"edited user {user_obj.username} (UID: {user_id})" f"with roles {user_obj.roles}" ), - server_id=0, + server_id=None, source_ip=self.get_remote_ip(), ) diff --git a/app/classes/web/websocket_handler.py b/app/classes/web/websocket_handler.py index cde97584..3e426797 100644 --- a/app/classes/web/websocket_handler.py +++ b/app/classes/web/websocket_handler.py @@ -55,7 +55,7 @@ class WebSocketHandler(tornado.websocket.WebSocketHandler): self.controller.management.add_to_audit_log_raw( "unknown", 0, - 0, + None, "Someone tried to connect via WebSocket without proper authentication", self.get_remote_ip(), ) diff --git a/app/migrations/20240216_rework_servers_uuid.py b/app/migrations/20240216_rework_servers_uuid.py index c741a84c..facd0e42 100644 --- a/app/migrations/20240216_rework_servers_uuid.py +++ b/app/migrations/20240216_rework_servers_uuid.py @@ -54,9 +54,6 @@ def migrate(migrator: Migrator, database, **kwargs): database = db try: - logger.info("Migrating Data from Int to UUID (Type Change)") - Console.info("Migrating Data from Int to UUID (Type Change)") - # Changes on Server Table migrator.alter_column_type( Servers, @@ -87,11 +84,6 @@ def migrate(migrator: Migrator, database, **kwargs): ), ) - migrator.run() - - logger.info("Migrating Data from Int to UUID (Type Change) : SUCCESS") - Console.info("Migrating Data from Int to UUID (Type Change) : SUCCESS") - except Exception as ex: logger.error("Error while migrating Data from Int to UUID (Type Change)") logger.error(ex) @@ -101,118 +93,6 @@ def migrate(migrator: Migrator, database, **kwargs): last_migration.delete() return - try: - logger.info("Migrating Data from Int to UUID (Foreign Keys)") - Console.info("Migrating Data from Int to UUID (Foreign Keys)") - # Changes on Audit Log Table - for audit_log in AuditLog.select(): - old_server_id = audit_log.server_id_id - if old_server_id == "0" or old_server_id is None: - server_uuid = None - else: - try: - server = Servers.get_by_id(old_server_id) - server_uuid = server.server_uuid - except: - server_uuid = old_server_id - AuditLog.update(server_id=server_uuid).where( - AuditLog.audit_id == audit_log.audit_id - ).execute() - - # Changes on Webhooks Log Table - for webhook in Webhooks.select(): - old_server_id = webhook.server_id_id - try: - server = Servers.get_by_id(old_server_id) - server_uuid = server.server_uuid - except: - server_uuid = old_server_id - Webhooks.update(server_id=server_uuid).where( - Webhooks.id == webhook.id - ).execute() - - # Changes on Schedules Log Table - for schedule in Schedules.select(): - old_server_id = schedule.server_id_id - try: - server = Servers.get_by_id(old_server_id) - server_uuid = server.server_uuid - except: - server_uuid = old_server_id - Schedules.update(server_id=server_uuid).where( - Schedules.schedule_id == schedule.schedule_id - ).execute() - - # Changes on Backups Log Table - for backup in Backups.select(): - old_server_id = backup.server_id_id - try: - server = Servers.get_by_id(old_server_id) - server_uuid = server.server_uuid - except: - server_uuid = old_server_id - Backups.update(server_id=server_uuid).where( - Backups.server_id == old_server_id - ).execute() - - # Changes on RoleServers Log Table - for role_servers in RoleServers.select(): - old_server_id = role_servers.server_id_id - try: - server = Servers.get_by_id(old_server_id) - server_uuid = server.server_uuid - except: - server_uuid = old_server_id - RoleServers.update(server_id=server_uuid).where( - RoleServers.role_id == role_servers.id - and RoleServers.server_id == old_server_id - ).execute() - - logger.info("Migrating Data from Int to UUID (Foreign Keys) : SUCCESS") - Console.info("Migrating Data from Int to UUID (Foreign Keys) : SUCCESS") - - except Exception as ex: - logger.error("Error while migrating Data from Int to UUID (Foreign Keys)") - logger.error(ex) - Console.error("Error while migrating Data from Int to UUID (Foreign Keys)") - Console.error(ex) - last_migration = MigrateHistory.get_by_id(MigrateHistory.select().count()) - last_migration.delete() - return - - try: - logger.info("Migrating Data from Int to UUID (Primary Keys)") - Console.info("Migrating Data from Int to UUID (Primary Keys)") - # Migrating servers from the old id type to the new one - for server in Servers.select(): - Servers.update(server_id=server.server_uuid).where( - Servers.server_id == server.server_id - ).execute() - - logger.info("Migrating Data from Int to UUID (Primary Keys) : SUCCESS") - Console.info("Migrating Data from Int to UUID (Primary Keys) : SUCCESS") - - except Exception as ex: - logger.error("Error while migrating Data from Int to UUID (Primary Keys)") - logger.error(ex) - Console.error("Error while migrating Data from Int to UUID (Primary Keys)") - Console.error(ex) - last_migration = MigrateHistory.get_by_id(MigrateHistory.select().count()) - last_migration.delete() - return - - # Changes on Server Table - logger.info("Migrating Data from Int to UUID (Removing UUID Field from Servers)") - Console.info("Migrating Data from Int to UUID (Removing UUID Field from Servers)") - migrator.drop_columns("servers", ["server_uuid"]) - migrator.run() - logger.info( - "Migrating Data from Int to UUID (Removing UUID Field from Servers) : SUCCESS" - ) - Console.info( - "Migrating Data from Int to UUID (Removing UUID Field from Servers) : SUCCESS" - ) - return diff --git a/app/migrations/20240217_rework_servers_uuid_part2.py b/app/migrations/20240217_rework_servers_uuid_part2.py new file mode 100644 index 00000000..eab04c28 --- /dev/null +++ b/app/migrations/20240217_rework_servers_uuid_part2.py @@ -0,0 +1,326 @@ +import datetime +import uuid +import peewee +import logging + +from app.classes.shared.console import Console +from app.classes.shared.migration import Migrator, MigrateHistory +from app.classes.models.management import ( + AuditLog, + Webhooks, + Schedules, + Backups, +) +from app.classes.models.server_permissions import RoleServers +from app.classes.models.base_model import BaseModel + +logger = logging.getLogger(__name__) + + +def migrate(migrator: Migrator, database, **kwargs): + """ + Write your migrations here. + """ + db = database + + # ********************************************************************************** + # Servers New Model from Old (easier to migrate without dunmping Database) + # ********************************************************************************** + class Servers(peewee.Model): + server_id = peewee.CharField(primary_key=True, default=str(uuid.uuid4())) + created = peewee.DateTimeField(default=datetime.datetime.now) + server_uuid = peewee.CharField(default="", index=True) + server_name = peewee.CharField(default="Server", index=True) + path = peewee.CharField(default="") + backup_path = peewee.CharField(default="") + executable = peewee.CharField(default="") + log_path = peewee.CharField(default="") + execution_command = peewee.CharField(default="") + auto_start = peewee.BooleanField(default=0) + auto_start_delay = peewee.IntegerField(default=10) + crash_detection = peewee.BooleanField(default=0) + stop_command = peewee.CharField(default="stop") + executable_update_url = peewee.CharField(default="") + server_ip = peewee.CharField(default="127.0.0.1") + server_port = peewee.IntegerField(default=25565) + logs_delete_after = peewee.IntegerField(default=0) + type = peewee.CharField(default="minecraft-java") + show_status = peewee.BooleanField(default=1) + created_by = peewee.IntegerField(default=-100) + shutdown_timeout = peewee.IntegerField(default=60) + ignored_exits = peewee.CharField(default="0") + + class Meta: + table_name = "servers" + database = db + + this_migration = MigrateHistory.get_or_none( + MigrateHistory.name == "20240217_rework_servers_uuid_part2" + ) + if this_migration is not None: + Console.debug("Update database already done, skipping this part") + return + else: + servers_columns = db.get_columns("servers") + if not any( + column_data.name == "server_uuid" for column_data in servers_columns + ): + Console.debug( + "Servers.server_uuid already deleted in Crafty version 4.3.0, skipping this part" + ) + return + + try: + logger.info("Migrating Data from Int to UUID (Foreign Keys)") + Console.info("Migrating Data from Int to UUID (Foreign Keys)") + # Changes on Audit Log Table + for audit_log in AuditLog.select(): + old_server_id = audit_log.server_id_id + if old_server_id == "0" or old_server_id is None: + server_uuid = None + else: + try: + server = Servers.get_by_id(old_server_id) + server_uuid = server.server_uuid + except: + server_uuid = old_server_id + AuditLog.update(server_id=server_uuid).where( + AuditLog.audit_id == audit_log.audit_id + ).execute() + + # Changes on Webhooks Log Table + for webhook in Webhooks.select(): + old_server_id = webhook.server_id_id + try: + server = Servers.get_by_id(old_server_id) + server_uuid = server.server_uuid + except: + server_uuid = old_server_id + Webhooks.update(server_id=server_uuid).where( + Webhooks.id == webhook.id + ).execute() + + # Changes on Schedules Log Table + for schedule in Schedules.select(): + old_server_id = schedule.server_id_id + try: + server = Servers.get_by_id(old_server_id) + server_uuid = server.server_uuid + except: + server_uuid = old_server_id + Schedules.update(server_id=server_uuid).where( + Schedules.schedule_id == schedule.schedule_id + ).execute() + + # Changes on Backups Log Table + for backup in Backups.select(): + old_server_id = backup.server_id_id + try: + server = Servers.get_by_id(old_server_id) + server_uuid = server.server_uuid + except: + server_uuid = old_server_id + Backups.update(server_id=server_uuid).where( + Backups.server_id == old_server_id + ).execute() + + # Changes on RoleServers Log Table + for role_servers in RoleServers.select(): + old_server_id = role_servers.server_id_id + try: + server = Servers.get_by_id(old_server_id) + server_uuid = server.server_uuid + except: + server_uuid = old_server_id + RoleServers.update(server_id=server_uuid).where( + RoleServers.role_id == role_servers.id + and RoleServers.server_id == old_server_id + ).execute() + + logger.info("Migrating Data from Int to UUID (Foreign Keys) : SUCCESS") + Console.info("Migrating Data from Int to UUID (Foreign Keys) : SUCCESS") + + except Exception as ex: + logger.error("Error while migrating Data from Int to UUID (Foreign Keys)") + logger.error(ex) + Console.error("Error while migrating Data from Int to UUID (Foreign Keys)") + Console.error(ex) + last_migration = MigrateHistory.get_by_id(MigrateHistory.select().count()) + last_migration.delete() + return + + try: + logger.info("Migrating Data from Int to UUID (Primary Keys)") + Console.info("Migrating Data from Int to UUID (Primary Keys)") + # Migrating servers from the old id type to the new one + for server in Servers.select(): + Servers.update(server_id=server.server_uuid).where( + Servers.server_id == server.server_id + ).execute() + + logger.info("Migrating Data from Int to UUID (Primary Keys) : SUCCESS") + Console.info("Migrating Data from Int to UUID (Primary Keys) : SUCCESS") + + except Exception as ex: + logger.error("Error while migrating Data from Int to UUID (Primary Keys)") + logger.error(ex) + Console.error("Error while migrating Data from Int to UUID (Primary Keys)") + Console.error(ex) + last_migration = MigrateHistory.get_by_id(MigrateHistory.select().count()) + last_migration.delete() + return + + return + + +def rollback(migrator: Migrator, database, **kwargs): + """ + Write your rollback migrations here. + """ + db = database + + # Condition to prevent running rollback each time we've got a rollback to do + this_migration = MigrateHistory.get_or_none( + MigrateHistory.name == "20240217_rework_servers_uuid_part2" + ) + if this_migration is None: + Console.debug("Update database already done, skipping this part") + return + + # ********************************************************************************** + # Servers New Model from Old (easier to migrate without dunmping Database) + # ********************************************************************************** + class Servers(peewee.Model): + server_id = peewee.CharField(primary_key=True, default=str(uuid.uuid4())) + created = peewee.DateTimeField(default=datetime.datetime.now) + server_uuid = peewee.CharField(default="", index=True) + server_name = peewee.CharField(default="Server", index=True) + path = peewee.CharField(default="") + backup_path = peewee.CharField(default="") + executable = peewee.CharField(default="") + log_path = peewee.CharField(default="") + execution_command = peewee.CharField(default="") + auto_start = peewee.BooleanField(default=0) + auto_start_delay = peewee.IntegerField(default=10) + crash_detection = peewee.BooleanField(default=0) + stop_command = peewee.CharField(default="stop") + executable_update_url = peewee.CharField(default="") + server_ip = peewee.CharField(default="127.0.0.1") + server_port = peewee.IntegerField(default=25565) + logs_delete_after = peewee.IntegerField(default=0) + type = peewee.CharField(default="minecraft-java") + show_status = peewee.BooleanField(default=1) + created_by = peewee.IntegerField(default=-100) + shutdown_timeout = peewee.IntegerField(default=60) + ignored_exits = peewee.CharField(default="0") + + class Meta: + table_name = "servers" + database = db + + try: + logger.info("Migrating Data from UUID to Int (Primary Keys)") + Console.info("Migrating Data from UUID to Int (Primary Keys)") + # Migrating servers from the old id type to the new one + new_id = 0 + for server in Servers.select(): + new_id += 1 + Servers.update(server_uuid=server.server_id).where( + Servers.server_id == server.server_id + ).execute() + Servers.update(server_id=new_id).where( + Servers.server_id == server.server_id + ).execute() + + logger.info("Migrating Data from UUID to Int (Primary Keys) : SUCCESS") + Console.info("Migrating Data from UUID to Int (Primary Keys) : SUCCESS") + + except Exception as ex: + logger.error("Error while migrating Data from UUID to Int (Primary Keys)") + logger.error(ex) + Console.error("Error while migrating Data from UUID to Int (Primary Keys)") + Console.error(ex) + last_migration = MigrateHistory.get_by_id(MigrateHistory.select().count()) + last_migration.delete() + return + + try: + logger.info("Migrating Data from UUID to Int (Foreign Keys)") + Console.info("Migrating Data from UUID to Int (Foreign Keys)") + # Changes on Audit Log Table + for audit_log in AuditLog.select(): + old_server_id = audit_log.server_id_id + if old_server_id is None: + new_server_id = 0 + else: + try: + server = Servers.get_or_none(Servers.server_uuid == old_server_id) + new_server_id = server.server_id + except: + new_server_id = old_server_id + AuditLog.update(server_id=new_server_id).where( + AuditLog.audit_id == audit_log.audit_id + ).execute() + + # Changes on Webhooks Log Table + for webhook in Webhooks.select(): + old_server_id = webhook.server_id_id + try: + server = Servers.get_or_none(Servers.server_uuid == old_server_id) + new_server_id = server.server_id + except: + new_server_id = old_server_id + Webhooks.update(server_id=new_server_id).where( + Webhooks.id == webhook.id + ).execute() + + # Changes on Schedules Log Table + for schedule in Schedules.select(): + old_server_id = schedule.server_id_id + try: + server = Servers.get_or_none(Servers.server_uuid == old_server_id) + new_server_id = server.server_id + except: + new_server_id = old_server_id + Schedules.update(server_id=new_server_id).where( + Schedules.schedule_id == schedule.schedule_id + ).execute() + + # Changes on Backups Log Table + for backup in Backups.select(): + old_server_id = backup.server_id_id + try: + server = Servers.get_or_none(Servers.server_uuid == old_server_id) + new_server_id = server.server_id + except: + new_server_id = old_server_id + Backups.update(server_id=new_server_id).where( + Backups.server_id == old_server_id + ).execute() + + # Changes on RoleServers Log Table + for role_servers in RoleServers.select(): + old_server_id = role_servers.server_id_id + try: + server = Servers.get_or_none(Servers.server_uuid == old_server_id) + new_server_id = server.server_id + except: + new_server_id = old_server_id + RoleServers.update(server_id=new_server_id).where( + RoleServers.role_id == role_servers.id + and RoleServers.server_id == old_server_id + ).execute() + + logger.info("Migrating Data from UUID to Int (Foreign Keys) : SUCCESS") + Console.info("Migrating Data from UUID to Int (Foreign Keys) : SUCCESS") + + except Exception as ex: + logger.error("Error while migrating Data from UUID to Int (Foreign Keys)") + logger.error(ex) + Console.error("Error while migrating Data from UUID to Int (Foreign Keys)") + Console.error(ex) + last_migration = MigrateHistory.get_by_id(MigrateHistory.select().count()) + last_migration.delete() + return + + return diff --git a/app/migrations/20240310_fixing_rework_servers_uuid.py b/app/migrations/20240310_fixing_rework_servers_uuid.py index 3c613e52..cbde833b 100644 --- a/app/migrations/20240310_fixing_rework_servers_uuid.py +++ b/app/migrations/20240310_fixing_rework_servers_uuid.py @@ -7,6 +7,7 @@ from app.classes.shared.console import Console from app.classes.shared.migration import Migrator, MigrateHistory from app.classes.models.management import Schedules, Backups from app.classes.models.server_permissions import RoleServers +from app.classes.models.servers import Servers logger = logging.getLogger(__name__) @@ -17,40 +18,7 @@ def migrate(migrator: Migrator, database, **kwargs): """ db = database - # ********************************************************************************** - # Servers New Model from Old (easier to migrate without dunmping Database) - # ********************************************************************************** - class Servers(peewee.Model): - server_id = peewee.CharField(primary_key=True, default=str(uuid.uuid4())) - created = peewee.DateTimeField(default=datetime.datetime.now) - server_name = peewee.CharField(default="Server", index=True) - path = peewee.CharField(default="") - backup_path = peewee.CharField(default="") - executable = peewee.CharField(default="") - log_path = peewee.CharField(default="") - execution_command = peewee.CharField(default="") - auto_start = peewee.BooleanField(default=0) - auto_start_delay = peewee.IntegerField(default=10) - crash_detection = peewee.BooleanField(default=0) - stop_command = peewee.CharField(default="stop") - executable_update_url = peewee.CharField(default="") - server_ip = peewee.CharField(default="127.0.0.1") - server_port = peewee.IntegerField(default=25565) - logs_delete_after = peewee.IntegerField(default=0) - type = peewee.CharField(default="minecraft-java") - show_status = peewee.BooleanField(default=1) - created_by = peewee.IntegerField(default=-100) - shutdown_timeout = peewee.IntegerField(default=60) - ignored_exits = peewee.CharField(default="0") - - class Meta: - table_name = "servers" - database = db - try: - logger.info("Migrating Data from Int to UUID (Fixing Issue)") - Console.info("Migrating Data from Int to UUID (Fixing Issue)") - # Changes on Servers Roles Table migrator.alter_column_type( RoleServers, @@ -87,10 +55,13 @@ def migrate(migrator: Migrator, database, **kwargs): ), ) - migrator.run() - - logger.info("Migrating Data from Int to UUID (Fixing Issue) : SUCCESS") - Console.info("Migrating Data from Int to UUID (Fixing Issue) : SUCCESS") + # Drop Column after migration + servers_columns = db.get_columns("servers") + if any(column_data.name == "server_uuid" for column_data in servers_columns): + Console.debug( + "Servers.server_uuid not deleted before Crafty version 4.3.2, skipping this part" + ) + migrator.drop_columns("servers", ["server_uuid"]) except Exception as ex: logger.error("Error while migrating Data from Int to UUID (Fixing Issue)") @@ -130,3 +101,7 @@ def rollback(migrator: Migrator, database, **kwargs): "server_id", peewee.IntegerField(null=True), ) + + migrator.add_columns( + "servers", server_uuid=peewee.CharField(default="", index=True) + ) # Recreating the column for roll back