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.rb42
1 files changed, 24 insertions, 18 deletions
diff --git a/activerecord/lib/active_record/schema_dumper.rb b/activerecord/lib/active_record/schema_dumper.rb
index c8e1b4f53a..22cd1cbe0c 100644
--- a/activerecord/lib/active_record/schema_dumper.rb
+++ b/activerecord/lib/active_record/schema_dumper.rb
@@ -76,6 +76,7 @@ HEADER
def table(table, stream)
columns = @connection.columns(table)
begin
+ column_specs = columns.dup
tbl = StringIO.new
# first dump primary key column
@@ -86,9 +87,12 @@ HEADER
end
tbl.print " create_table #{table.inspect}"
- if columns.detect { |c| c.name == pk }
- if pk != 'id'
- tbl.print %Q(, :primary_key => "#{pk}")
+ if pkc = column_specs.detect { |c| c.name == pk }
+ if pkc.limit != @types[:primary_key][:limit]
+ tbl.print ", :id => false"
+ else
+ column_specs.delete(pkc)
+ tbl.print %Q(, :primary_key => "#{pk}") if pk != 'id'
end
else
tbl.print ", :id => false"
@@ -97,27 +101,29 @@ HEADER
tbl.puts " do |t|"
# then dump all non-primary key columns
- column_specs = columns.map do |column|
+ column_specs.map! do |column|
raise StandardError, "Unknown type '#{column.sql_type}' for column '#{column.name}'" if @types[column.type].nil?
- next if column.name == pk
spec = {}
spec[:name] = column.name.inspect
-
- # AR has an optimisation which handles zero-scale decimals as integers. This
- # code ensures that the dumper still dumps the column as a decimal.
- spec[:type] = if column.type == :integer && [/^numeric/, /^decimal/].any? { |e| e.match(column.sql_type) }
- 'decimal'
- else
- column.type.to_s
- end
spec[:limit] = column.limit.inspect if column.limit != @types[column.type][:limit] && spec[:type] != 'decimal'
- spec[:precision] = column.precision.inspect if !column.precision.nil?
- spec[:scale] = column.scale.inspect if !column.scale.nil?
- spec[:null] = 'false' if !column.null
- spec[:default] = default_string(column.default) if column.has_default?
+ if column.name == pk
+ spec[:type] = 'primary_key'
+ else
+ # AR has an optimisation which handles zero-scale decimals as integers. This
+ # code ensures that the dumper still dumps the column as a decimal.
+ spec[:type] = if column.type == :integer && [/^numeric/, /^decimal/].any? { |e| e.match(column.sql_type) }
+ 'decimal'
+ else
+ column.type.to_s
+ end
+ spec[:precision] = column.precision.inspect if !column.precision.nil?
+ spec[:scale] = column.scale.inspect if !column.scale.nil?
+ spec[:null] = 'false' if !column.null
+ spec[:default] = default_string(column.default) if column.has_default?
+ end
(spec.keys - [:name, :type]).each{ |k| spec[k].insert(0, "#{k.inspect} => ")}
spec
- end.compact
+ end
# find all migration keys used in this table
keys = [:name, :limit, :precision, :scale, :default, :null] & column_specs.map(&:keys).flatten