From b1b55f16bed2bd3bd47232b703266ba5af61a626 Mon Sep 17 00:00:00 2001 From: Zurdi Date: Wed, 3 Jul 2024 17:19:49 +0200 Subject: [PATCH] fixed migrations --- .../versions/0019_resources_refactor.py | 12 ++-- backend/alembic/versions/0022_collections_.py | 57 +++++++++++++++++++ 2 files changed, 62 insertions(+), 7 deletions(-) diff --git a/backend/alembic/versions/0019_resources_refactor.py b/backend/alembic/versions/0019_resources_refactor.py index d463cc0b7..22b47ba13 100644 --- a/backend/alembic/versions/0019_resources_refactor.py +++ b/backend/alembic/versions/0019_resources_refactor.py @@ -44,7 +44,7 @@ def upgrade() -> None: old_folder_path = f"{RESOURCES_BASE_PATH}/{platform_slug}/{rom.name}" new_folder_path = ( - f"{RESOURCES_BASE_PATH}/roms/{platform_folder_name}/{rom_folder_name}" + f"{RESOURCES_BASE_PATH}/{platform_folder_name}/{rom_folder_name}" ) print("INFO:\t [Resources migration] Renaming folder:") @@ -52,9 +52,7 @@ def upgrade() -> None: print(f"INFO:\t [Resources migration] - new: {new_folder_path}") try: - os.makedirs( - f"{RESOURCES_BASE_PATH}/roms/{platform_folder_name}", exist_ok=True - ) + os.makedirs(f"{RESOURCES_BASE_PATH}/{platform_folder_name}", exist_ok=True) except OSError as error: print(error) @@ -70,17 +68,17 @@ def upgrade() -> None: updated_path_cover_s = re.sub( quoted_platform_slug, - f"roms/{platform_folder_name}", + platform_folder_name, re.sub(quoted_rom_name, rom_folder_name, rom.path_cover_s), ) updated_path_cover_l = re.sub( quoted_platform_slug, - f"roms/{platform_folder_name}", + platform_folder_name, re.sub(quoted_rom_name, rom_folder_name, rom.path_cover_l), ) updated_path_screenshots = re.sub( quoted_platform_slug, - f"roms/{platform_folder_name}", + platform_folder_name, re.sub(quoted_rom_name, rom_folder_name, rom.path_screenshots), ) diff --git a/backend/alembic/versions/0022_collections_.py b/backend/alembic/versions/0022_collections_.py index 992ce32d2..7c11b82af 100644 --- a/backend/alembic/versions/0022_collections_.py +++ b/backend/alembic/versions/0022_collections_.py @@ -6,8 +6,13 @@ """ +import json +import os +import shutil + import sqlalchemy as sa from alembic import op +from config import RESOURCES_BASE_PATH from sqlalchemy.dialects import mysql # revision identifiers, used by Alembic. @@ -52,6 +57,58 @@ def upgrade() -> None: nullable=True, ) + connection = op.get_bind() + roms = connection.execute( + sa.text( + "SELECT id, name, platform_id, path_cover_s, path_cover_l, path_screenshots FROM roms" + ) + ).fetchall() + + # Define the path for the new folder + roms_folder_path = os.path.join(RESOURCES_BASE_PATH, "roms") + + # Create the new folder if it doesn't exist + os.makedirs(roms_folder_path, exist_ok=True) + + # List all items in the base directory + for folder in os.listdir(RESOURCES_BASE_PATH): + folder_path = os.path.join(RESOURCES_BASE_PATH, folder) + + # Check if the item is a directory and not the new folder itself + if os.path.isdir(folder_path) and folder != "roms": + # Move the folder to the new folder + shutil.move(folder_path, roms_folder_path) + + # Update paths for each rom + for rom in roms: + path_cover_s = rom.path_cover_s + path_cover_l = rom.path_cover_l + path_screenshots = rom.path_screenshots + + # Add "roms/" prefix to path_cover_s and path_cover_l + if path_cover_s: + path_cover_s = f"roms/{path_cover_s}" + if path_cover_l: + path_cover_l = f"roms/{path_cover_l}" + + # Add "roms/" prefix to each path in path_screenshots + if path_screenshots: + path_screenshots_list = json.loads(path_screenshots) + path_screenshots_list = [f"roms/{path}" for path in path_screenshots_list] + path_screenshots = json.dumps(path_screenshots_list) + + # Update the database with the new paths + connection.execute( + sa.text( + "UPDATE roms SET path_cover_s = :path_cover_s, path_cover_l = :path_cover_l, path_screenshots = :path_screenshots WHERE id = :id" + ), + { + "path_cover_s": path_cover_s, + "path_cover_l": path_cover_l, + "path_screenshots": path_screenshots, + "id": rom.id, + }, + ) # ### end Alembic commands ###