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

Create triggers for tables in unmigrated apps when run_syncdb is specified #113

Closed
wants to merge 1 commit into from

Conversation

danifus
Copy link
Contributor

@danifus danifus commented Nov 9, 2022

Implements #112

It creates the triggers in DatabaseSchemaEditorMixin.create_model by creating any triggers defined in the models Meta.triggers attribute. The triggers should only be created here if we're doing a 'run_syncdb' operation on an unmigrated app. create_model is also called during a CreateTable migration operation too, but given the logic in MigrationAutodetectorMixin above, the initial state of the table used in the CreateTable operation won't have any triggers attribute yet. The triggers attibute only gets added to the model's migration state with an AddTrigger operation after the initial CreateTable operation.

@wesleykendall
Copy link
Member

@danifus thanks so much! This is a great enhancement. Also thanks for fixing the bug that you found with the migrations.

I put a reminder to come back to this PR this weekend to see if I can get it in. I'm assuming that you have a local project that uses syncdb for tests and tried it out locally?

Thanks for all of the tests. I may need to push a few things to this branch since I just recently made some changes to CI (psycopg3 testing). But I should be good to release this and test it out more

@danifus
Copy link
Contributor Author

danifus commented Jun 9, 2023

Great! Thanks!

Yeah, I was working on a library package and it was a bit easier to iterate while developing without having to recreate the migrations between tests after updating the triggers. Happy to make any changes to the PR to make it compatible with your psycopg3 changes.

… create missing triggers

Django can create tables without migrations if an app is left
'unmigrated' (doesn't have a migrations package in the app) and the
run_syncdb option is provided to the migrate command. This change allows
triggers to be created in this manner.

We override `DatabaseSchemaEditorMixin.create_model()` to detect missing
triggers from one migration state to the next (or reverse if
unapplying), and add them to the database if they're missing.

This also has the side effect of recreating triggers when a migration
that removed some triggers is unapplied, the triggers are now recreated.
@wesleykendall
Copy link
Member

Implemented these fixes with #130 and ensured you were the commit author. Thanks for these fixes! Will post back on the issues when they are deployed

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