From 1fa3747c3b623c9d35c134b1032aeb9fa44217d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Kornek?= Date: Tue, 19 Jan 2021 16:43:39 +0100 Subject: [PATCH 1/9] use ActiveRecord::ConnectionAdapters::SchemaCreation for rails 6.1 --- .../connection_adapters/sqlserver/schema_creation.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/active_record/connection_adapters/sqlserver/schema_creation.rb b/lib/active_record/connection_adapters/sqlserver/schema_creation.rb index 4f65bd228..cb5205d05 100644 --- a/lib/active_record/connection_adapters/sqlserver/schema_creation.rb +++ b/lib/active_record/connection_adapters/sqlserver/schema_creation.rb @@ -3,7 +3,11 @@ module ActiveRecord module ConnectionAdapters module SQLServer - class SchemaCreation < AbstractAdapter::SchemaCreation + BASE_SCHEMA_CREATION_CLASS = Rails::VERSION::MAJOR >= 6 && Rails::VERSION::MINOR >= 1 ? + SchemaCreation : + AbstractAdapter::SchemaCreation + + class SchemaCreation < BASE_SCHEMA_CREATION_CLASS private def visit_TableDefinition(o) From 77905a4163f558417e973612f000ceb4c51d4888 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Kornek?= Date: Tue, 19 Jan 2021 16:44:45 +0100 Subject: [PATCH 2/9] use just **args instead of options, **args in SQLServerRealTransaction --- .../connection_adapters/sqlserver/transaction.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/active_record/connection_adapters/sqlserver/transaction.rb b/lib/active_record/connection_adapters/sqlserver/transaction.rb index d25617e9e..84e13ede1 100644 --- a/lib/active_record/connection_adapters/sqlserver/transaction.rb +++ b/lib/active_record/connection_adapters/sqlserver/transaction.rb @@ -31,9 +31,9 @@ def current_isolation_level module SQLServerRealTransaction attr_reader :starting_isolation_level - def initialize(connection, options, **args) + def initialize(connection, **args) @connection = connection - @starting_isolation_level = current_isolation_level if options[:isolation] + @starting_isolation_level = current_isolation_level if args[:isolation] super end From bc3c149c416eefdcf077da1995dcbb6699d11add Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Kornek?= Date: Tue, 19 Jan 2021 17:10:49 +0100 Subject: [PATCH 3/9] only use updated initialize arguments in SQLServerRealTransaction for rails 6.1 --- .../connection_adapters/sqlserver/transaction.rb | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/active_record/connection_adapters/sqlserver/transaction.rb b/lib/active_record/connection_adapters/sqlserver/transaction.rb index 84e13ede1..7f321d20e 100644 --- a/lib/active_record/connection_adapters/sqlserver/transaction.rb +++ b/lib/active_record/connection_adapters/sqlserver/transaction.rb @@ -31,10 +31,18 @@ def current_isolation_level module SQLServerRealTransaction attr_reader :starting_isolation_level - def initialize(connection, **args) - @connection = connection - @starting_isolation_level = current_isolation_level if args[:isolation] - super + if Rails::VERSION::MAJOR >= 6 && Rails::VERSION::MINOR >= 1 + def initialize(connection, **args) + @connection = connection + @starting_isolation_level = current_isolation_level if args[:isolation] + super + end + else + def initialize(connection, options, **args) + @connection = connection + @starting_isolation_level = current_isolation_level if options[:isolation] + super + end end def commit From 3399f41829d1e205b23e296acae457ab2332e2b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Kornek?= Date: Tue, 19 Jan 2021 18:04:29 +0100 Subject: [PATCH 4/9] fix for type_cast_calculated_value has already been applied to rails 5.2, so we don't need to it anymore --- .../sqlserver/core_ext/calculations.rb | 9 --------- 1 file changed, 9 deletions(-) diff --git a/lib/active_record/connection_adapters/sqlserver/core_ext/calculations.rb b/lib/active_record/connection_adapters/sqlserver/core_ext/calculations.rb index d02e6a302..a69058b81 100644 --- a/lib/active_record/connection_adapters/sqlserver/core_ext/calculations.rb +++ b/lib/active_record/connection_adapters/sqlserver/core_ext/calculations.rb @@ -31,15 +31,6 @@ def calculate(operation, column_name) def build_count_subquery(relation, column_name, distinct) super(relation.unscope(:order), column_name, distinct) end - - def type_cast_calculated_value(value, type, operation = nil) - case operation - when "count" then value.to_i - when "sum" then type.deserialize(value || 0) - when "average" then value&.respond_to?(:to_d) ? value.to_d : value - else type.deserialize(value) - end - end end end end From d79cfb1e54973ab03bd700c4b1c2b2e70fb3178f Mon Sep 17 00:00:00 2001 From: Wanderson Policarpo Date: Mon, 12 Apr 2021 12:20:21 +0100 Subject: [PATCH 5/9] Remove backward compatible code --- .../sqlserver/schema_creation.rb | 6 +----- .../connection_adapters/sqlserver/transaction.rb | 16 ++++------------ 2 files changed, 5 insertions(+), 17 deletions(-) diff --git a/lib/active_record/connection_adapters/sqlserver/schema_creation.rb b/lib/active_record/connection_adapters/sqlserver/schema_creation.rb index cb5205d05..16f98aa54 100644 --- a/lib/active_record/connection_adapters/sqlserver/schema_creation.rb +++ b/lib/active_record/connection_adapters/sqlserver/schema_creation.rb @@ -3,11 +3,7 @@ module ActiveRecord module ConnectionAdapters module SQLServer - BASE_SCHEMA_CREATION_CLASS = Rails::VERSION::MAJOR >= 6 && Rails::VERSION::MINOR >= 1 ? - SchemaCreation : - AbstractAdapter::SchemaCreation - - class SchemaCreation < BASE_SCHEMA_CREATION_CLASS + class SchemaCreation < SchemaCreation private def visit_TableDefinition(o) diff --git a/lib/active_record/connection_adapters/sqlserver/transaction.rb b/lib/active_record/connection_adapters/sqlserver/transaction.rb index 7f321d20e..86f4aa777 100644 --- a/lib/active_record/connection_adapters/sqlserver/transaction.rb +++ b/lib/active_record/connection_adapters/sqlserver/transaction.rb @@ -31,18 +31,10 @@ def current_isolation_level module SQLServerRealTransaction attr_reader :starting_isolation_level - if Rails::VERSION::MAJOR >= 6 && Rails::VERSION::MINOR >= 1 - def initialize(connection, **args) - @connection = connection - @starting_isolation_level = current_isolation_level if args[:isolation] - super - end - else - def initialize(connection, options, **args) - @connection = connection - @starting_isolation_level = current_isolation_level if options[:isolation] - super - end + def initialize(connection, isolation: nil, joinable: true, run_commit_callbacks: false) + @connection = connection + @starting_isolation_level = current_isolation_level if isolation + super end def commit From 2b56e52bda99fdccfce7a33871773f451bb157c6 Mon Sep 17 00:00:00 2001 From: Joshua Harris Date: Wed, 17 Feb 2021 17:40:14 +0000 Subject: [PATCH 6/9] fixed bug with false default values on bit columns --- .../connection_adapters/sqlserver/schema_statements.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/active_record/connection_adapters/sqlserver/schema_statements.rb b/lib/active_record/connection_adapters/sqlserver/schema_statements.rb index 7d19774a8..4b3116e02 100644 --- a/lib/active_record/connection_adapters/sqlserver/schema_statements.rb +++ b/lib/active_record/connection_adapters/sqlserver/schema_statements.rb @@ -422,10 +422,12 @@ def column_definitions(table_name) else type = case ci[:type] when /smallint|int|bigint/ then ci[:_type] + when /bit/ then 'smallint' else ci[:type] end value = default.match(/\A\((.*)\)\Z/m)[1] value = select_value("SELECT CAST(#{value} AS #{type}) AS value", "SCHEMA") + [value, nil] end end From 80c3b9d7f1f5909a75aad5b4abaee0b4dd5bf544 Mon Sep 17 00:00:00 2001 From: Circuit8 Date: Mon, 22 Feb 2021 10:38:04 +0000 Subject: [PATCH 7/9] fixed tests --- .../sqlserver/schema_creation.rb | 6 +++++- .../connection_adapters/sqlserver/transaction.rb | 16 ++++++++++++---- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/lib/active_record/connection_adapters/sqlserver/schema_creation.rb b/lib/active_record/connection_adapters/sqlserver/schema_creation.rb index 16f98aa54..79f314fe5 100644 --- a/lib/active_record/connection_adapters/sqlserver/schema_creation.rb +++ b/lib/active_record/connection_adapters/sqlserver/schema_creation.rb @@ -3,7 +3,11 @@ module ActiveRecord module ConnectionAdapters module SQLServer - class SchemaCreation < SchemaCreation + BASE_SCHEMA_CREATION_CLASS = ((ActiveRecord.version <=> Gem::Version.new("6.1")) != -1) ? + SchemaCreation : + AbstractAdapter::SchemaCreation + + class SchemaCreation < BASE_SCHEMA_CREATION_CLASS private def visit_TableDefinition(o) diff --git a/lib/active_record/connection_adapters/sqlserver/transaction.rb b/lib/active_record/connection_adapters/sqlserver/transaction.rb index 86f4aa777..96acbcfe0 100644 --- a/lib/active_record/connection_adapters/sqlserver/transaction.rb +++ b/lib/active_record/connection_adapters/sqlserver/transaction.rb @@ -31,10 +31,18 @@ def current_isolation_level module SQLServerRealTransaction attr_reader :starting_isolation_level - def initialize(connection, isolation: nil, joinable: true, run_commit_callbacks: false) - @connection = connection - @starting_isolation_level = current_isolation_level if isolation - super + if (ActiveRecord.version <=> Gem::Version.new("6.1")) != -1 + def initialize(connection, **args) + @connection = connection + @starting_isolation_level = current_isolation_level if args[:isolation] + super + end + else + def initialize(connection, options, **args) + @connection = connection + @starting_isolation_level = current_isolation_level if options[:isolation] + super + end end def commit From 168ef28243e4c860de7cf76488fcd37c917b59d0 Mon Sep 17 00:00:00 2001 From: Circuit8 Date: Mon, 22 Feb 2021 10:47:50 +0000 Subject: [PATCH 8/9] Fixed broken test --- test/cases/column_test_sqlserver.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/cases/column_test_sqlserver.rb b/test/cases/column_test_sqlserver.rb index 532f65837..6134e49e4 100644 --- a/test/cases/column_test_sqlserver.rb +++ b/test/cases/column_test_sqlserver.rb @@ -99,7 +99,7 @@ def assert_obj_set_and_save(attribute, value) _(col.sql_type).must_equal "bit" _(col.type).must_equal :boolean _(col.null).must_equal true - _(col.default).must_equal true + _(col.default).must_equal "1" _(obj.bit).must_equal true _(col.default_function).must_be_nil type = connection.lookup_cast_type_from_column(col) From d88d30a3d3a9e1371ec290a9f6e613fda5cdf9d8 Mon Sep 17 00:00:00 2001 From: Circuit8 Date: Mon, 22 Feb 2021 10:54:42 +0000 Subject: [PATCH 9/9] Fixed another broken test --- test/cases/column_test_sqlserver.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/cases/column_test_sqlserver.rb b/test/cases/column_test_sqlserver.rb index 6134e49e4..f4809be01 100644 --- a/test/cases/column_test_sqlserver.rb +++ b/test/cases/column_test_sqlserver.rb @@ -99,7 +99,7 @@ def assert_obj_set_and_save(attribute, value) _(col.sql_type).must_equal "bit" _(col.type).must_equal :boolean _(col.null).must_equal true - _(col.default).must_equal "1" + _(col.default).must_equal 1 _(obj.bit).must_equal true _(col.default_function).must_be_nil type = connection.lookup_cast_type_from_column(col)