Skip to content

Commit a5e9334

Browse files
authored
Merge pull request rails#27008 from kirs/new-column-from-field
Refactor column initialization into `new_column_from_field`
2 parents cac3be6 + 3dce96a commit a5e9334

File tree

4 files changed

+48
-38
lines changed

4 files changed

+48
-38
lines changed

activerecord/lib/active_record/connection_adapters/abstract_adapter.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,14 @@ def schema_creation
161161
SchemaCreation.new self
162162
end
163163

164+
# Returns an array of +Column+ objects for the table specified by +table_name+.
165+
def columns(table_name) # :nodoc:
166+
table_name = table_name.to_s
167+
column_definitions(table_name).map do |field|
168+
new_column_from_field(table_name, field)
169+
end
170+
end
171+
164172
# this method must only be called while holding connection pool's mutex
165173
def lease
166174
if in_use?

activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -398,18 +398,14 @@ def indexes(table_name, name = nil) #:nodoc:
398398
indexes
399399
end
400400

401-
# Returns an array of +Column+ objects for the table specified by +table_name+.
402-
def columns(table_name) # :nodoc:
403-
table_name = table_name.to_s
404-
column_definitions(table_name).map do |field|
405-
type_metadata = fetch_type_metadata(field[:Type], field[:Extra])
406-
if type_metadata.type == :datetime && field[:Default] == "CURRENT_TIMESTAMP"
407-
default, default_function = nil, field[:Default]
408-
else
409-
default, default_function = field[:Default], nil
410-
end
411-
new_column(field[:Field], default, type_metadata, field[:Null] == "YES", table_name, default_function, field[:Collation], comment: field[:Comment].presence)
401+
def new_column_from_field(table_name, field) # :nodoc:
402+
type_metadata = fetch_type_metadata(field[:Type], field[:Extra])
403+
if type_metadata.type == :datetime && field[:Default] == "CURRENT_TIMESTAMP"
404+
default, default_function = nil, field[:Default]
405+
else
406+
default, default_function = field[:Default], nil
412407
end
408+
new_column(field[:Field], default, type_metadata, field[:Null] == "YES", table_name, default_function, field[:Collation], comment: field[:Comment].presence)
413409
end
414410

415411
def table_comment(table_name) # :nodoc:

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

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -221,21 +221,23 @@ def indexes(table_name, name = nil)
221221
end.compact
222222
end
223223

224-
# Returns the list of all column definitions for a table.
225-
def columns(table_name) # :nodoc:
226-
table_name = table_name.to_s
227-
column_definitions(table_name).map do |column_name, type, default, notnull, oid, fmod, collation, comment|
228-
oid = oid.to_i
229-
fmod = fmod.to_i
230-
type_metadata = fetch_type_metadata(column_name, type, oid, fmod)
231-
default_value = extract_value_from_default(default)
232-
default_function = extract_default_function(default_value, default)
233-
new_column(column_name, default_value, type_metadata, !notnull, table_name, default_function, collation, comment: comment.presence)
234-
end
235-
end
236-
237-
def new_column(*args) # :nodoc:
238-
PostgreSQLColumn.new(*args)
224+
def new_column_from_field(table_name, field) # :nondoc:
225+
column_name, type, default, notnull, oid, fmod, collation, comment = field
226+
oid = oid.to_i
227+
fmod = fmod.to_i
228+
type_metadata = fetch_type_metadata(column_name, type, oid, fmod)
229+
default_value = extract_value_from_default(default)
230+
default_function = extract_default_function(default_value, default)
231+
PostgreSQLColumn.new(
232+
column_name,
233+
default_value,
234+
type_metadata,
235+
!notnull,
236+
table_name,
237+
default_function,
238+
collation,
239+
comment: comment.presence
240+
)
239241
end
240242

241243
def table_options(table_name) # :nodoc:

activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -308,20 +308,24 @@ def view_exists?(view_name) # :nodoc:
308308
def columns(table_name) # :nodoc:
309309
table_name = table_name.to_s
310310
table_structure(table_name).map do |field|
311-
case field["dflt_value"]
312-
when /^null$/i
313-
field["dflt_value"] = nil
314-
when /^'(.*)'$/m
315-
field["dflt_value"] = $1.gsub("''", "'")
316-
when /^"(.*)"$/m
317-
field["dflt_value"] = $1.gsub('""', '"')
318-
end
311+
new_column_from_field(table_name, field)
312+
end
313+
end
319314

320-
collation = field["collation"]
321-
sql_type = field["type"]
322-
type_metadata = fetch_type_metadata(sql_type)
323-
new_column(field["name"], field["dflt_value"], type_metadata, field["notnull"].to_i == 0, table_name, nil, collation)
315+
def new_column_from_field(table_name, field) # :nondoc:
316+
case field["dflt_value"]
317+
when /^null$/i
318+
field["dflt_value"] = nil
319+
when /^'(.*)'$/m
320+
field["dflt_value"] = $1.gsub("''", "'")
321+
when /^"(.*)"$/m
322+
field["dflt_value"] = $1.gsub('""', '"')
324323
end
324+
325+
collation = field["collation"]
326+
sql_type = field["type"]
327+
type_metadata = fetch_type_metadata(sql_type)
328+
new_column(field["name"], field["dflt_value"], type_metadata, field["notnull"].to_i == 0, table_name, nil, collation)
325329
end
326330

327331
# Returns an array of indexes for the given table.

0 commit comments

Comments
 (0)