ActiveRecord Migrations Without Rails

Continuing exploring ActiveRecord without Rails, I thought I’d look into using Migrations without Rails.

Once again, why?

  • You already are using ActiveRecord without Rails
  • Versionable instances of how your database looked like
  • Database agnostic
  • You forget how to crack out SQL code (happens to me, maybe too much ORM)
  • You want migrations in non Ruby Projects
  • boredom

One thing I’d like to emphasize is that you don’t have to use Migrations for Ruby Projects. You can use them in non Ruby projects as well. Just use them to create a versioned of the database.

By using Migrations, your (Ruby and non Ruby) developers will appreciate that they can easily see what changes were made to the database.

Note:

  • I won’t go into getting ActiveRecord working without Rails, please refer to that post. It’s dead simple.
  • All these commands are executed from the root directory of your non-rails application.

Creating the Migrations Directory

First we need a directory where the migrations will be stored, in this case I’ll use the name ‘db/migrate File

mkdir -p db/migrate

Now we’ll create our first migration, and save it as db/migrate/001_create_users.rb File:

Wonderful

Warning:

  • The numbers prefixed to the migration file’s name (in this case 001) is very important. DO NOT OMMIT THIS.
  • Notice the class name is CreateUsers, this should correspond to a camel case representation of the filename.

Setting up the Rakefile

Now we’ll need a Rakefile. If your not familiar with Rakefiles just create a file called: Rakefile File

Note: The :environment task sets up the ActiveRecord configurations, configure as appropriate/desired

Your First Migration

Executing the migration is dead easy:

rake

If you chose to log the output you’d get something like this:

  SQL (0.004480)   CREATE TABLE schema_info (version integer)
  SQL (0.001514)   INSERT INTO schema_info (version) VALUES(0)
  SQL (0.000000)   SQLite::Exceptions::SQLException: table schema_info already exists: CREATE TABLE schema_info (version integer)
  SQL (0.000246)   SELECT version FROM schema_info
Migrating to CreateUsers (1)
== CreateUsers: migrating =====================================================
-- create_table(:users)
  SQL (0.002094)   CREATE TABLE users ("id" INTEGER PRIMARY KEY NOT NULL, "name" varchar(255) NOT NULL)
   -> 0.0026s
== CreateUsers: migrated (0.0028s) ============================================

  SQL (0.001499)   UPDATE schema_info SET version = 1

Perfect

Note: I am using SQLite in this example, your results may come out differently.

Your Second Migration

Knowing that our first migration works, lets create a second migration and test it out!

Now let’s create another migration: db/migrate/002_create_roles.rb File

And when executing rake Terminal, you should see the new table roles being created!

Wizard!

Moving Between Versions

At this stage we are at at version 2 of the database. But how do we go down to say version 0, before everything existed?

rake VERSION=0

What? You only want to go to version 1?

rake VERSION=1

Okay, enough showing off

Conclusion

That’s it, your ready to use and enjoy ActiveRecord’s Migrations Without Rails!

The Migration’s have a ton of features which you can and should utilize, which including adding indexes, controlling verbosity, renaming tables, dropping tables, and modifying existing tables (adding, renaming, modifying, deleting columns).

Just a whole alot of features, do check out the API for it!

10 Comments »

  1. kamal said,

    May 27, 2007 @ 8:35 am

    Shouldn’t 002_create_roles.rb’s self.up read

    create_table :roles …

    Also, worth nothing that edge rails now supports the Sexy Migrations syntax

    create_table :foo do |t|
    t.string :name, :address
    t.integer :age
    end

  2. aizatto said,

    May 27, 2007 @ 11:11 am

    Haha, oops. /me needs to work on his copy and paste abilities. And his proof reading, lots of errors popping up lately.

    Thanks :)

    Regards,
    Aizat

  3. Christopher Dwan said,

    May 29, 2007 @ 1:57 am

    This is too funny. I got up and running with my project using your other article, and of course the next thing I did after getting running was to look up migrations and voila! You wrote the article the day before I went searching for it. Are you reading my mind?

    Sexy Migrations appeared in changeset 6667. It’s a trivial change really, so I snagged the new code and tossed it into my copy of ActiveRecord.

    http://dev.rubyonrails.org/changeset/6667?new_path=trunk

    Now let’s have a whirl with Sexy Migrations off the rails.

  4. aizatto said,

    May 29, 2007 @ 7:58 am

    Hehe, good to hear my articles are coming to use :) hope others have will find them useful as well.

  5. Chris said,

    June 3, 2007 @ 7:16 pm

    Very useful article!! Now that I have ActiveRecord and Migrations working outside of rails, one thing is now left…. fixtures!!

    Do you know by chance how to use fixtures outside of rails? There’s some old examples floating around but I cant get it to work on the latest stable version of rails.

  6. aizatto said,

    June 4, 2007 @ 12:21 am

    Hi Chris, I hope my new article Fixtures Without Rails can get you started, if there are any problems, do let me know! :)

  7. Ahmed Micky said,

    July 13, 2007 @ 10:33 pm

    Thank you for this tutorial, it’s verry usefull.
    To complete this you can add,

    require “rubygems”
    require “active_record”
    require “yaml”
    require ‘rake’

    as a header of your Rakefile, and

    require “rubygems”

    at the begining of “CreateUsers” and “CreateRoles” classes

    to avoid exceptions, because you are no more using rails environement and some people can waste time to find out the error source.
    Thank you again.

  8. James Horsley said,

    August 5, 2007 @ 11:09 pm

    Thanks for the how to, it saved me a great deal of time and got me the results I was looking for!

  9. Frank said,

    January 25, 2008 @ 12:21 am

    I wish I had found this earlier. Thank you so much for this and all the other helpful tutorials.

  10. ken said,

    July 17, 2008 @ 7:50 am

    These tutorials are pretty great! Keep ‘em up!

    I hope it didn’t change too much in 2.1 (I know migrations are no longer numbered).

RSS feed for comments on this post · TrackBack URI

Leave a Comment