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

Ticket #7085: schema_dumper.diff

File schema_dumper.diff, 5.1 kB (added by ben, 2 years ago)

Same as previous patch but without the migration changes

  • activesupport/test/inflector_test.rb

    old new  
    141141    "Person" => "personid", 
    142142    "MyApplication::Billing::Account" => "accountid" 
    143143  } 
     144   
     145  TableNameToPrimaryKeyWithUnderscore = { 
     146    'people' => 'person_id' 
     147  } 
    144148 
     149  TableNameToPrimaryKeyWithoutUnderscore = { 
     150    'people' => 'personid' 
     151  } 
     152   
    145153  ClassNameToTableName = { 
    146154    "PrimarySpokesman" => "primary_spokesmen", 
    147155    "NodeChild"        => "node_children" 
     
    265273      assert_equal(foreign_key, Inflector.foreign_key(klass, false)) 
    266274    end 
    267275  end 
     276   
     277  def test_primary_key 
     278    TableNameToPrimaryKeyWithUnderscore.each do |table, primary_key| 
     279      assert_equal(primary_key, Inflector.primary_key(table)) 
     280    end 
     281     
     282    TableNameToPrimaryKeyWithoutUnderscore.each do |table, primary_key| 
     283      assert_equal(primary_key, Inflector.primary_key(table, false)) 
     284    end 
     285  end 
    268286 
    269287  def test_tableize 
    270288    ClassNameToTableName.each do |class_name, table_name| 
  • activesupport/lib/active_support/inflector.rb

    old new  
    234234  def foreign_key(class_name, separate_class_name_and_id_with_underscore = true) 
    235235    underscore(demodulize(class_name)) + (separate_class_name_and_id_with_underscore ? "_id" : "id") 
    236236  end 
     237   
     238  def primary_key(table_name, separate_class_name_and_id_with_underscore = true) 
     239    singularize(table_name) + (separate_class_name_and_id_with_underscore ? "_id" : "id") 
     240  end 
    237241 
    238242  # Constantize tries to find a declared constant with the name specified 
    239243  # in the string. It raises a NameError when the name is not in CamelCase 
  • activerecord/test/schema_dumper_test.rb

    old new  
    9696      output = stream.string 
    9797      assert_match %r{:precision => 3,[[:space:]]+:scale => 2,[[:space:]]+:default => 2.78}, output 
    9898    end 
     99     
     100    def test_tablename_primary_key 
     101      ActiveRecord::Base.primary_key_prefix_type = :table_name 
     102      output = standard_dump 
     103      assert_no_match %r{create_table "prefixes", :id => false}, output 
     104      assert_match %r{create_table "prefixes", :primary_key => "prefixid"}, output 
     105      ActiveRecord::Base.primary_key_prefix_type = nil 
     106    end 
     107     
     108    def test_tablename_with_underscore_primary_key 
     109      ActiveRecord::Base.primary_key_prefix_type = :table_name_with_underscore 
     110      output = standard_dump 
     111      assert_no_match %r{create_table "underscored_prefixes", :id => false}, output 
     112      assert_match %r{create_table "underscored_prefixes", :primary_key => "underscored_prefix_id"}, output 
     113      ActiveRecord::Base.primary_key_prefix_type = nil 
     114    end 
    99115  end 
    100116 
    101117end 
  • activerecord/test/fixtures/db_definitions/mysql.sql

    old new  
    226226  `my_house_population` decimal(2), 
    227227  `decimal_number_with_default` decimal(3,2) DEFAULT 2.78 
    228228) TYPE=InnoDB; 
     229 
     230CREATE TABLE `prefixes` ( 
     231  `prefixid` int(11) NOT NULL auto_increment, 
     232  `name` VARCHAR(255) NOT NULL, 
     233  PRIMARY KEY  (`prefixid`) 
     234) TYPE=InnoDB; 
     235 
     236CREATE TABLE `underscored_prefixes` ( 
     237  `underscored_prefix_id` int(11) NOT NULL auto_increment, 
     238  `name` VARCHAR(255) NOT NULL, 
     239  PRIMARY KEY  (`underscored_prefix_id`) 
     240) TYPE=InnoDB; 
  • activerecord/test/fixtures/db_definitions/mysql.drop.sql

    old new  
    2929DROP TABLE keyboards; 
    3030DROP TABLE legacy_things; 
    3131DROP TABLE numeric_data; 
     32DROP TABLE prefixes; 
     33DROP TABLE underscored_prefixes; 
  • activerecord/lib/active_record/schema_dumper.rb

    old new  
    7272          if @connection.respond_to?(:pk_and_sequence_for) 
    7373            pk, pk_seq = @connection.pk_and_sequence_for(table) 
    7474          end 
    75           pk ||= 'id' 
     75          pk ||=   
     76            case ActiveRecord::Base.primary_key_prefix_type 
     77              when :table_name 
     78                Inflector.primary_key(table, false) 
     79              when :table_name_with_underscore 
     80                Inflector.primary_key(table) 
     81              else 
     82                "id" 
     83            end 
    7684 
    7785          tbl.print "  create_table #{table.inspect}" 
    7886          if columns.detect { |c| c.name == pk }