Software Development |
Ruby on Rails
auto_migrations plugin
GitHub: auto_migrations (pjhyett) is one of the plugins used in altered_beast.
altered_beast uses auto_migrations to initially set up the database then has normal migrations to extend it.
How normal migrations work
With normal migrations you write ruby code to alter the database incrementally from version to version. And each migration comes with a roll back option. The combination gives you the ability to roll the database scheme forward and back with fairly fine grained control.
Applying migrations is a rake task and you can just bring the database up to date or take it to a specific version:
db:migrate db:migrate VERSION=18
Having a single view of the overall schema in one place is useful and Rails gives us this by maintaining the current schema in one file (db/schema.rb). This is meant to be read only because the migrations update this file.
How auto_migrations changes this
In a sense auto_migrations reverses the normal approach. With auto_migrations she schema (db/schema.rb) is used to drive the database so this file is now read/write. Auto_migrations compares db/schema.rb to the database and identifies the differences.
Applying the changes is a rake task like normal migrations:
rake db:auto:migrate
I haven't explored it but I guess a roll back would involve putting an older version of db/schema.rb back and running db:auto:migrate again.
Conclusion
Auto-migrations seems a sensible approach and if auto_migrations were the Rails convention then it would be fine. But this isn't the case and the difference from normal migrations is confusing when you first bump into it. I don't think you can comfortably mix and match regular migrations with auto-migrations although altered_beast's approach, of using auto-migrations to initially set up the database then normal migrations to extend it, does work. But you end up using normal migrations which begs the question of why bother with auto_migrations in the first place. The magic of the auto-migrations could have been done with a single normal migration that just loaded all the tables. This would have been easier and more obvious.
Rating: Clever but why bother?
