|
@@ -1,4 +1,4 @@
|
|
|
-from django.db import migrations
|
|
|
|
|
|
|
+from django.db import migrations, models
|
|
|
from django.apps import apps
|
|
from django.apps import apps
|
|
|
from django.contrib.contenttypes.models import ContentType
|
|
from django.contrib.contenttypes.models import ContentType
|
|
|
|
|
|
|
@@ -8,18 +8,17 @@ def disable_fk_checks(cursor):
|
|
|
def enable_fk_checks(cursor):
|
|
def enable_fk_checks(cursor):
|
|
|
cursor.execute("PRAGMA foreign_keys = ON;")
|
|
cursor.execute("PRAGMA foreign_keys = ON;")
|
|
|
|
|
|
|
|
-
|
|
|
|
|
-def copy_tags_raw_sql(cursor, old_table, old_id_column, new_item_id):
|
|
|
|
|
- # Get content_type_id for ScrobblableItem
|
|
|
|
|
- ct_id = ContentType.objects.get(app_label='scrobbles', model='scrobblableitem').id
|
|
|
|
|
-
|
|
|
|
|
- # Insert tags directly from old table to ObjectWithGenres
|
|
|
|
|
- cursor.execute(f"""
|
|
|
|
|
- INSERT INTO scrobbles_objectwithgenres (tag_id, content_type_id, object_id)
|
|
|
|
|
- SELECT tag_id, %s, %s
|
|
|
|
|
- FROM old_table_genres
|
|
|
|
|
- WHERE {old_id_column} = %s
|
|
|
|
|
- """, [ct_id, new_item_id, old_item_id])
|
|
|
|
|
|
|
+M2M_TABLES = {
|
|
|
|
|
+ "beers": [("beers_beer_beerstyles", "beer_id")],
|
|
|
|
|
+ "music": [("music_track_albums", "track_id")],
|
|
|
|
|
+ "boardgames": [
|
|
|
|
|
+ ("boardgames_boardgame_publishers", "board_game_id"),
|
|
|
|
|
+ ("boardgames_boardgame_designers", "board_game_id"),
|
|
|
|
|
+ ],
|
|
|
|
|
+ "videogames": [
|
|
|
|
|
+ ("videogames_videogame_platforms", "video_game_id")
|
|
|
|
|
+ ],
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
def get_table_columns(cursor, table_name, vendor):
|
|
def get_table_columns(cursor, table_name, vendor):
|
|
|
"""
|
|
"""
|
|
@@ -93,27 +92,44 @@ def create_and_copy_data(apps, schema_editor, lookup_keys, old_to_new_mappings):
|
|
|
description=data.get("description", "") or "",
|
|
description=data.get("description", "") or "",
|
|
|
base_run_time_seconds=data.get("base_run_time_seconds"),
|
|
base_run_time_seconds=data.get("base_run_time_seconds"),
|
|
|
)
|
|
)
|
|
|
- base.created = data.get("created")
|
|
|
|
|
- base.modified = data.get("modified")
|
|
|
|
|
- base.save(update_fields=["created", "modified"])
|
|
|
|
|
|
|
+ #base.created = data.get("created")
|
|
|
|
|
+ #base.modified = data.get("modified")
|
|
|
|
|
+ #base.save(update_fields=["created", "modified"])
|
|
|
|
|
|
|
|
mapping[old_id] = base.id
|
|
mapping[old_id] = base.id
|
|
|
|
|
|
|
|
# -----------------------
|
|
# -----------------------
|
|
|
# 3. Update media table FK
|
|
# 3. Update media table FK
|
|
|
# -----------------------
|
|
# -----------------------
|
|
|
- cursor.execute(
|
|
|
|
|
- f"UPDATE {table} SET scrobblableitem_ptr_id = %s WHERE id = %s",
|
|
|
|
|
- [base.id, old_id],
|
|
|
|
|
- )
|
|
|
|
|
-
|
|
|
|
|
- # -----------------------
|
|
|
|
|
- # 4. Update scrobbles FK
|
|
|
|
|
- # -----------------------
|
|
|
|
|
- cursor.execute(
|
|
|
|
|
- f"UPDATE {scrobble_table} SET item_id = %s WHERE {model_id_field} = %s",
|
|
|
|
|
- [base.id, old_id],
|
|
|
|
|
- )
|
|
|
|
|
|
|
+ if app_label in M2M_TABLES:
|
|
|
|
|
+ for table_and_key in M2M_TABLES.get(app_label, []):
|
|
|
|
|
+ table, key_id = table_and_key
|
|
|
|
|
+ print(f"Updating M2M values for {table}: changing {old_id} to {base.id}")
|
|
|
|
|
+ cursor.execute(
|
|
|
|
|
+ f"UPDATE {table} SET {key_id} = %s WHERE {key_id} = %s", [base.id, old_id]
|
|
|
|
|
+ )
|
|
|
|
|
+ else:
|
|
|
|
|
+ cursor.execute(
|
|
|
|
|
+ f"UPDATE {table} SET scrobblableitem_ptr_id = %s WHERE id = %s",
|
|
|
|
|
+ [base.id, old_id],
|
|
|
|
|
+ )
|
|
|
|
|
+
|
|
|
|
|
+ # -----------------------
|
|
|
|
|
+ # 4. Update scrobbles FK
|
|
|
|
|
+ # -----------------------
|
|
|
|
|
+ cursor.execute(
|
|
|
|
|
+ f"UPDATE {scrobble_table} SET item_id = %s WHERE {model_id_field} = %s",
|
|
|
|
|
+ [base.id, old_id],
|
|
|
|
|
+ )
|
|
|
|
|
+
|
|
|
|
|
+ # M2M
|
|
|
|
|
+ #if app_label in M2M_TABLES:
|
|
|
|
|
+ # for table_and_key in M2M_TABLES.get(app_label):
|
|
|
|
|
+ # table, key = table_and_key
|
|
|
|
|
+ # print(f"Updating M2M for {table}")
|
|
|
|
|
+ # cursor.execute(
|
|
|
|
|
+ # f"UPDATE {table} SET {key} = %s WHERE {key} = %s", [base.id, old_id]
|
|
|
|
|
+ # )
|
|
|
|
|
|
|
|
|
|
|
|
|
# -----------------------
|
|
# -----------------------
|
|
@@ -123,11 +139,11 @@ def create_and_copy_data(apps, schema_editor, lookup_keys, old_to_new_mappings):
|
|
|
ct_id = ContentType.objects.get(app_label=app_label, model=model_name.lower()).id
|
|
ct_id = ContentType.objects.get(app_label=app_label, model=model_name.lower()).id
|
|
|
|
|
|
|
|
# Only copy if the old table has tags
|
|
# Only copy if the old table has tags
|
|
|
- cursor.execute(f"""
|
|
|
|
|
|
|
+ cursor.execute("""
|
|
|
UPDATE scrobbles_objectwithgenres SET object_id = %s
|
|
UPDATE scrobbles_objectwithgenres SET object_id = %s
|
|
|
WHERE content_type_id = %s AND object_id = %s
|
|
WHERE content_type_id = %s AND object_id = %s
|
|
|
""", [base.id, ct_id, old_id])
|
|
""", [base.id, ct_id, old_id])
|
|
|
- except:
|
|
|
|
|
|
|
+ except Exception:
|
|
|
print(f"No content type for {app_label}.{model_name}")
|
|
print(f"No content type for {app_label}.{model_name}")
|
|
|
|
|
|
|
|
print(f" Finished migrating {app_label}.{model_name}")
|
|
print(f" Finished migrating {app_label}.{model_name}")
|
|
@@ -136,7 +152,6 @@ def reassign_foreign_keys(apps, schema_editor, old_to_new_mappings):
|
|
|
"""
|
|
"""
|
|
|
Detect all FKs pointing to old media tables and reassign to new ScrobblableItem.
|
|
Detect all FKs pointing to old media tables and reassign to new ScrobblableItem.
|
|
|
"""
|
|
"""
|
|
|
- print(old_to_new_mappings)
|
|
|
|
|
db = schema_editor.connection
|
|
db = schema_editor.connection
|
|
|
cursor = db.cursor()
|
|
cursor = db.cursor()
|
|
|
|
|
|
|
@@ -156,18 +171,6 @@ def reassign_foreign_keys(apps, schema_editor, old_to_new_mappings):
|
|
|
for old_id, new_id in mapping.items():
|
|
for old_id, new_id in mapping.items():
|
|
|
cursor.execute(f"UPDATE {table} SET {fk_column} = %s WHERE {fk_column} = %s",
|
|
cursor.execute(f"UPDATE {table} SET {fk_column} = %s WHERE {fk_column} = %s",
|
|
|
[new_id, old_id])
|
|
[new_id, old_id])
|
|
|
-
|
|
|
|
|
- # ManyToMany FKs
|
|
|
|
|
- if field.many_to_many and not field.auto_created:
|
|
|
|
|
- through_table = field.remote_field.through._meta.db_table
|
|
|
|
|
- from_col = field.m2m_column_name()
|
|
|
|
|
- to_model = field.remote_field.model
|
|
|
|
|
- if to_model in old_to_new_mappings:
|
|
|
|
|
- mapping = old_to_new_mappings[to_model]
|
|
|
|
|
- print(f"Updating M2M {through_table}.{from_col} → ScrobblableItem")
|
|
|
|
|
- for old_id, new_id in mapping.items():
|
|
|
|
|
- cursor.execute(f"UPDATE {through_table} SET {field.m2m_reverse_name()} = %s WHERE {field.m2m_reverse_name()} = %s",
|
|
|
|
|
- [new_id, old_id])
|
|
|
|
|
enable_fk_checks(cursor)
|
|
enable_fk_checks(cursor)
|
|
|
|
|
|
|
|
def backfill_media(apps, schema_editor):
|
|
def backfill_media(apps, schema_editor):
|