repair legacy table in migration
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
# Before the submissions -> opus_magnum rename, this app owned the opus magnum
|
||||
# tables and its 0001_initial was already applied. The new 0001_initial has the
|
||||
# same name, so on those databases Django skipped it and kept the legacy
|
||||
# submissions_submission / submissions_submissionfile layout (is_validated NOT
|
||||
# NULL, response_id, ...) and never created submissions_imagecrop.
|
||||
LEGACY_TABLES = [
|
||||
"submissions_imagecrop",
|
||||
"submissions_submissionfile",
|
||||
"submissions_puzzleresponse",
|
||||
"submissions_submission",
|
||||
"submissions_steamcollectionitem",
|
||||
"submissions_steamcollection",
|
||||
"submissions_steamapikey",
|
||||
]
|
||||
|
||||
|
||||
def repair_legacy_tables(apps, schema_editor):
|
||||
connection = schema_editor.connection
|
||||
|
||||
with connection.cursor() as cursor:
|
||||
columns = {
|
||||
col.name
|
||||
for col in connection.introspection.get_table_description(
|
||||
cursor, "submissions_submission"
|
||||
)
|
||||
}
|
||||
|
||||
if "is_validated" not in columns:
|
||||
return
|
||||
|
||||
# Data was copied to opus_magnum (0015) and 0007 wiped submissions, so
|
||||
# this should be empty: refuse to drop anything otherwise.
|
||||
cursor.execute("SELECT COUNT(*) FROM submissions_submission")
|
||||
if cursor.fetchone()[0]:
|
||||
raise RuntimeError(
|
||||
"submissions_submission has the legacy layout but is not empty"
|
||||
)
|
||||
|
||||
existing_tables = set(connection.introspection.table_names())
|
||||
for table in LEGACY_TABLES:
|
||||
if table in existing_tables:
|
||||
schema_editor.execute(f"DROP TABLE {schema_editor.quote_name(table)}")
|
||||
|
||||
for model_name in ["Submission", "SubmissionFile", "ImageCrop"]:
|
||||
schema_editor.create_model(apps.get_model("submissions", model_name))
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
dependencies = [
|
||||
("submissions", "0008_remove_submission_game"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RunPython(repair_legacy_tables, migrations.RunPython.noop),
|
||||
]
|
||||
Reference in New Issue
Block a user