Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow nil condition values for backward compatibility #38

Merged
merged 1 commit into from
Mar 10, 2021
Merged
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
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