import json import os import shutil import sqlite3 from dotenv import load_dotenv load_dotenv() remap = json.load(open(file="./Data_exported/Data_mods/x64/datatable/dec/remap.json", encoding="utf-8")) # 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, removes any rows where the column value is equal to the new ID to avoid duplicates, and updates the favorite_songs_array accordingly. 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(): # Step 1: Remove rows where column = new_id to avoid duplicates sql_delete_query = f"DELETE FROM {table} WHERE {column} = ?" cursor.execute(sql_delete_query, (new_id,)) if cursor.rowcount > 0: print(f"Removed {cursor.rowcount} entries in {table} where {column} = {new_id}") # Step 2: Update favorite_songs_array in UserData table (assumes column index 12) cursor.execute(f"SELECT rowid, * FROM UserData") # `rowid` lets us update specific rows user_data_rows = cursor.fetchall() for row in user_data_rows: row_id = row[0] favorite_songs_array = json.loads(row[13]) if row[13] else [] # Load favorite_songs_array from column 12 # Update the favorite_songs_array, removing any instances of new_id updated_favorite_songs = [song_id for song_id in favorite_songs_array if int(song_id) != new_id] # Check if the favorite_songs_array was actually changed if favorite_songs_array != updated_favorite_songs: # Convert back to JSON and update in the database updated_favorite_songs_json = json.dumps(updated_favorite_songs) cursor.execute( "UPDATE UserData SET FavoriteSongsArray = ? WHERE rowid = ?", (updated_favorite_songs_json, row_id) ) print(f'Updated FavoriteSongsArray for "{row[19]}": Removed song id {new_id}') # Step 3: Update the IDs in the specified column (e.g., main table update) sql_update_query = f"UPDATE {table} SET {column} = ? WHERE {column} = ?" 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 = True 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.")