Skip to content

Commit 3fb4e2d

Browse files
committed
Fix test failing under fresh Sequel by guessing types
It's not clear what changed across c. 50 (!) releases of Sequel but it no longer returns :datetime for columns with altered precision. This commits fixes the issue by mapping raw DB types to the known symbols. It should not backfire.
1 parent 7c7d4bb commit 3fb4e2d

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

lib/rom/sql/schema/type_builder.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ def self.[](db_type)
3737

3838
numeric_pk_type Types::Serial
3939

40+
TYPE_GUESSES = {
41+
/datetime\((\d+)\)/ => :datetime
42+
}.freeze
43+
4044
def call(primary_key:, db_type:, type:, allow_null:, **rest)
4145
if primary_key
4246
map_pk_type(type, db_type, **rest)
@@ -64,7 +68,7 @@ def map_pk_type(_ruby_type, _db_type, **)
6468

6569
# @api private
6670
def map_type(ruby_type, db_type, **kw)
67-
type = self.class.ruby_type_mapping[ruby_type]
71+
type = self.class.ruby_type_mapping[ruby_type || guess_type(db_type)]
6872

6973
if db_type.is_a?(String) && db_type.include?('numeric') || db_type.include?('decimal')
7074
map_decimal_type(db_type)
@@ -75,6 +79,15 @@ def map_type(ruby_type, db_type, **kw)
7579
end
7680
end
7781

82+
# @api private
83+
def guess_type(db_type)
84+
TYPE_GUESSES.find do |regex, type|
85+
if db_type.is_a?(String) && db_type.match(regex)
86+
break type
87+
end
88+
end
89+
end
90+
7891
# @api private
7992
def map_decimal_type(type)
8093
precision = DECIMAL_REGEX.match(type)

0 commit comments

Comments
 (0)