namespace :db do
task :migrate do
m = Sequel::Migrator
db = Sequel.connect(ENV['DATABASE_URL'] || 'sqlite://library.sqlite')
dir = "dbMigration"
target = ENV['TARGET'] ? ENV['TARGET'].to_i : nil
current = ENV['CURRENT'] ? ENV['CURRENT'].to_i : nil
m.run(db, dir, :target => target, :current => current)
We create a sequel migrator and connect to the database. The database connection line contains two parts. The first side is if there's an environment variable named
DATABASE_URLthen we'll use that to connect to the database. This is an environment variable set by Heroku. If this isn't set, then we'll go ahead and connect to a sqlite database,
library.sqlite. We use the
Sequel.connect command rather than
Sequel.sqliteas we don't know which type of database we're going to be using. On Heroku, it will be PostgreSQL and locally, we use sqlite. Next up, we set the directory where we'll keep our migrations, in this case, dbMigration which is my standard. After that, there's two lines used for telling sequel which version we're going to,
target, and starting from,
current. Finally, we run the migrator with the database, directory, target and current. The final two are part of a hashtable that's used by this migrator. You could also use
m.applywhere the first two parameters are the same and then pass the target and current as integers, but the migrator code seems to imply that the
runversion is preferred. To run this locally, simply do a
rake db:migrateand it should migrate to the latest version. To move to a different version you can run
rake TARGET=0 db:migrateto remove everything from the database. If you just want to move to the latest version, don't put a target or current in, they'll be nil, and it should just migrate to the latest. Now, to run it on Heroku and migrate the database there simply add heroku in front of the command as in
heroku rake db:migrateafter you've pushed all the code up to Heroku with a
git push heroku masteras we saw in the last post.
I haven't talked about rake before, so I should probably give a little background for it. It's a ruby version of the Unix make. It has tasks and commands to do them. The homepage for it is here and here is a nice article on using it.
OK, now let's take a look at our model file model/models.rb.
# The database should have been set up using the database migrations in
# the dbMigration directory.
# Open the library's database. This must be done before we access the models
# that use it.
Sequel.connect(ENV['DATABASE_URL'] || 'sqlite://library.sqlite')
# This is the model for the authors and is backed by the :authors table in the
# Create the Author model.
class Author < Sequel::Model
# This is the model for the book and is backed by the :book table in the
# Create the Book model.
class Book < Sequel::Model
This looks exactly like our previous models with a couple of exceptions. Here, we've moved our database connection into here from start.rb. It probably makes more sense here and keeps all of our Sequel code in one place (I got this idea from the Ramaze generated code). The connection itself looks surprisingly like our connection that was used in the rakefile. We end up using either a DATABASE_URL environment variable (supplied by Heroku) or the sqlite library.sqlite. After that, there are a couple of models that are in the database that we can use. The tables for the database and some data are created in the dbMigration/001_LibraryMigration.rb file.
You can see the end result of all of this here.
All of the code for this is up on github and as always, if you have questions, leave them in the comments.