Skip to content
This repository
Browse code

Barckport to 3-1-stable: fixed an issue id false option is ignored on…

… mysql/mysql2 (fix #3440)
  • Loading branch information...
commit fb734233707b6802d613cb50d75ff3d5eb1ea1c5 1 parent 3cd33ea
Toshinori Kajihara authored November 06, 2011
3  activerecord/CHANGELOG.md
Source Rendered
@@ -20,6 +20,9 @@
20 20
 
21 21
     *Jon Leighton*
22 22
 
  23
+*   MySQL: use the information_schema than the describe command when we look for a primary key. *GH #3440*
  24
+    *Kenny J*
  25
+
23 26
 ## Rails 3.1.1 (October 7, 2011) ##
24 27
 
25 28
 *   Raise an exception if the primary key of a model in an association is needed
13  activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb
@@ -573,9 +573,18 @@ def show_variable(name)
573 573
       # Returns a table's primary key and belonging sequence.
574 574
       def pk_and_sequence_for(table)
575 575
         keys = []
576  
-        result = execute("DESCRIBE #{quote_table_name(table)}", 'SCHEMA')
  576
+        sql = <<-SQL
  577
+          SELECT t.constraint_type, k.column_name
  578
+          FROM information_schema.table_constraints t
  579
+          JOIN information_schema.key_column_usage k
  580
+          USING (constraint_name, table_schema, table_name)
  581
+          WHERE t.table_schema = DATABASE()
  582
+            AND t.table_name   = '#{table}'
  583
+        SQL
  584
+
  585
+        result = execute(sql, 'SCHEMA')
577 586
         result.each(:symbolize_keys => true, :as => :hash) do |row|
578  
-          keys << row[:Field] if row[:Key] == "PRI"
  587
+          keys << row[:column_name] if row[:constraint_type] == "PRIMARY KEY"
579 588
         end
580 589
         keys.length == 1 ? [keys.first, nil] : nil
581 590
       end
13  activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
@@ -756,9 +756,18 @@ def show_variable(name)
756 756
       # Returns a table's primary key and belonging sequence.
757 757
       def pk_and_sequence_for(table) #:nodoc:
758 758
         keys = []
759  
-        result = execute("describe #{quote_table_name(table)}", 'SCHEMA')
  759
+        sql = <<-SQL
  760
+          SELECT t.constraint_type, k.column_name
  761
+          FROM information_schema.table_constraints t
  762
+          JOIN information_schema.key_column_usage k
  763
+          USING (constraint_name, table_schema, table_name)
  764
+          WHERE t.table_schema = DATABASE()
  765
+            AND t.table_name   = '#{table}'
  766
+        SQL
  767
+
  768
+        result = execute(sql, 'SCHEMA')
760 769
         result.each_hash do |h|
761  
-          keys << h["Field"]if h["Key"] == "PRI"
  770
+          keys << h["column_name"] if h["constraint_type"] == "PRIMARY KEY"
762 771
         end
763 772
         result.free
764 773
         keys.length == 1 ? [keys.first, nil] : nil
5  activerecord/test/cases/schema_dumper_test.rb
@@ -239,4 +239,9 @@ def test_schema_dump_keeps_id_column_when_id_is_false_and_id_column_added
239 239
     assert_match %r(:id => false), match[1], "no table id not preserved"
240 240
     assert_match %r{t.string[[:space:]]+"id",[[:space:]]+:null => false$}, match[2], "non-primary key id column not preserved"
241 241
   end
  242
+
  243
+  def test_schema_dump_keeps_id_false_when_id_is_false_and_unique_not_null_column_added
  244
+    output = standard_dump
  245
+    assert_match %r{create_table "subscribers", :id => false}, output
  246
+  end
242 247
 end

0 notes on commit fb73423

Please sign in to comment.
Something went wrong with that request. Please try again.