Skip to content

Instantly share code, notes, and snippets.

@azizshamim
Created August 15, 2012 19:19
Show Gist options
  • Save azizshamim/3362779 to your computer and use it in GitHub Desktop.
Save azizshamim/3362779 to your computer and use it in GitHub Desktop.
Sequel Rake tasks for a Sinatra app
# change foo to your library name
# change Foo::Database to your Sequel database
namespace :bundler do
task :setup do
require 'rubygems'
require 'bundler/setup'
end
end
task :environment, [:env] => 'bundler:setup' do |cmd, args|
ENV["RACK_ENV"] = args[:env] || "development"
require "./lib/foo"
end
namespace :db do
desc "Run database migrations"
task :migrate, :env do |cmd, args|
env = args[:env] || "development"
Rake::Task['environment'].invoke(env)
require 'sequel/extensions/migration'
Sequel::Migrator.apply(Foo::Database, "db/migrate")
end
desc "Rollback the database"
task :rollback, :env do |cmd, args|
env = args[:env] || "development"
Rake::Task['environment'].invoke(env)
require 'sequel/extensions/migration'
version = (row = Foo::Database[:schema_info].first) ? row[:version] : nil
Sequel::Migrator.apply(Foo::Database, "db/migrate", version - 1)
end
desc "Nuke the database (drop all tables)"
task :nuke, :env do |cmd, args|
env = args[:env] || "development"
Rake::Task['environment'].invoke(env)
Foo::Database.tables.each do |table|
Foo::Database.run("DROP TABLE #{table}")
end
end
desc "Reset the database"
task :reset, [:env] => [:nuke, :migrate]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment