Initial commit
This commit is contained in:
@ -7,11 +7,7 @@ def migrate_edition_to_editions_temp(apps, schema_editor):
|
||||
Purchase = apps.get_model("games", "Purchase")
|
||||
for purchase in Purchase.objects.all():
|
||||
if purchase.edition:
|
||||
print(
|
||||
f"Migrating Purchase {purchase.id} with Edition {purchase.edition.id}"
|
||||
)
|
||||
purchase.editions_temp.add(purchase.edition)
|
||||
print(purchase.editions_temp.all())
|
||||
purchase.save()
|
||||
else:
|
||||
print(f"No edition found for Purchase {purchase.id}")
|
||||
|
@ -0,0 +1,28 @@
|
||||
# Generated by Django 5.1.3 on 2025-01-08 20:06
|
||||
|
||||
import django.db.models.deletion
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
dependencies = [
|
||||
("games", "0045_alter_purchase_editions"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name="game",
|
||||
name="platform",
|
||||
field=models.ForeignKey(
|
||||
blank=True,
|
||||
default=None,
|
||||
null=True,
|
||||
on_delete=django.db.models.deletion.SET_DEFAULT,
|
||||
to="games.platform",
|
||||
),
|
||||
),
|
||||
migrations.AlterUniqueTogether(
|
||||
name="game",
|
||||
unique_together={("name", "platform", "year_released")},
|
||||
),
|
||||
]
|
61
games/migrations/0047_auto_20250119_2129.py
Normal file
61
games/migrations/0047_auto_20250119_2129.py
Normal file
@ -0,0 +1,61 @@
|
||||
# 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"
|
||||
),
|
||||
),
|
||||
]
|
Reference in New Issue
Block a user