Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/nandi/instructions/add_column.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ def procedure
def lock
Nandi::Migration::LockWeights::ACCESS_EXCLUSIVE
end

def validator
Validation::AddColumnValidator
end
end
end
end
4 changes: 4 additions & 0 deletions lib/nandi/instructions/add_index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ def lock
Nandi::Migration::LockWeights::SHARE
end

def validator
Validation::AddIndexValidator
end

attr_reader :table, :fields

private
Expand Down
4 changes: 4 additions & 0 deletions lib/nandi/instructions/add_reference.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ def procedure
def lock
Nandi::Migration::LockWeights::ACCESS_EXCLUSIVE
end

def validator
Validation::AddReferenceValidator
end
end
end
end
4 changes: 4 additions & 0 deletions lib/nandi/instructions/remove_index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ def lock
Nandi::Migration::LockWeights::SHARE
end

def validator
Validation::RemoveIndexValidator
end

attr_reader :table

private
Expand Down
15 changes: 3 additions & 12 deletions lib/nandi/validation/each_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,9 @@ def initialize(instruction)
end

def call
case instruction.procedure
when :add_index
AddIndexValidator.call(instruction)
when :remove_index
RemoveIndexValidator.call(instruction)
when :add_column
AddColumnValidator.call(instruction)
when :add_reference
AddReferenceValidator.call(instruction)
else
success
end
return success unless instruction.respond_to?(:validator)

instruction.validator.call(instruction)
end

attr_reader :instruction
Expand Down
2 changes: 1 addition & 1 deletion lib/nandi/validation/failure_helpers.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require "dry/monads/result"
require "dry/monads"

module Nandi
module Validation
Expand Down
30 changes: 5 additions & 25 deletions spec/nandi/validation/each_validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@

describe "#call" do
context "when the given instruction is to remove an index" do
let(:instruction) { instance_double(Nandi::Instructions::RemoveIndex) }

before do
allow(instruction).to receive(:procedure).and_return(:remove_index)
end
let(:instruction) { Nandi::Instructions::RemoveIndex.new(table: :payments, field: :foo) }

it "calls RemoveIndexValidator" do
expect(Nandi::Validation::RemoveIndexValidator).to receive(:call).
Expand All @@ -26,11 +22,7 @@
end

context "when the given instruction is to add a column" do
let(:instruction) { instance_double(Nandi::Instructions::AddColumn) }

before do
allow(instruction).to receive(:procedure).and_return(:add_column)
end
let(:instruction) { Nandi::Instructions::AddColumn.new(table: :payments, name: :foo, type: :text) }

it "calls AddColumnValidator" do
expect(Nandi::Validation::AddColumnValidator).to receive(:call).with(instruction)
Expand All @@ -40,11 +32,7 @@
end

context "when the given instruction is to add a reference" do
let(:instruction) { instance_double(Nandi::Instructions::AddReference) }

before do
allow(instruction).to receive(:procedure).and_return(:add_reference)
end
let(:instruction) { Nandi::Instructions::AddReference.new(table: :payments, ref_name: :user) }

it "calls AddReferenceValidator" do
expect(Nandi::Validation::AddReferenceValidator).to receive(:call).
Expand All @@ -55,11 +43,7 @@
end

context "when the given instruction is to add an index" do
let(:instruction) { instance_double(Nandi::Instructions::AddIndex) }

before do
allow(instruction).to receive(:procedure).and_return(:add_index)
end
let(:instruction) { Nandi::Instructions::AddIndex.new(table: :payments, fields: [:foo]) }

it "calls AddIndexValidator" do
expect(Nandi::Validation::AddIndexValidator).to receive(:call).
Expand All @@ -70,11 +54,7 @@
end

context "when the given instruction isn't explicitly validated" do
let(:instruction) { instance_double(Nandi::Instructions::AddForeignKey) }

before do
allow(instruction).to receive(:procedure).and_return(:add_foreign_key)
end
let(:instruction) { Nandi::Instructions::AddForeignKey.new(table: :payments, target: :users) }

it "returns successful" do
expect(call).to eq(Dry::Monads::Result::Success.new(nil))
Expand Down