Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Rake task sugar for Sequel Migrations (version, migrate, rollback, reset)
namespace :db do
require "sequel"
Sequel.extension :migration
DB = Sequel.connect(ENV['DATABASE_URL'])
desc "Prints current schema version"
task :version do
version = if DB.tables.include?(:schema_info)
DB[:schema_info].first[:version]
end || 0
puts "Schema Version: #{version}"
end
desc "Perform migration up to latest migration available"
task :migrate do
Sequel::Migrator.run(DB, "migrations")
Rake::Task['db:version'].execute
end
desc "Perform rollback to specified target or full rollback as default"
task :rollback, :target do |t, args|
args.with_defaults(:target => 0)
Sequel::Migrator.run(DB, "migrations", :target => args[:target].to_i)
Rake::Task['db:version'].execute
end
desc "Perform migration reset (full rollback and migration)"
task :reset do
Sequel::Migrator.run(DB, "migrations", :target => 0)
Sequel::Migrator.run(DB, "migrations")
Rake::Task['db:version'].execute
end
end
@solutus

This comment has been minimized.

Copy link

@solutus solutus commented Feb 21, 2019

possibly, you may want the same behavior as in rails, where rollback steps to previous version. In this case, use

  task :rollback, :target do |t, args|                                                                                                                                                                             
                                                                                                                                                 
    version = if DB.tables.include?(:schema_info)                                                                                                                                                                  
      DB[:schema_info].first[:version]                                                                                                                                                                             
    end || 0                                                                                                                                                                                                       
                                                                                                                                                                                                                   
    target = version.zero? ? 0 : (version - 1)                                                                                                                                                                     
    args.with_defaults(:target => target)                                                                                                                                                                          
                                                                                                                                                                                                                   
    Sequel::Migrator.run(db, "db/migrations", :target => args[:target].to_i)                                                                                                                                       
    Rake::Task['db:version'].execute                                                                                                                                                                               
  end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment