62 lines
2.1 KiB
Python
62 lines
2.1 KiB
Python
# Generated by Django 5.1.3 on 2025-01-19 20:29
|
|
|
|
from django.db import connection, migrations, models
|
|
|
|
|
|
def recreate_games(apps, schema_editor):
|
|
Edition = apps.get_model("games", "Edition")
|
|
Game = apps.get_model("games", "Game")
|
|
Purchase = apps.get_model("games", "Purchase")
|
|
|
|
with connection.cursor() as cursor:
|
|
print("Create table games_gametemp")
|
|
cursor.execute(
|
|
"CREATE TABLE games_gametemp AS SELECT * FROM games_game WHERE 1=0;"
|
|
)
|
|
|
|
for edition in Edition.objects.all():
|
|
print(f"Re-create edition with ID {edition.id}")
|
|
cursor.execute(
|
|
"""
|
|
INSERT INTO games_gametemp (
|
|
id, name, sort_name, year_released, platform_id, wikidata, created_at
|
|
)
|
|
VALUES (%s, %s, %s, %s, %s, %s, %s)
|
|
""",
|
|
[
|
|
edition.id, # Reuse the Edition ID
|
|
edition.name,
|
|
edition.sort_name,
|
|
edition.year_released,
|
|
edition.platform_id,
|
|
Game.objects.get(id=edition.game.id).wikidata,
|
|
edition.created_at,
|
|
],
|
|
)
|
|
print("Turn foreign keys off")
|
|
cursor.execute("PRAGMA foreign_keys = OFF;")
|
|
print("Drop table games_game")
|
|
cursor.execute("DROP TABLE games_game;")
|
|
print("Drop table games_edition")
|
|
cursor.execute("DROP TABLE games_edition;")
|
|
print("Rename table games_gametemp to games_game")
|
|
# cursor.execute("ALTER TABLE games_gametemp RENAME TO games_game;")
|
|
cursor.execute("CREATE TABLE games_game AS SELECT * FROM games_gametemp;")
|
|
|
|
|
|
class Migration(migrations.Migration):
|
|
dependencies = [
|
|
("games", "0046_game_platform_alter_game_unique_together"),
|
|
]
|
|
|
|
operations = [
|
|
migrations.RunPython(recreate_games),
|
|
migrations.AlterField(
|
|
model_name="purchase",
|
|
name="editions",
|
|
field=models.ManyToManyField(
|
|
blank=True, related_name="purchases", to="games.game"
|
|
),
|
|
),
|
|
]
|