Skip to content

Instantly share code, notes, and snippets.

@sebleier
Created August 5, 2011 16:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sebleier/1127937 to your computer and use it in GitHub Desktop.
Save sebleier/1127937 to your computer and use it in GitHub Desktop.
This is how I kinda wished django migrations would work.
$ git checkout master
Already on 'master'
$ django-admin.py migrate
Error: Cannot migrate in a dirty state
$ git commit -am "Added migration to remove blog entry's pub_month and pub_day fields."
$ django-admin.py tag_migration add_column_to_foo # tags a migration identifier using git-tag
$ django-admin.py migrate
last known sha: c39168d21bb27754737a1036a50f57687cf6ae56
new migrations found between c39168..HEAD:
1) 6e0768 add_pub_date_field_to_blog_entry
2) 9ba9f3 migrate_blog_entry_month_and_day_to_pub_date
3) b6a0ac remove_blog_month_and_day_field
Running migrations...
[F] add_pub_date_field_to_blog_entry
[F] migrate_blog_entry_month_and_day_to_pub_date
[F] remove_blog_month_and_day_field
Ran 3 migrations c39168..HEAD
# Awesome, but now I have to work on this other branch with a different schema.
# This branch has its own migrations since it diverged with master.
$ git checkout feature/timestamped-entries
Switched to branch "feature/timestamped-entries"
# Migrate by finding the last migration in the database that is found by
# listing out the migration tags. Database will have information on the
# last run migrations. Using that, the migrator traverses back up the history
# until it finds a sha that matches what is in the database...this is the last
# know migration
$ django-admin.py migrate
last known sha: c39168d21bb27754737a1036a50f57687cf6ae56
new migrations found between c39168..HEAD:
1) b8c2a1 add_timestamp_field_to_blog_entry
2) 1be2c8 migrate_blog_entry_month_and_day_to_pub_date
3) dea198 remove_blog_month_and_day_field
Last migration run: remove_blog_month_and_day_field
# Traversing back to last know migration and then forward to new state.
[R] remove_blog_month_and_day_field
[R] migrate_blog_entry_month_and_day_to_pub_date
[R] add_pub_date_field_to_blog_entry
[F] add_timestamp_field_to_blog_entry
[F] migrate_blog_entry_month_and_day_to_pub_date
[F] remove_blog_month_and_day_field
@sebleier
Copy link
Author

sebleier commented Aug 5, 2011

This would eliminate the need for a frozen orm and allow use of model and manager methods. Also, no need to explicitly state dependencies because commit order enforces that for you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment