Comparing Rails to Liquibase: Getting Started with Rails (Part 2)


Over the next week, you’ll see a series of posts comparing Rails
Migrations to Liquibase. In yesterday’s post, I discussed some issues
that start to become apparent after using Rails Migrations for a few
years to support an ever growing and increasingly complex enterprise database.

In this post, I outline the effort required
to create a new Rails application which uses the latest JRuby
distribution and a MySQL database. Throughout this series, I’m going
to refer back to the sample project
created in this post. If you are interested in following along with
some of my experiments and comparisons, I’ll refer back to this post
for people interested in following along.

Prerequisites and Assumptions

You are running a Java 6 SDK. Either OpenJDK or a JDK from Oracle.
You should also be running a relatively recent MySQL 5.1
distribution, and you should have administrative access to it.

Step 1: Download JRuby

$ cd /usr/local
$ curl
http://jruby.org.s3.amazonaws.com/downloads/1.5.5/jruby-bin-1.5.5.tar.gz
| sudo tar xvz -
$ ln -s /usr/local/jruby-1.5.5 /usr/local/jruby

What’s with the crazy piping command? I got into the habit of
piping curl to tar after years of downloading and unpacking tarballs. If you do
it this way you don’t end up with a disk full of .tar.gz files.

Step 2: Configure Your Environment

I’m on OSX and I use bash, so I put the following lines in my
~/.bash_profile. I understand that everyone has a different way of
setting environment variables, so if you have a better idea, go for
it.
All you need to do is define JRUBY_HOME, add bin to the
path, and I also just follow up by defining an alias for ruby which
points to jruby.

export JRUBY_HOME=/usr/local/jruby
export PATH=${JRUBY_HOME}/bin:${PATH}
alias ruby=jruby

You’ll have to logout and login for this change to ~/.bash_profile
to take effect.

Step 3: Install the Rails Gem

To install Rails (and all related libraries), download JRuby as described in the previous set. Then install rails by running the following command:

$ gem install rails

Step 3: Create Your Rails Application

Change directories to whenever you want to create your Rails
application and run the following command.

$ rails create dbtest

This will create the default Rails project in the directory dbtest/.

Step 4: Add the ActiveRecord JDBC MySQL adapter

Change directories to the
newly created dbtest/ directory. You will want to
configure your application to use MySQL, so add the following line to
Gemfile:

gem 'activerecord-jdbcmysql-adapter'

This will tell Rails to configure the JDBC adapter for ActiveRecord.

Step 5: Configure Your Database Connection

Next, update your config/database.yml file to point to a MySQL
database using the jdbcmysql adapter, here’s the development section
of the config/database.yml. (Note: You should change your credentials
from those listed below.)

development:
  adapter: jdbcmysql
  database: dbtest_development
  host: localhost
  username: root
  password:
  pool: 5
  timeout: 5000

I’m going to skip the dbtest_production and dbtest_test databases.
Since we’re just going to be using this application as a way to test
database change management tools, there’s no need to go through these motions.

Step 6: Create Your Database

At this point, a Rails expert would likely suggest running something
like Rake db:setup. I don’t do this. While I appreciate that Rails
migrations can be used to do everything, I also don’t think you should
yield everything to the tool. For me, it is mysqladmin:

$ mysqladmin -u root -p create
dbtest_development

Step 7: Run Rails Migrations

Lastly, run “rake db:migrate”. Runnning this command from your
application will create a single table “schema_migrations” which will
be used to keep track of your Rails migrations.

Next Steps…

None. You’ve set up a simple Rails application which runs in
JRuby, you’ve connected it to a database, and you ran “rake
db:migrate”. The next post in this series is going to set up a
liquibase-enabled project which will we use to explore and compare the
features of rails with liquibase.