Created
November 13, 2012 13:10
-
-
Save kohgpat/4065694 to your computer and use it in GitHub Desktop.
Sequel Rake tasks for a Sinatra app
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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