From 3aee185de328ce017e30ce14e42428d41c91caa2 Mon Sep 17 00:00:00 2001 From: Neil Armstrong Date: Tue, 9 Mar 2021 13:50:41 +0000 Subject: [PATCH] allow nil condition values for backward compatibility --- lib/mysql_framework/sql_condition.rb | 5 ++++ lib/mysql_framework/version.rb | 2 +- .../lib/mysql_framework/sql_condition_spec.rb | 30 +++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/lib/mysql_framework/sql_condition.rb b/lib/mysql_framework/sql_condition.rb index 7584a6c..0eabb77 100644 --- a/lib/mysql_framework/sql_condition.rb +++ b/lib/mysql_framework/sql_condition.rb @@ -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 diff --git a/lib/mysql_framework/version.rb b/lib/mysql_framework/version.rb index c88bcc3..a729a44 100644 --- a/lib/mysql_framework/version.rb +++ b/lib/mysql_framework/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module MysqlFramework - VERSION = '2.1.1' + VERSION = '2.1.2' end diff --git a/spec/lib/mysql_framework/sql_condition_spec.rb b/spec/lib/mysql_framework/sql_condition_spec.rb index d8b86d0..dc35571 100644 --- a/spec/lib/mysql_framework/sql_condition_spec.rb +++ b/spec/lib/mysql_framework/sql_condition_spec.rb @@ -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 = ?') @@ -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 @@ -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 @@ -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