Opened 9 years ago
Closed 9 years ago
#25833 closed New feature (fixed)
Add support for non-atomic migrations
Reported by: | Ludwig Hähne | Owned by: | nobody |
---|---|---|---|
Component: | Migrations | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Shai Berger | Triage Stage: | Accepted |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description (last modified by )
Database transactions are always wrapped in transactions (on DBs that support transactional DDL like Postgres). Generally this is a good idea.
However, one can't do batched updates in data migrations which is essential for performing changes on big tables on a live DB:
http://pankrat.github.io/2015/django-migrations-without-downtimes/
It's also not possible to create indexes concurrently on Postgres from inside a transaction.
Therefore, I'd like to have support for non-atomic migrations in Django because it's pretty messy to work around not having proper support for that:
http://stackoverflow.com/questions/31247810/commit-manually-in-django-data-migration
I propose exempting specific migrations from being wrapped in a transaction by setting atomic = False
on the migration:
https://github.com/django/django/compare/master...Ableton:non-atomic-migrations
This was already discussed on the Django developers mailing list:
https://groups.google.com/forum/#!topic/django-developers/aAYiyAqTlUc
I'll open a pull request for this new feature in a bit.
Change History (8)
comment:1 by , 9 years ago
Description: | modified (diff) |
---|
comment:2 by , 9 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:3 by , 9 years ago
comment:4 by , 9 years ago
If the migration doesn't apply without errors, it will not be marked as applied.
It's up to the user to handle those migration errors gracefully (e.g. skip already migrated rows when re-running a previously failed non-atomic migration).
Would it be enough to mention this behaviour in the non-atomic migration documentation?
AFAIU, non-atomic migrations have the same semantics as applying migrations on a database that doesn't support transactional DDL.
comment:6 by , 9 years ago
Patch needs improvement: | set |
---|---|
Summary: | Non-atomic migrations → Add support for non-atomic migrations |
Left some comments for improvement on the pull request.
comment:7 by , 9 years ago
Patch needs improvement: | unset |
---|
Tim's patch has been applied (thanks!), the tests have been improved and the PR has been rebased.
What happens when a non-atomic migrations fails halfway?
Your database is no longer in a state matching this migration, nor the previous one. The database will be left in an undefined, irrecoverable state.
There's no way to roll back from that either.