Skip to content

Commit 3af0407

Browse files
committed
support proc default values in ruby 3.2
Fixes: ``` NoMethodError: undefined method `=~' for #<Proc:.. ``` As #1032 explains `=~` [was removed](https://bugs.ruby-lang.org/issues/15231) from Proc. `Proc` are handled by [this line of code](https://github.com/rails/rails/blob/v7.0.4.2/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb#L96) The solution is to add a check and call `~=` only when `value` is a `String`. I took the oportunity to also follow rubocop performance suggestions ``` Performance/StringInclude: Use String#include? instead of a regex match with literal-only pattern. ``` Got the idea from postgresql adapter (rails/rails@5726b1d#diff-d8e4ab5c8c59df4fc1bc85457af0d07b88bd842a287233617a97ad2436fcb842R120)
1 parent 2094cab commit 3af0407

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#### Fixed
44

55
- [#1029](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/1029) Handle views defined in other databases.
6+
- [#1033](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/1033) Support proc default values in ruby 3.2.
7+
68

79
#### Changed
810

lib/active_record/connection_adapters/sqlserver/quoting.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def quote_column_name(name)
3939

4040
def quote_default_expression(value, column)
4141
cast_type = lookup_cast_type(column.sql_type)
42-
if cast_type.type == :uuid && value =~ /\(\)/
42+
if cast_type.type == :uuid && value.is_a?(String) && value.include?('()')
4343
value
4444
else
4545
super

test/cases/uuid_test_sqlserver.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,12 @@ class SQLServerUuidTest < ActiveRecord::TestCase
4343
obj = with_use_output_inserted_disabled { SSTestUuid.create!(name: "😢") }
4444
_(obj.id).must_be :nil?
4545
end
46+
47+
it "can add column with proc as default" do
48+
table_name = SSTestUuid.table_name
49+
connection.add_column table_name, :thingy, :uuid, null: false, default: -> { "NEWSEQUENTIALID()" }
50+
SSTestUuid.reset_column_information
51+
column = SSTestUuid.columns_hash["thingy"]
52+
_(column.default_function).must_equal "newsequentialid()"
53+
end
4654
end

0 commit comments

Comments
 (0)