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

Ticket #9971: mysql_schema_dump_pk_patch.diff

File mysql_schema_dump_pk_patch.diff, 2.0 kB (added by RubyRedRick, 1 year ago)
  • test/schema_dumper_test.rb

    old new  
    110110        output = standard_dump 
    111111        assert_match %r{t.text\s+"body",\s+:default => "",\s+:null => false$}, output 
    112112      end 
     113       
     114      def test_mysql_schema_dump_should_honor_nonstandard_primary_keys 
     115        output = standard_dump 
     116        match = output.match(%r{create_table "nonstandardpk"(.*)$}) 
     117        assert_not_nil(match, "nonstandardpk table not found")  
     118        assert_match %r(:primary_key => "NotId"), output, "non-standard primary key not preserved" 
     119      end 
    113120    end 
    114121 
    115122    def test_schema_dump_includes_decimal_options 
  • test/fixtures/db_definitions/mysql.sql

    old new  
    236236CREATE TABLE `minimalistics` ( 
    237237  `id` INTEGER NOT NULL auto_increment PRIMARY KEY 
    238238); 
     239 
     240CREATE TABLE `nonstandardpk` ( 
     241  `NotId` INTEGER NOT NULL auto_increment PRIMARY KEY 
     242); 
  • lib/active_record/connection_adapters/mysql_adapter.rb

    old new  
    438438      def show_variable(name) 
    439439        variables = select_all("SHOW VARIABLES LIKE '#{name}'") 
    440440        variables.first['Value'] unless variables.empty? 
     441      end  
     442       
     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 
    441451      end 
    442452 
    443453      private