aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/schema_dumper.rb
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord/lib/active_record/schema_dumper.rb')
-rw-r--r--activerecord/lib/active_record/schema_dumper.rb39
1 files changed, 17 insertions, 22 deletions
diff --git a/activerecord/lib/active_record/schema_dumper.rb b/activerecord/lib/active_record/schema_dumper.rb
index ab2d64e903..c1c6519cfa 100644
--- a/activerecord/lib/active_record/schema_dumper.rb
+++ b/activerecord/lib/active_record/schema_dumper.rb
@@ -115,9 +115,7 @@ HEADER
pkcol = columns.detect { |c| c.name == pk }
pkcolspec = @connection.column_spec_for_primary_key(pkcol)
if pkcolspec.present?
- pkcolspec.each do |key, value|
- tbl.print ", #{key}: #{value}"
- end
+ tbl.print ", #{format_colspec(pkcolspec)}"
end
when Array
tbl.print ", primary_key: #{pk.inspect}"
@@ -128,26 +126,19 @@ HEADER
table_options = @connection.table_options(table)
if table_options.present?
- table_options.each do |key, value|
- tbl.print ", #{key}: #{value.inspect}" if value.present?
- end
+ tbl.print ", #{format_options(table_options)}"
end
tbl.puts " do |t|"
# then dump all non-primary key columns
- column_specs = columns.map do |column|
+ columns.each do |column|
raise StandardError, "Unknown type '#{column.sql_type}' for column '#{column.name}'" unless @connection.valid_type?(column.type)
next if column.name == pk
- @connection.column_spec(column)
- end.compact
-
- # find all migration keys used in this table
- keys = @connection.migration_keys
-
- column_specs.each do |colspec|
- values = keys.map { |key| colspec[key] }.compact
- tbl.puts " t.#{colspec[:type]} #{values.join(", ")}"
+ type, colspec = @connection.column_spec(column)
+ tbl.print " t.#{type} #{column.name.inspect}"
+ tbl.print ", #{format_colspec(colspec)}" if colspec.present?
+ tbl.puts
end
indexes_in_create(table, tbl)
@@ -194,12 +185,8 @@ HEADER
"name: #{index.name.inspect}",
]
index_parts << "unique: true" if index.unique
-
- index_lengths = (index.lengths || []).compact
- index_parts << "length: #{Hash[index.columns.zip(index.lengths)].inspect}" if index_lengths.any?
-
- index_orders = index.orders || {}
- index_parts << "order: #{index.orders.inspect}" if index_orders.any?
+ index_parts << "length: { #{format_options(index.lengths)} }" if index.lengths.present?
+ index_parts << "order: { #{format_options(index.orders)} }" if index.orders.present?
index_parts << "where: #{index.where.inspect}" if index.where
index_parts << "using: #{index.using.inspect}" if index.using
index_parts << "type: #{index.type.inspect}" if index.type
@@ -237,6 +224,14 @@ HEADER
end
end
+ def format_colspec(colspec)
+ colspec.map { |key, value| "#{key}: #{value}" }.join(", ")
+ end
+
+ def format_options(options)
+ options.map { |key, value| "#{key}: #{value.inspect}" }.join(", ")
+ end
+
def remove_prefix_and_suffix(table)
table.gsub(/^(#{@options[:table_name_prefix]})(.+)(#{@options[:table_name_suffix]})$/, "\\2")
end