Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Rakefile for blog post
# Rakefile
namespace :db do
require "sequel"
namespace :migrate do
Sequel.extension :migration
DB = Sequel.connect(ENV['DATABASE_URL'])
desc "Perform migration reset (full erase and migration up)"
task :reset do
Sequel::Migrator.run(DB, "migrations", :target => 0)
Sequel::Migrator.run(DB, "migrations")
puts "<= sq:migrate:reset executed"
end
desc "Perform migration up/down to VERSION"
task :to do
version = ENV['VERSION'].to_i
raise "No VERSION was provided" if version.nil?
Sequel::Migrator.run(DB, "migrations", :target => version)
puts "<= sq:migrate:to version=[#{version}] executed"
end
desc "Perform migration up to latest migration available"
task :up do
Sequel::Migrator.run(DB, "migrations")
puts "<= sq:migrate:up executed"
end
desc "Perform migration down (erase all data)"
task :down do
Sequel::Migrator.run(DB, "migrations", :target => 0)
puts "<= sq:migrate:down executed"
end
end
end
@DevL
Copy link

DevL commented Dec 12, 2012

The :to task has a slight problem. Since nil.to_i and "".to_i both return 0, not setting the VERSION environment variable (or setting it to empty string) will actually wipe the database. Line 19 will never raise an error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment