Merge pull request #27008 from kirs/new-column-from-field

Refactor column initialization into `new_column_from_field`
This commit is contained in:
Rafael França 2016-11-11 14:08:37 -05:00 committed by GitHub
commit a5e933410d
4 changed files with 49 additions and 39 deletions

View File

@ -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?

View File

@ -398,18 +398,14 @@ 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|
type_metadata = fetch_type_metadata(field[:Type], field[:Extra])
if type_metadata.type == :datetime && field[:Default] == "CURRENT_TIMESTAMP"
default, default_function = nil, field[:Default]
else
default, default_function = field[:Default], nil
end
new_column(field[:Field], default, type_metadata, field[:Null] == "YES", table_name, default_function, field[:Collation], comment: field[:Comment].presence)
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]
else
default, default_function = field[:Default], nil
end
new_column(field[:Field], default, type_metadata, field[:Null] == "YES", table_name, default_function, field[:Collation], comment: field[:Comment].presence)
end
def table_comment(table_name) # :nodoc:

View File

@ -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|
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)
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)
PostgreSQLColumn.new(
column_name,
default_value,
type_metadata,
!notnull,
table_name,
default_function,
collation,
comment: comment.presence
)
end
def table_options(table_name) # :nodoc:

View File

@ -308,22 +308,26 @@ module ActiveRecord
def columns(table_name) # :nodoc:
table_name = table_name.to_s
table_structure(table_name).map do |field|
case field["dflt_value"]
when /^null$/i
field["dflt_value"] = nil
when /^'(.*)'$/m
field["dflt_value"] = $1.gsub("''", "'")
when /^"(.*)"$/m
field["dflt_value"] = $1.gsub('""', '"')
end
collation = field["collation"]
sql_type = field["type"]
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)
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
when /^'(.*)'$/m
field["dflt_value"] = $1.gsub("''", "'")
when /^"(.*)"$/m
field["dflt_value"] = $1.gsub('""', '"')
end
collation = field["collation"]
sql_type = field["type"]
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
# Returns an array of indexes for the given table.
def indexes(table_name, name = nil) #:nodoc:
exec_query("PRAGMA index_list(#{quote_table_name(table_name)})", "SCHEMA").map do |row|