Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #9971 (closed defect: fixed)

Opened 10 months ago

Last modified 10 months ago

[PATCH] Make mysql adapter keep non-standard primary key names in SchemaDumper.dump

Reported by: RubyRedRick Assigned to: core
Priority: normal Milestone: 2.x
Component: ActiveRecord Version: edge
Severity: normal Keywords:
Cc:

Description

If a table has a primary key which has a name other than 'id', then dumping the schema ruby file produced by using ActiveRecord::SchemaDumper.dump (as rake db:schema:dump uses to produce db/schema.rb) does not define ANY primary key for the table.

SchemaDumper has a mechanism to handle this, and it's used by other adapters such as Oracle and Postgresql. The mysql adapter doesn't implement the pk_and_sequence_for method which is needed to find such primary keys.

This can cause havoc when you do something like rake db:test:prepare I've found that most of my ActiveRecord tests LIKE to have primary keys defined in the database. <G>

The attached patch fixes this along with a test.

Attachments

mysql_schema_dump_pk_patch.diff (2.0 kB) - added by RubyRedRick on 10/24/07 14:00:51.
mysql_schema_dump_pk_patch2.diff (1.6 kB) - added by RubyRedRick on 10/24/07 17:04:23.

Change History

10/24/07 14:00:51 changed by RubyRedRick

  • attachment mysql_schema_dump_pk_patch.diff added.

10/24/07 17:04:23 changed by RubyRedRick

  • attachment mysql_schema_dump_pk_patch2.diff added.

10/24/07 17:04:38 changed by RubyRedRick

I just discovered that the patch won't apply to the current edge due to changes to use migrations to populate the test db.

I've attached a new patch which works with the current edge.

10/24/07 17:41:25 changed by ntalbott

+1

(follow-up: ↓ 4 ) 10/25/07 06:42:08 changed by nzkoz

  • status changed from new to closed.
  • resolution set to fixed.

(In [8012]) Make sure that the Schema Dumper supports non-standard primary keys with MySQL. Closes #9971 [RubyRedRick]

(in reply to: ↑ 3 ) 10/25/07 18:46:10 changed by jkit

I believe line 445 of the applied patch is superfluous, since line 447 also executes the "describe #{table}" command.

443           # Returns a table's primary key and belonging sequence. 
444           def pk_and_sequence_for(table) #:nodoc: 
445             table_desc_result = execute("describe #{table}") 
446             keys = [] 
447             execute("describe #{table}").each_hash do |h| 
448               keys << h["Field"]if h["Key"] == "PRI" 
449             end 
450             keys.length == 1 ? [keys.first, nil] : nil 
451           end 

10/25/07 18:53:12 changed by nzkoz

(In [8015]) Remove superfluous code and quote table name. References #9971 [jkit, nzkoz]