JRuby on Rails with Nine Lives: Running a JRuby on Rails Application on Tomcat, Page 4
Creating Your Migration
You will use Rails Migrations to manage the creation and deletion of the database table that you are going to use to manage your Guitars from your app. Your table is going to conveniently be named guitars. Run the following from the railswartest direcrory:
jruby script/generate migration add_a_new_table
This will create the db/migrate/001_add_guitar_table.rb file:
Click here for a larger image.
Figure 10: Creating our add_guitar_table migration
This file contains a Ruby class named AddGuitarTable that extends Rails' ActiveRecord::Migration class. Edit this file to contain the following:
class AddGuitarTable < ActiveRecord::Migration
def self.up
create_table :guitars do |table|
table.column :make, :string, :null => false
table.column :model, :string, :null => false
table.column :color, :string, :null => false
end
end
def self.down
drop_table :guitars
end
end
Click here for a larger image.
Figure 11: AddGuitarTable migration class
Now, run the migration:
jruby -S rake db:migrate
Click here for a larger image.
Figure 12: Running your migration to create the guitars table
Running MySQL Query Browser confirms that the guitars table has been created by the migration:
Click here for a larger image.
Figure 13: Empty guitars table in MySQL
