Created
April 20, 2019 04:59
-
-
Save zzidante/af292da03eef89f809bc91fd9f0ea4b2 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