Skip to content

Commit

Permalink
fixed migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
zurdi15 committed Jul 3, 2024
1 parent 69ed831 commit b1b55f1
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 7 deletions.
12 changes: 5 additions & 7 deletions backend/alembic/versions/0019_resources_refactor.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,15 @@ 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:")
print(f"INFO:\t [Resources migration] - old: {old_folder_path}")
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)

Expand All @@ -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),
)

Expand Down
57 changes: 57 additions & 0 deletions backend/alembic/versions/0022_collections_.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 ###


Expand Down

0 comments on commit b1b55f1

Please sign in to comment.