Skip to content

Commit

Permalink
migration base suppress validations
Browse files Browse the repository at this point in the history
  • Loading branch information
vprokopchuk256 committed Dec 27, 2014
1 parent 3bcf495 commit ceb93ee
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 6 deletions.
34 changes: 29 additions & 5 deletions lib/mv/core/migration/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ def initialize()
SUPPORTED_METHODS.each do |operation_name|
define_method operation_name do |*args|

operation = operations_factory.create_operation(operation_name, *args)
operations_list.add_operation(operation)
unless disabled_validations?
operation = operations_factory.create_operation(operation_name, *args)
operations_list.add_operation(operation)
end
end
end

Expand All @@ -50,14 +52,36 @@ def execute
def add_column table_name, column_name, opts
return unless opts.present?

operation = operations_factory.create_operation(:add_column, table_name, column_name, opts)
operations_list.add_operation(operation)
unless disabled_validations?
operation = operations_factory.create_operation(:add_column, table_name, column_name, opts)
operations_list.add_operation(operation)
end
end

def with_suppressed_validations
disable_validations!
yield
enable_validations!
end

class << self
alias_method :current, :instance

delegate *[SUPPORTED_METHODS, :execute].flatten, to: :current, allow_nil: true
delegate *[SUPPORTED_METHODS, :execute, :with_suppressed_validations].flatten, to: :current, allow_nil: true
end

private

def disabled_validations?
!!@disabled_validations
end

def disable_validations!
@disabled_validations = true
end

def enable_validations!
@disabled_validations = false
end
end
end
Expand Down
79 changes: 78 additions & 1 deletion spec/mv/core/migration/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@
end
end

describe "when validaions are suppressed" do
subject(:add_column) {
migration.with_suppressed_validations do
migration.add_column :table_name, :column_name, length: { is: 5 }
end
}

it "does NOT add any operation to the list" do
expect(migration.operations_list).not_to receive(:add_operation)
add_column
end
end

describe "when validations are NOT defined" do
subject(:add_column) {
migration.add_column :table_name, :column_name, nil
Expand Down Expand Up @@ -55,6 +68,19 @@

subject
end

describe "when validaions are suppressed" do
subject(:remove_column) do
migration.with_suppressed_validations do
migration.remove_column :table_name, :column_name
end
end

it "does NOT add any operation to the list" do
expect(migration.operations_list).not_to receive(:add_operation)
remove_column
end
end
end

describe "#rename_column" do
Expand All @@ -72,6 +98,19 @@

subject
end

describe "when validaions are suppressed" do
subject(:rename_column) do
migration.with_suppressed_validations do
migration.rename_column :table_name, :old_column_name, :new_column_name
end
end

it "does NOT add any operation to the list" do
expect(migration.operations_list).not_to receive(:add_operation)
rename_column
end
end
end

describe "#change_column" do
Expand All @@ -89,6 +128,19 @@

subject
end

describe "when validaions are suppressed" do
subject(:change_column) do
migration.with_suppressed_validations do
migration.change_column :table_name, :column_name, length: { is: 5 }
end
end

it "does NOT add any operation to the list" do
expect(migration.operations_list).not_to receive(:add_operation)
change_column
end
end
end

describe "#rename_table" do
Expand All @@ -106,6 +158,19 @@

subject
end

describe "when validaions are suppressed" do
subject(:rename_table) do
migration.with_suppressed_validations do
migration.rename_table :old_table_name, :new_table_name
end
end

it "does NOT add any operation to the list" do
expect(migration.operations_list).not_to receive(:add_operation)
rename_table
end
end
end

describe "#drop_table" do
Expand All @@ -123,6 +188,19 @@

subject
end

describe "when validaions are suppressed" do
subject(:drop_table) do
migration.with_suppressed_validations do
migration.drop_table :table_name
end
end

it "does NOT add any operation to the list" do
expect(migration.operations_list).not_to receive(:add_operation)
drop_table
end
end
end

describe "#execute" do
Expand All @@ -133,6 +211,5 @@

subject
end

end
end

0 comments on commit ceb93ee

Please sign in to comment.