Tony Lâmpada Tony Lâmpada - 22 days ago 5
Ruby Question

Ignore missing migration with sequel -s

I'm trying to use sequel to manage migrations for my database, and sometimes due to feature-bracnhing there are moments when the database has migraations applied that doen't really exist in the filesystem.

By default, when I apply migrations with

sequel -m
on those situations I get the error below

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

Now, looking at the docs I can see that 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.


QUESTION: How do I pass that allow_missing_migration_files option to
sequel -m


I think you'll have to use the Sequel::Migrator API for that. Something like, '/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]}", "db/migrations", target: args[:version].to_i, allow_missing_migration_files: true)
      puts "Migrating to latest", "db/migrations", allow_missing_migration_files: true)

Then run rake db:migrate_without_missing.