Skip to content

Commit

Permalink
Merge pull request #38 from Sage/build-skip-nil-validation
Browse files Browse the repository at this point in the history
allow nil condition values for backward compatibility
  • Loading branch information
buzz1710 authored Mar 10, 2021
2 parents 2d02cd4 + 3aee185 commit d014476
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
5 changes: 5 additions & 0 deletions lib/mysql_framework/sql_condition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,12 @@ def invalid_null_condition?(value)
end

def invalid_nil_value?(value)
return false if skip_nil_validation?
nil_comparison? == false && value.nil?
end

def skip_nil_validation?
ENV.fetch('MYSQL_FRAMEWORK_SKIP_NIL_VALUE_VALIDATION', 'false').downcase == 'true'
end
end
end
2 changes: 1 addition & 1 deletion lib/mysql_framework/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module MysqlFramework
VERSION = '2.1.1'
VERSION = '2.1.2'
end
30 changes: 30 additions & 0 deletions spec/lib/mysql_framework/sql_condition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
describe MysqlFramework::SqlCondition do
subject { described_class.new(column: 'version', comparison: '=', value: '1.0.0') }

before :each do
allow_any_instance_of(MysqlFramework::SqlCondition).to receive(:skip_nil_validation?).and_return(skip_nil_validation)
end

let(:skip_nil_validation) { false }

describe '#to_s' do
it 'returns the condition as a string for a prepared statement' do
expect(subject.to_s).to eq('version = ?')
Expand All @@ -16,6 +22,14 @@
it 'does raises an ArgumentError' do
expect { subject }.to raise_error(ArgumentError, "Comparison of = requires value to be not nil")
end

context 'when skip_nil_validation? is true' do
let(:skip_nil_validation) { true }

it 'does not raise an ArgumentError' do
expect(subject.value).to be_nil
end
end
end
end

Expand All @@ -33,6 +47,14 @@
it 'raises an ArgumentError if value is set' do
expect { subject }.to raise_error(ArgumentError, 'Cannot set value when comparison is IS NULL')
end

context 'when skip_nil_validation? is true' do
let(:skip_nil_validation) { true }

it 'raises an ArgumentError if value is set' do
expect { subject }.to raise_error(ArgumentError, 'Cannot set value when comparison is IS NULL')
end
end
end
end

Expand Down Expand Up @@ -67,6 +89,14 @@
it 'raises an ArgumentError if value is set' do
expect { subject }.to raise_error(ArgumentError, 'Cannot set value when comparison is IS NOT NULL')
end

context 'when skip_nil_validation? is true' do
let(:skip_nil_validation) { true }

it 'raises an ArgumentError if value is set' do
expect { subject }.to raise_error(ArgumentError, 'Cannot set value when comparison is IS NOT NULL')
end
end
end
end

Expand Down

0 comments on commit d014476

Please sign in to comment.