如何使用 "sequel -s" 忽略丢失的迁移

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

我正在尝试使用 Sequel 来管理我的数据库的迁移,有时,由于功能分支,有时数据库应用了文件系统中不存在的迁移。

默认情况下,当我在这些情况下使用 sequel -m 应用迁移时,我收到此错误:

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

"Migrations" 表示有一个选项:

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.

不错!

如何将 allow_missing_migration_files 选项传递给 sequel -m

我认为您必须为此使用 Sequel::Migrator API。像

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

您也可以将此行为包装在 Rake 任务中,这样您就不必启动控制台来 运行 迁移。

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

然后 运行 rake db:migrate_without_missing.