diff options
author | Rizwan Reza <rizwanreza@gmail.com> | 2010-03-27 15:17:07 +0430 |
---|---|---|
committer | wycats <wycats@gmail.com> | 2010-03-27 03:49:53 -0700 |
commit | faeca694b3d4afebf6b623b493e86731e773c462 (patch) | |
tree | f63f5d10cdefa1df2e74fc4ffe446ad0a3e79e47 /activerecord/lib/active_record/schema_dumper.rb | |
parent | 0a352056a4055c170e62a521267f5bc51c6659a0 (diff) | |
download | rails-faeca694b3d4afebf6b623b493e86731e773c462.tar.gz rails-faeca694b3d4afebf6b623b493e86731e773c462.tar.bz2 rails-faeca694b3d4afebf6b623b493e86731e773c462.zip |
primary_key now supports :limit for MySQL
Signed-off-by: wycats <wycats@gmail.com>
Diffstat (limited to 'activerecord/lib/active_record/schema_dumper.rb')
-rw-r--r-- | activerecord/lib/active_record/schema_dumper.rb | 42 |
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 |