From dbce96a05054015f6bd2b89fa281a6924301b659 Mon Sep 17 00:00:00 2001 From: Jacob Swanner Date: Tue, 24 Nov 2009 15:31:11 -0500 Subject: [PATCH] ActiveRecord::SchemaDumper now keeps id column when ':id => false' and id column added as primary key --- activerecord/lib/active_record/schema_dumper.rb | 4 +++- activerecord/test/cases/schema_dumper_test.rb | 8 ++++++++ activerecord/test/schema/schema.rb | 7 +++++++ 3 files changed, 18 insertions(+), 1 deletions(-) diff --git a/activerecord/lib/active_record/schema_dumper.rb b/activerecord/lib/active_record/schema_dumper.rb index 68a45a0..af2aa18 100644 --- a/activerecord/lib/active_record/schema_dumper.rb +++ b/activerecord/lib/active_record/schema_dumper.rb @@ -86,11 +86,13 @@ HEADER end tbl.print " create_table #{table.inspect}" - if columns.detect { |c| c.name == pk } + if columns.detect { |c| c.name == pk && c.type == :integer } if pk != 'id' tbl.print %Q(, :primary_key => "#{pk}") end else + # force the pk column to be added in column_specs below + pk = '' tbl.print ", :id => false" end tbl.print ", :force => true" diff --git a/activerecord/test/cases/schema_dumper_test.rb b/activerecord/test/cases/schema_dumper_test.rb index ba714a9..6c3426b 100644 --- a/activerecord/test/cases/schema_dumper_test.rb +++ b/activerecord/test/cases/schema_dumper_test.rb @@ -207,5 +207,13 @@ class SchemaDumperTest < ActiveRecord::TestCase assert_match %r(:id => false), match[1], "no table id not preserved" assert_match %r{t.string[[:space:]]+"id",[[:space:]]+:null => false$}, match[2], "non-primary key id column not preserved" end + + def test_schema_dump_keeps_id_column_when_id_is_false_and_id_column_added_as_primary_key + output = standard_dump + match = output.match(%r{create_table "goofy_string_id_with_primary_key"(.*)do.*\n(.*)\n}) + assert_not_nil(match, "goofy_string_id_with_primary_key table not found") + assert_match %r(:id => false), match[1], "no table id not preserved" + assert_match %r{t.string[[:space:]]+"id",[[:space:]]+:null => false$}, match[2], "non-primary key id column not preserved" + end end diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index 984c5ba..977d443 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -185,6 +185,13 @@ ActiveRecord::Schema.define do t.string :info end + create_table :goofy_string_id_with_primary_key, :force => true, :id => false do |t| + t.string :id, :null => false + t.string :info + end + + execute "ALTER TABLE goofy_string_id_with_primary_key ADD PRIMARY KEY (#{quote_column_name 'id'})" + create_table :items, :force => true do |t| t.column :name, :integer end -- 1.6.4.4