Skip to content

Commit

Permalink
Fix test failing under fresh Sequel by guessing types
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
flash-gordon committed Dec 25, 2024
1 parent 58a49ba commit 68812bf
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion lib/rom/sql/schema/type_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ def self.[](db_type)

numeric_pk_type Types::Serial

TYPE_GUESSES = {
/datetime\((\d+)\)/ => :datetime
}.freeze

def call(primary_key:, db_type:, type:, allow_null:, **rest)
if primary_key
map_pk_type(type, db_type, **rest)
Expand Down Expand Up @@ -64,7 +68,7 @@ def map_pk_type(_ruby_type, _db_type, **)

# @api private
def map_type(ruby_type, db_type, **kw)
type = self.class.ruby_type_mapping[ruby_type]
type = self.class.ruby_type_mapping[ruby_type || guess_type(db_type)]

if (db_type.is_a?(String) && db_type.include?("numeric")) || db_type.include?("decimal")
map_decimal_type(db_type)
Expand All @@ -75,6 +79,15 @@ def map_type(ruby_type, db_type, **kw)
end
end

# @api private
def guess_type(db_type)
TYPE_GUESSES.find do |regex, type|
if db_type.is_a?(String) && db_type.match(regex)
break type
end
end
end

# @api private
def map_decimal_type(type)
precision = DECIMAL_REGEX.match(type)
Expand Down

0 comments on commit 68812bf

Please sign in to comment.