diff options
-rwxr-xr-x | activerecord/lib/active_record/connection_adapters/mysql_adapter.rb | 10 | ||||
-rw-r--r-- | activerecord/test/schema_dumper_test.rb | 7 |
2 files changed, 17 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb index 65faa80065..5242151736 100755 --- a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb @@ -440,6 +440,16 @@ module ActiveRecord variables.first['Value'] unless variables.empty? end + # Returns a table's primary key and belonging sequence. + def pk_and_sequence_for(table) #:nodoc: + table_desc_result = execute("describe #{table}") + keys = [] + execute("describe #{table}").each_hash do |h| + keys << h["Field"]if h["Key"] == "PRI" + end + keys.length == 1 ? [keys.first, nil] : nil + end + private def connect encoding = @config[:encoding] diff --git a/activerecord/test/schema_dumper_test.rb b/activerecord/test/schema_dumper_test.rb index fa2b8bdf24..c6041e4f5c 100644 --- a/activerecord/test/schema_dumper_test.rb +++ b/activerecord/test/schema_dumper_test.rb @@ -110,6 +110,13 @@ if ActiveRecord::Base.connection.respond_to?(:tables) output = standard_dump assert_match %r{t.text\s+"body",\s+:default => "",\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 end def test_schema_dump_includes_decimal_options |