From b8839302c0a4d8ada99a695f8829027fa433e05e Mon Sep 17 00:00:00 2001 From: Mateusz Mandera Date: Fri, 5 Jul 2019 00:50:38 +0200 Subject: [PATCH] retention: Fix migration making archive_transaction field not null. DELETing from archive tables and ALTERing ArchivedMessage needs to be split into separate transactions. zerver_archivedattachment_messages needs to be cleared out before zerver_archivedattachment. --- ..._archive_transaction_field_not_nullable.py | 25 +++++++++++++------ 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/zerver/migrations/0232_make_archive_transaction_field_not_nullable.py b/zerver/migrations/0232_make_archive_transaction_field_not_nullable.py index 43f1673a01..2891637705 100644 --- a/zerver/migrations/0232_make_archive_transaction_field_not_nullable.py +++ b/zerver/migrations/0232_make_archive_transaction_field_not_nullable.py @@ -4,21 +4,30 @@ from __future__ import unicode_literals from django.db import migrations, models import django.db.models.deletion - class Migration(migrations.Migration): + """ + Tables cannot have data deleted from them and be altered in a single transaction, + but we need the DELETEs to be atomic together. So we set atomic=False for the migration + in general, and run the DELETEs in one transaction, and AlterField in another. + """ + atomic = False dependencies = [ ('zerver', '0231_add_archive_transaction_model'), ] operations = [ - migrations.RunSQL("DELETE FROM zerver_archivedusermessage"), - migrations.RunSQL("DELETE FROM zerver_archivedreaction"), - migrations.RunSQL("DELETE FROM zerver_archivedsubmessage"), - migrations.RunSQL("DELETE FROM zerver_archivedattachment"), - migrations.RunSQL("DELETE FROM zerver_archivedattachment_messages"), - migrations.RunSQL("DELETE FROM zerver_archivedmessage"), - migrations.RunSQL("DELETE FROM zerver_archivetransaction"), + migrations.RunSQL(""" + BEGIN; + DELETE FROM zerver_archivedusermessage; + DELETE FROM zerver_archivedreaction; + DELETE FROM zerver_archivedsubmessage; + DELETE FROM zerver_archivedattachment_messages; + DELETE FROM zerver_archivedattachment; + DELETE FROM zerver_archivedmessage; + DELETE FROM zerver_archivetransaction; + COMMIT; + """), migrations.AlterField( model_name='archivedmessage', name='archive_transaction',