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

Ton*_*ada 2 ruby sequel

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

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

Error: Sequel::Migrator::Error: Applied migration files not in file system
Run Code Online (Sandbox Code Playgroud)

Migrations ”说有一个选择:

忽略缺失的迁移

在某些情况下,您可能希望允许在文件系统中不存在的数据库中进行迁移(例如,在部署自动迁移时部署到旧版本的代码而不运行向下迁移)。如果需要,您可以传递 :allow_missing_migration_files => true 作为选项。如果数据库中存在文件系统中不存在的迁移,这将阻止引发错误。

好的!

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

Ale*_*lim 6

我认为你必须Sequel::Migrator API为此使用。就像是

Sequel::Migrator.run(DB, '/path/to/migrations/dir', allow_missing_migration_files: true)
Run Code Online (Sandbox Code Playgroud)

您还可以将此行为包装在 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
Run Code Online (Sandbox Code Playgroud)

然后跑rake db:migrate_without_missing