Search code examples
rubysequel

How to ignore a missing migration with "sequel -s"


I'm trying to use Sequel to manage migrations for my database, and sometimes, due to feature-branching, there are moments when the database has migrations applied that don't exist in the filesystem.

By default, when I apply migrations with sequel -m on those situations I get this error:

Error: Sequel::Migrator::Error: Applied migration files not in file system

"Migrations" says there's an option for that:

Ignoring missing migrations

In some cases, you may want to allow a migration in the database that does not exist in the filesystem (deploying to an older version of code without running a down migration when deploy auto-migrates, for example). If required, you can pass :allow_missing_migration_files => true as an option. This will stop errors from being raised if there are migrations in the database that do not exist in the filesystem.

Nice!

How do I pass that allow_missing_migration_files option to sequel -m?


Solution

  • I think you'll have to use the Sequel::Migrator API for that. Something like

    Sequel::Migrator.run(DB, '/path/to/migrations/dir', allow_missing_migration_files: true)
    

    You can also wrap this behavior in a Rake task, so you don't have to start a console to run migrations.

    namespace :db do
      desc "Run migrations ignoring the missing ones"
      task :migrate_without_missing, [: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, allow_missing_migration_files: true)
        else
          puts "Migrating to latest"
          Sequel::Migrator.run(db, "db/migrations", allow_missing_migration_files: true)
        end
      end
    end
    

    Then run rake db:migrate_without_missing.