Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issues updating triggers in non-atomic migrations #137

Merged
merged 1 commit into from
Nov 23, 2023

Conversation

wesleykendall
Copy link
Member

Fields of trigger conditions can now have their types updated in non-atomic migrations.

Type: bug

@kekekekule I wanted to quickly address the issue at #136 and fix some things in your PR at #136. I thought it would be easiest to get this in another PR and deploy since I'm going to be away for a bit. I made sure you are the author of the commit, so you will get credit in the release. I also added you to the README as a contributor. Thanks for your contributions!

I believe that this individual operation still needs to be atomic, even if it's in a non-atomic migration. It's dangerous to drop a trigger outside of a transaction as it could lead to data loss for django-pghistory users and other use cases where triggers are necessary to be installed for data integrity.

I updated the code to still wrap the individual schema change in a transaction, ensuring that the trigger is dropped and re-created in that transaction. So, if your migration looks like this:

class Migration(migrations.Migration):
    atomic = False

    operations = [
        migrations.AlterField(), 
        migrations.AlterField(),
    ]

The separate alter statements won't be wrapped in a transaction. However, an individual alter statement may be temporarily wrapped in a transaction so that the trigger may be able to be safely dropped and re-created while the field is being altered.

I hope that makes sense. I made a failing test case first similar to the one you made, and I just used the normal transaction.atomic. I was incorrectly using a self.atomic utility in the schema editor.

If for any reason this doesn't address your use case, please let me know. I'm happy to revisit it. For now I plan to deploy this as the fix

Fields of trigger conditions can now have their types updated in non-atomic migrations.

Type: bug
@wesleykendall wesleykendall merged commit 8f52a80 into master Nov 23, 2023
@wesleykendall wesleykendall deleted the fix-non-atomic-migrations branch November 23, 2023 02:37
@wesleykendall
Copy link
Member Author

Deployed in 4.10.0

@kekekekule
Copy link
Contributor

Thank you! I missed the fact that DatabaseSchemaEditor is dealing with not with a whole migration, but with a specific operation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants