Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add support for dumping non-standard primary keys when using the SQLi…

…te3 adapter. Fix unit tests so that this feature is tested for all adapters. [#2868 state:resolved]

Signed-off-by: Yehuda Katz <wycats@yehuda-katzs-macbookpro41.local>
  • Loading branch information...
commit 187d90f7529e02c3c863e6b68b45d8e34f315140 1 parent 1e2d722
Hongli Lai FooBarWidget authored Yehuda Katz committed
6 activerecord/lib/active_record/schema_dumper.rb
View
@@ -78,11 +78,14 @@ def table(table, stream)
begin
tbl = StringIO.new
+ # first dump primary key column
if @connection.respond_to?(:pk_and_sequence_for)
pk, pk_seq = @connection.pk_and_sequence_for(table)
+ elsif @connection.respond_to?(:primary_key)
+ pk = @connection.primary_key(table)
end
pk ||= 'id'
-
+
tbl.print " create_table #{table.inspect}"
if columns.detect { |c| c.name == pk }
if pk != 'id'
@@ -94,6 +97,7 @@ def table(table, stream)
tbl.print ", :force => true"
tbl.puts " do |t|"
+ # then dump all non-primary key columns
column_specs = columns.map do |column|
raise StandardError, "Unknown type '#{column.sql_type}' for column '#{column.name}'" if @types[column.type].nil?
next if column.name == pk
14 activerecord/test/cases/schema_dumper_test.rb
View
@@ -156,6 +156,13 @@ def test_schema_dumps_index_columns_in_right_order
index_definition = standard_dump.split(/\n/).grep(/add_index.*companies/).first.strip
assert_equal 'add_index "companies", ["firm_id", "type", "rating", "ruby_type"], :name => "company_index"', index_definition
end
+
+ def test_schema_dump_should_honor_nonstandard_primary_keys
+ output = standard_dump
+ match = output.match(%r{create_table "movies"(.*)do})
+ assert_not_nil(match, "nonstandardpk table not found")
+ assert_match %r(:primary_key => "movieid"), match[1], "non-standard primary key not preserved"
+ end
if current_adapter?(:MysqlAdapter)
def test_schema_dump_should_not_add_default_value_for_mysql_text_field
@@ -163,13 +170,6 @@ def test_schema_dump_should_not_add_default_value_for_mysql_text_field
assert_match %r{t.text\s+"body",\s+:null => false$}, output
end
- def test_mysql_schema_dump_should_honor_nonstandard_primary_keys
- output = standard_dump
- match = output.match(%r{create_table "movies"(.*)do})
- assert_not_nil(match, "nonstandardpk table not found")
- assert_match %r(:primary_key => "movieid"), match[1], "non-standard primary key not preserved"
- end
-
def test_schema_dump_includes_length_for_mysql_blob_and_text_fields
output = standard_dump
assert_match %r{t.binary\s+"tiny_blob",\s+:limit => 255$}, output
Please sign in to comment.
Something went wrong with that request. Please try again.