mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #27008 from kirs/new-column-from-field
Refactor column initialization into `new_column_from_field`
This commit is contained in:
commit
a5e933410d
4 changed files with 49 additions and 39 deletions
|
@ -161,6 +161,14 @@ module ActiveRecord
|
|||
SchemaCreation.new self
|
||||
end
|
||||
|
||||
# Returns an array of +Column+ objects for the table specified by +table_name+.
|
||||
def columns(table_name) # :nodoc:
|
||||
table_name = table_name.to_s
|
||||
column_definitions(table_name).map do |field|
|
||||
new_column_from_field(table_name, field)
|
||||
end
|
||||
end
|
||||
|
||||
# this method must only be called while holding connection pool's mutex
|
||||
def lease
|
||||
if in_use?
|
||||
|
|
|
@ -398,10 +398,7 @@ module ActiveRecord
|
|||
indexes
|
||||
end
|
||||
|
||||
# Returns an array of +Column+ objects for the table specified by +table_name+.
|
||||
def columns(table_name) # :nodoc:
|
||||
table_name = table_name.to_s
|
||||
column_definitions(table_name).map do |field|
|
||||
def new_column_from_field(table_name, field) # :nodoc:
|
||||
type_metadata = fetch_type_metadata(field[:Type], field[:Extra])
|
||||
if type_metadata.type == :datetime && field[:Default] == "CURRENT_TIMESTAMP"
|
||||
default, default_function = nil, field[:Default]
|
||||
|
@ -410,7 +407,6 @@ module ActiveRecord
|
|||
end
|
||||
new_column(field[:Field], default, type_metadata, field[:Null] == "YES", table_name, default_function, field[:Collation], comment: field[:Comment].presence)
|
||||
end
|
||||
end
|
||||
|
||||
def table_comment(table_name) # :nodoc:
|
||||
schema, name = extract_schema_qualified_name(table_name)
|
||||
|
|
|
@ -221,21 +221,23 @@ module ActiveRecord
|
|||
end.compact
|
||||
end
|
||||
|
||||
# Returns the list of all column definitions for a table.
|
||||
def columns(table_name) # :nodoc:
|
||||
table_name = table_name.to_s
|
||||
column_definitions(table_name).map do |column_name, type, default, notnull, oid, fmod, collation, comment|
|
||||
def new_column_from_field(table_name, field) # :nondoc:
|
||||
column_name, type, default, notnull, oid, fmod, collation, comment = field
|
||||
oid = oid.to_i
|
||||
fmod = fmod.to_i
|
||||
type_metadata = fetch_type_metadata(column_name, type, oid, fmod)
|
||||
default_value = extract_value_from_default(default)
|
||||
default_function = extract_default_function(default_value, default)
|
||||
new_column(column_name, default_value, type_metadata, !notnull, table_name, default_function, collation, comment: comment.presence)
|
||||
end
|
||||
end
|
||||
|
||||
def new_column(*args) # :nodoc:
|
||||
PostgreSQLColumn.new(*args)
|
||||
PostgreSQLColumn.new(
|
||||
column_name,
|
||||
default_value,
|
||||
type_metadata,
|
||||
!notnull,
|
||||
table_name,
|
||||
default_function,
|
||||
collation,
|
||||
comment: comment.presence
|
||||
)
|
||||
end
|
||||
|
||||
def table_options(table_name) # :nodoc:
|
||||
|
|
|
@ -308,6 +308,11 @@ module ActiveRecord
|
|||
def columns(table_name) # :nodoc:
|
||||
table_name = table_name.to_s
|
||||
table_structure(table_name).map do |field|
|
||||
new_column_from_field(table_name, field)
|
||||
end
|
||||
end
|
||||
|
||||
def new_column_from_field(table_name, field) # :nondoc:
|
||||
case field["dflt_value"]
|
||||
when /^null$/i
|
||||
field["dflt_value"] = nil
|
||||
|
@ -322,7 +327,6 @@ module ActiveRecord
|
|||
type_metadata = fetch_type_metadata(sql_type)
|
||||
new_column(field["name"], field["dflt_value"], type_metadata, field["notnull"].to_i == 0, table_name, nil, collation)
|
||||
end
|
||||
end
|
||||
|
||||
# Returns an array of indexes for the given table.
|
||||
def indexes(table_name, name = nil) #:nodoc:
|
||||
|
|
Loading…
Reference in a new issue