Taiko/tooling/remapToOriginal.py
2025-01-20 23:20:26 +01:00

75 lines
2.2 KiB
Python

import json
import os
import shutil
import sqlite3
from dotenv import load_dotenv
load_dotenv()
remap = json.load(open(file="../Migration & Backup/CHN to 39.06/unalteredRemap.json", encoding="utf-8"))
os.makedirs("./temp/", exist_ok=True)
os.makedirs("./Data_exported/Server/wwwroot/data/datatable", exist_ok=True)
os.makedirs("./Data_exported/Data_mods/x64/datatable/dec/", exist_ok=True)
shutil.copy2(
"../Migration & Backup/CHN to 39.06/unaltered" + os.getenv("DBNAME"),
"./Data_exported/Server/wwwroot/taiko.db3",
)
# Connect to the database
conn = sqlite3.connect("./Data_exported/Server/wwwroot/taiko.db3")
cursor = conn.cursor()
def update_ids_in_table(cursor, table, column, id_mappings):
"""
Updates the IDs in a specific table and column based on the provided id_mappings.
Args:
cursor: SQLite cursor object.
table: Name of the table to update.
column: Name of the column to update.
id_mappings: Dictionary with old IDs as keys and new IDs as values.
"""
for old_id, new_id in id_mappings.items():
# SQL query to update the IDs
sql_update_query = f"UPDATE {table} SET {column} = ? WHERE {column} = ?"
# Execute the update query
cursor.execute(sql_update_query, (new_id, old_id))
print(f"Updated {table}.{column} from {old_id} to {new_id}")
# List of tables and their corresponding columns to update
tables_to_update = {
"AiScoreData": "SongId",
"AiSectionScoreData": "SongId",
"SongBestData": "SongId",
"SongPlayData": "SongId",
}
invert = False
id_mappings = {}
for remapentry in remap:
RemapFrom = remapentry["uniqueIdOriginal" if invert else "uniqueIdRemap"]
RemapTo = remapentry["uniqueIdRemap" if invert else "uniqueIdOriginal"]
id_mappings[RemapFrom] = RemapTo
try:
# Iterate over each table and update the IDs
for table, column in tables_to_update.items():
update_ids_in_table(cursor, table, column, id_mappings)
# Commit the changes to the database
conn.commit()
except sqlite3.Error as e:
print(f"An error occurred: {e}")
conn.rollback() # Rollback changes if something goes wrong
finally:
# Close the database connection
conn.close()
print("Database connection closed.")