Last active
August 29, 2015 14:25
-
-
Save jsborjesson/c67a6581f160d3b344e9 to your computer and use it in GitHub Desktop.
Rails-like rake db:migrate with sequel
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
| # Rakefile | |
| namespace :db do | |
| desc "Run migrations" | |
| task :migrate, [:version] do |t, args| | |
| require "sequel" | |
| Sequel.extension :migration | |
| db = Sequel.connect(ENV.fetch("DATABASE_URL")) | |
| if args[:version] | |
| puts "Migrating to version #{args[:version]}" | |
| Sequel::Migrator.run(db, "db/migrations", target: args[:version].to_i) | |
| else | |
| puts "Migrating to latest" | |
| Sequel::Migrator.run(db, "db/migrations") | |
| end | |
| end | |
| desc "Rollback migration" | |
| task :rollback, [:version] do |t, args| | |
| require "sequel" | |
| Sequel.extension :migration | |
| db = Sequel.connect(ENV.fetch("DATABASE_URL")) | |
| version = args[:version] || db[:schema_info].first[:version] - 1 | |
| if version | |
| puts "Rolling back to version #{version}" | |
| Sequel::Migrator.run(db, "db/migrations", target: version.to_i) | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment