Skip to content

Commit

Permalink
Add allow_missing_migration_files option
Browse files Browse the repository at this point in the history
This PR allow use allow_missing_migration_files in config

ref to sequel commit jeremyevans/sequel@b64b812
  • Loading branch information
artofhuman committed Nov 16, 2019
1 parent 7da66c5 commit a18a31e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ You can configure some options with the usual rails mechanism, in
# Allowed options: :sql, :ruby.
config.sequel.schema_format = :sql

# Allowed options: true, false, default false
config.sequel.allow_missing_migration_files = true

# Whether to dump the schema after successful migrations.
# Defaults to false in production and test, true otherwise.
config.sequel.schema_dump = true
Expand Down
17 changes: 13 additions & 4 deletions lib/sequel_rails/migrations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class << self
def migrate(version = nil)
opts = {}
opts[:target] = version.to_i if version
opts[:allow_missing_migration_files] = !!SequelRails.configuration.allow_missing_migration_files

if migrations_dir.directory?
::Sequel::Migrator.run(::Sequel::Model.db, migrations_dir, opts)
Expand Down Expand Up @@ -43,8 +44,6 @@ def migrations_dir
def current_migration
return unless available_migrations?

migrator_class = ::Sequel::Migrator.send(:migrator_class, migrations_dir)
migrator = migrator_class.new ::Sequel::Model.db, migrations_dir
if migrator.respond_to?(:applied_migrations)
migrator.applied_migrations.last
elsif migrator.respond_to?(:current_version)
Expand All @@ -55,8 +54,6 @@ def current_migration
def previous_migration
return unless available_migrations?

migrator_class = ::Sequel::Migrator.send(:migrator_class, migrations_dir)
migrator = migrator_class.new ::Sequel::Model.db, migrations_dir
if migrator.respond_to?(:applied_migrations)
migrator.applied_migrations[-2] || '0'
elsif migrator.respond_to?(:current_version)
Expand All @@ -67,6 +64,18 @@ def previous_migration
def available_migrations?
File.exist?(migrations_dir) && Dir[File.join(migrations_dir, '*')].any?
end

def migrator
return @migrator if defined?(@migrator)

migrator_class = ::Sequel::Migrator.send(:migrator_class, migrations_dir)

@migrator = migrator_class.new(
::Sequel::Model.db,
migrations_dir,
allow_missing_migration_files: !!SequelRails.configuration.allow_missing_migration_files
)
end
end
end
end
25 changes: 21 additions & 4 deletions spec/lib/sequel_rails/migrations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,40 @@
describe ".#{migration_method}" do
let(:result) { double(:result) }
context 'with no version specified' do
let(:opts) { {} }
it 'runs migrations using Sequel::Migrator' do
expect(::Sequel::Migrator).to receive(:run).with(
db, Rails.root.join('db/migrate'), opts
db, Rails.root.join('db/migrate'), allow_missing_migration_files: false
).and_return result
expect(described_class.send(migration_method)).to be(result)
end
end
context 'with version specified' do
let(:opts) { { :target => 1 } }
it 'runs migrations using Sequel::Migrator' do
expect(::Sequel::Migrator).to receive(:run).with(
db, Rails.root.join('db/migrate'), opts
db, Rails.root.join('db/migrate'), allow_missing_migration_files: false, target: 1
).and_return result
expect(described_class.send(migration_method, 1)).to be(result)
end
end

context 'with allow_missing_migration_files' do
around do |ex|
option = SequelRails.configuration.allow_missing_migration_files
SequelRails.configuration.allow_missing_migration_files = true

ex.run

SequelRails.configuration.allow_missing_migration_files = option
end

it 'runs migrations using Sequel::Migrator' do
expect(::Sequel::Migrator).to receive(:run).with(
db, Rails.root.join('db/migrate'), allow_missing_migration_files: true
).and_return result

described_class.send(migration_method)
end
end
end
end

Expand Down

0 comments on commit a18a31e

Please sign in to comment.