You are here: Home Solidarrow Ruby on Rails Solidarrow Migrations

Auto migrations

A brilliant plugin that makes changing your database schema even easier and faster.

 

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 :string to :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!

 

* Comments

4 MONTHS AGO

Fantastic!

This is perfect! Just what I was needing right now! You are my hero.

 

> RELATED ARTICLE

* Rails Migrations Cheatsheet

Up and down methods, rake tasks, database mapping, loading fixtures, example migration file, all packed onto one A4 page for an easy at-a-glance reference > More

 

> RELATED ARTICLE

* New in Rails 2.0: Sexy Migrations

A look at the new features and syntax available in migrations on Rails 2.0 > More