Migrations are great when you’re working on an application that’s already in production, but when you’re in the early stages of development and your database schema is constantly changing, it can get irritating and unwieldy to keep generating migration files. But now, there’s a better way…
Auto migrations is a plugin that will make changing your database even quicker and easier than using normal migrations. It’s intended to be used in the early stages of your application development, when your database schema is fluid and constantly being changed. And best of all, it’s one of my favourite kind of plugins because you can remove it at any time and it won’t affect your application. Let’s take a look…
Installation
First, let’s install the auto_migrations plugin…
ruby script/plugin install svn://errtheblog.com/svn/plugins/auto_migrations
This should export it into your vendor/plugins directory and provide you with two new rake tasks: rake db:auto:migrate and rake db:schema:to_migration.
schema.rb file
Now let’s take a look in the db directory of your Rails application, where you’ll find a file named schema.rb. This is a little bit like a consolidated migrations file, and contains the migrations syntax for your complete database schema…
create_table "users", :force => true do |t|
t.column "name", :string
t.column "hashed_password", :string
t.column "salt", :string
t.column "firstname", :string
t.column "surname", :string
t.column "email", :string
end
create_table "companies", :force => true do |t|
t.column "name", :string, :limit => 40
t.column "description", :string
end
What the auto_migrations plugin does, is allow you to edit your schema.rb file directly, and then run rake db:auto:migrate to have the changes applied automatically. That’s right: there’s no need to generate and edit migrations files, just edit schema.rb directly and the plugin will detect the changes you’ve made as soon as you run rake db:auto:migrate.
Example
In the above example schema.rb file, we have a companies table…
create_table "companies", :force => true do |t|
t.column "name", :string, :limit => 40
t.column "description", :string
end
Let’s imagine that we want to add a new column–a string labelled email with a limit of 60 characters–to the companies table. We just edit schema.rb and insert an extra t.column line:
create_table "companies", :force => true do |t|
t.column "name", :string, :limit => 40
t.column "description", :string
t.column "email", :string, :limit => 60
end
Now, when we run rake db:auto:migrate, the email column will be added. Just run rake:
rake db:auto:migrate
And you’ll see the new column added:
-- add_column("companies", "email", :string)
-> 0.0216s
Now is that magic, or is that magic? It works if you delete columns too. Just delete the line containing the column from the schema.rb file:
create_table "users", :force => true do |t|
t.column "name", :string
t.column "hashed_password", :string
t.column "salt", :string
t.column "firstname", :string
t.column "surname", :string
t.column "email", :string
end
Let’s remove the surname column:
create_table "users", :force => true do |t|
t.column "name", :string
t.column "hashed_password", :string
t.column "salt", :string
t.column "firstname", :string
t.column "email", :string
end
Then run rake:
rake db:auto:migrate
And hey presto:
-- remove_column("users", "surname")
-> 0.1250s
Limitations
Because auto_migrations makes several assumptions, there a few things it cannot do, namely changing the name of columns: it would simply drop the existing column and then create a new one with a new name. However, it will notice all of the following changes to schema.rb:
- creating and dropping tables
- creating and removing columns
- changing the types of existing columns, for example from
:stringto:integer - adding and removing indexes
db:schema:to_migration
And finally, when you want to start working with traditional migrations, you can get auto_migrations to create an initial migrations file based on the current schema.rb. Just run…
rake db:schema:to_migration
And it’ll create a 001_initial_schema.rb migration file in your db/migrate drectory. Fantastic!








ABOUT 1 YEAR AGO
len
Fantastic!
This is perfect! Just what I was needing right now! You are my hero.
3 MONTHS AGO
Jack
the plugin won't install
this looks great but I cannot get the plugin to install. The host seems to be blocking the download.
2 MONTHS AGO
cpr
git plugin location
http://mojodna.com/pjhyett/auto_migrations/commits/gh-pages
2 MONTHS AGO
Millisami
conflicting migration
Well, I used this plugin from GitHub and it works but with few error like below: =========================================== -- add_column("voices", :published, :integer, {:scale=>nil, :precision=>nil, :default=>0, :limit=>4}) -> 2.5290s rake aborted! Mysql::Error: Duplicate entry '20090421111610' for key 1: INSERT INTO schema_migrations VALUES ('20090421111610') =========================================== And when I delete the entry '2009042111610' from the schema_migration table and run the command, the error won't appear until the next rake db:auto:migrate command. And one more thing, if I run the command rake db:schema:to_migration, it will create one schema file named 001_migration_file?? Is this the way it will generate the migration? Any help would be appreciated!!