feat: add events app + "multi-tenants" aka multi-events
This commit is contained in:
@@ -3,45 +3,31 @@
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
TABLE_PAIRS = [
|
||||
("submissions_steamapikey", "opus_magnum_steamapikey"),
|
||||
("submissions_steamcollection", "opus_magnum_steamcollection"),
|
||||
("submissions_steamcollectionitem", "opus_magnum_steamcollectionitem"),
|
||||
("submissions_submission", "opus_magnum_submission"),
|
||||
("submissions_puzzleresponse", "opus_magnum_puzzleresponse"),
|
||||
("submissions_submissionfile", "opus_magnum_submissionfile"),
|
||||
]
|
||||
|
||||
|
||||
def migrate_data_forward(apps, schema_editor):
|
||||
"""Copy data from submissions_* tables to opus_magnum_* tables"""
|
||||
# Use raw SQL to copy data while preserving all fields
|
||||
"""Copy data from submissions_* tables to opus_magnum_* tables.
|
||||
|
||||
The source tables only exist on databases that predate the submissions ->
|
||||
opus_magnum rename; a database built fresh from this migration history
|
||||
never has them, so skip pairs whose source table is absent instead of
|
||||
failing.
|
||||
"""
|
||||
existing_tables = set(schema_editor.connection.introspection.table_names())
|
||||
|
||||
with schema_editor.connection.cursor() as cursor:
|
||||
# Copy SteamAPIKey
|
||||
cursor.execute("""
|
||||
INSERT INTO opus_magnum_steamapikey
|
||||
SELECT * FROM submissions_steamapikey
|
||||
""")
|
||||
|
||||
# Copy SteamCollection
|
||||
cursor.execute("""
|
||||
INSERT INTO opus_magnum_steamcollection
|
||||
SELECT * FROM submissions_steamcollection
|
||||
""")
|
||||
|
||||
# Copy SteamCollectionItem
|
||||
cursor.execute("""
|
||||
INSERT INTO opus_magnum_steamcollectionitem
|
||||
SELECT * FROM submissions_steamcollectionitem
|
||||
""")
|
||||
|
||||
# Copy Submission
|
||||
cursor.execute("""
|
||||
INSERT INTO opus_magnum_submission
|
||||
SELECT * FROM submissions_submission
|
||||
""")
|
||||
|
||||
# Copy PuzzleResponse
|
||||
cursor.execute("""
|
||||
INSERT INTO opus_magnum_puzzleresponse
|
||||
SELECT * FROM submissions_puzzleresponse
|
||||
""")
|
||||
|
||||
# Copy SubmissionFile
|
||||
cursor.execute("""
|
||||
INSERT INTO opus_magnum_submissionfile
|
||||
SELECT * FROM submissions_submissionfile
|
||||
""")
|
||||
for source, dest in TABLE_PAIRS:
|
||||
if source not in existing_tables:
|
||||
continue
|
||||
cursor.execute(f"INSERT INTO {dest} SELECT * FROM {source}")
|
||||
|
||||
|
||||
def migrate_data_backward(apps, schema_editor):
|
||||
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
# Generated by Django 6.0.5 on 2026-09-15 21:09
|
||||
|
||||
import django.db.models.deletion
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
dependencies = [
|
||||
("events", "0002_seed_polylan_43"),
|
||||
("opus_magnum", "0015_migrate_data_from_submissions"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name="steamcollection",
|
||||
name="event",
|
||||
field=models.ForeignKey(
|
||||
null=True,
|
||||
on_delete=django.db.models.deletion.CASCADE,
|
||||
related_name="steam_collections",
|
||||
to="events.event",
|
||||
),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name="submission",
|
||||
name="event",
|
||||
field=models.ForeignKey(
|
||||
null=True,
|
||||
on_delete=django.db.models.deletion.CASCADE,
|
||||
related_name="opus_magnum_submissions",
|
||||
to="events.event",
|
||||
),
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,27 @@
|
||||
from django.db import migrations
|
||||
|
||||
EVENT_PATH = "polylan-43"
|
||||
|
||||
|
||||
def backfill_event(apps, schema_editor):
|
||||
Event = apps.get_model("events", "Event")
|
||||
SteamCollection = apps.get_model("opus_magnum", "SteamCollection")
|
||||
Submission = apps.get_model("opus_magnum", "Submission")
|
||||
event = Event.objects.get(path=EVENT_PATH)
|
||||
SteamCollection.objects.filter(event__isnull=True).update(event=event)
|
||||
Submission.objects.filter(event__isnull=True).update(event=event)
|
||||
|
||||
|
||||
def noop(apps, schema_editor):
|
||||
pass
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
dependencies = [
|
||||
("opus_magnum", "0016_steamcollection_event_submission_event"),
|
||||
("events", "0002_seed_polylan_43"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RunPython(backfill_event, noop),
|
||||
]
|
||||
@@ -0,0 +1,29 @@
|
||||
import django.db.models.deletion
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
dependencies = [
|
||||
("opus_magnum", "0017_backfill_event"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name="steamcollection",
|
||||
name="event",
|
||||
field=models.ForeignKey(
|
||||
on_delete=django.db.models.deletion.CASCADE,
|
||||
related_name="steam_collections",
|
||||
to="events.event",
|
||||
),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name="submission",
|
||||
name="event",
|
||||
field=models.ForeignKey(
|
||||
on_delete=django.db.models.deletion.CASCADE,
|
||||
related_name="opus_magnum_submissions",
|
||||
to="events.event",
|
||||
),
|
||||
),
|
||||
]
|
||||
Reference in New Issue
Block a user