Skip to content

Commit 20f6edf

Browse files
committed
Avoid checking if internal objects respond to methods
We can just make sure all objects used in that method have the same interface.
1 parent fc3efcb commit 20f6edf

File tree

4 files changed

+14
-5
lines changed

4 files changed

+14
-5
lines changed

activerecord/lib/active_record/connection_adapters/abstract/quoting.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def quote_default_expression(value, column) # :nodoc:
147147
value.call
148148
else
149149
# TODO: Remove fetch_cast_type and the need for connection after we release 8.1.
150-
cast_type = column.respond_to?(:fetch_cast_type) ? column.fetch_cast_type(self) : lookup_cast_type(column.sql_type)
150+
cast_type = column.fetch_cast_type(self)
151151
value = cast_type.serialize(value)
152152
quote(value)
153153
end

activerecord/lib/active_record/connection_adapters/abstract/schema_creation.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def accept(o)
1717
:options_include_default?, :supports_indexes_in_create?, :use_foreign_keys?,
1818
:quoted_columns_for_index, :supports_partial_index?, :supports_check_constraints?,
1919
:supports_index_include?, :supports_exclusion_constraints?, :supports_unique_constraints?,
20-
:supports_nulls_not_distinct?,
20+
:supports_nulls_not_distinct?, :lookup_cast_type,
2121
to: :@conn, private: true
2222

2323
private
@@ -148,7 +148,7 @@ def column_options(o)
148148
end
149149

150150
def add_column_options!(sql, options)
151-
sql << " DEFAULT #{quote_default_expression(options[:default], options[:column])}" if options_include_default?(options)
151+
sql << " DEFAULT #{quote_default_expression_for_column_definition(options[:default], options[:column])}" if options_include_default?(options)
152152
# must explicitly check for :null to allow change_column to work on migrations
153153
if options[:null] == false
154154
sql << " NOT NULL"
@@ -162,6 +162,11 @@ def add_column_options!(sql, options)
162162
sql
163163
end
164164

165+
def quote_default_expression_for_column_definition(default, column_definition)
166+
column_definition.cast_type = lookup_cast_type(column_definition.sql_type)
167+
quote_default_expression(default, column_definition)
168+
end
169+
165170
def to_sql(sql)
166171
sql = sql.to_sql if sql.respond_to?(:to_sql)
167172
sql

activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def concise_options(options)
7575
# are typically created by methods in TableDefinition, and added to the
7676
# +columns+ attribute of said TableDefinition object, in order to be used
7777
# for generating a number of table creation or table changing SQL statements.
78-
ColumnDefinition = Struct.new(:name, :type, :options, :sql_type) do # :nodoc:
78+
ColumnDefinition = Struct.new(:name, :type, :options, :sql_type, :cast_type) do # :nodoc:
7979
self::OPTION_NAMES = [
8080
:limit,
8181
:precision,
@@ -108,6 +108,10 @@ def #{option_name}=(value)
108108
def aliased_types(name, fallback)
109109
"timestamp" == name ? :datetime : fallback
110110
end
111+
112+
def fetch_cast_type(connection)
113+
cast_type
114+
end
111115
end
112116

113117
AddColumnDefinition = Struct.new(:column) # :nodoc:

activerecord/lib/active_record/connection_adapters/postgresql/schema_creation.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def visit_ChangeColumnDefinition(o)
9999
if options[:default].nil?
100100
change_column_sql << ", ALTER COLUMN #{quoted_column_name} DROP DEFAULT"
101101
else
102-
quoted_default = quote_default_expression(options[:default], column)
102+
quoted_default = quote_default_expression_for_column_definition(options[:default], column)
103103
change_column_sql << ", ALTER COLUMN #{quoted_column_name} SET DEFAULT #{quoted_default}"
104104
end
105105
end

0 commit comments

Comments
 (0)