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

Ticket #10698: 10698.diff

File 10698.diff, 15.4 kB (added by lotswholetime, 10 months ago)

Updated patch (quoted a missed table name, adds missing files)

  • activerecord/test/base_test.rb

    old new  
    1212require 'fixtures/keyboard' 
    1313require 'fixtures/post' 
    1414require 'fixtures/minimalistic' 
     15require 'fixtures/warehouse_thing' 
    1516require 'rexml/document' 
    1617 
    1718class Category < ActiveRecord::Base; end 
     
    7172end 
    7273 
    7374class BasicsTest < Test::Unit::TestCase 
    74   fixtures :topics, :companies, :developers, :projects, :computers, :accounts, :minimalistics 
     75  fixtures :topics, :companies, :developers, :projects, :computers, :accounts, :minimalistics, 'warehouse-things' 
    7576 
    7677  def test_table_exists 
    7778    assert !NonExistentTable.table_exists? 
     
    590591    assert_nil Topic.find(2).last_read 
    591592  end 
    592593 
     594  def test_update_all_with_non_standard_table_name 
     595    assert_equal 1, WarehouseThing.update_all(['value = ?', 0], ['id = ?', 1]) 
     596    assert_equal 0, WarehouseThing.find(1).value 
     597  end 
     598 
    593599  if current_adapter?(:MysqlAdapter) 
    594600    def test_update_all_with_order_and_limit 
    595601      assert_equal 1, Topic.update_all("content = 'bulk updated!'", nil, :limit => 1, :order => 'id DESC') 
  • activerecord/test/fixtures/warehouse-things.yml

    old new  
     1one: 
     2  id: 1 
     3  value: 1000 
  • activerecord/test/fixtures/db_definitions/schema.rb

    old new  
    351351    t.datetime :updated_at 
    352352    t.datetime :updated_on 
    353353  end 
     354   
     355  create_table 'warehouse-things', :force => true do |t| 
     356    t.integer :value 
     357  end 
    354358end 
  • activerecord/test/fixtures/warehouse_thing.rb

    old new  
     1class WarehouseThing < ActiveRecord::Base 
     2  set_table_name "warehouse-things" 
     3   
     4  validates_uniqueness_of :value 
     5end 
  • activerecord/test/validations_test.rb

    old new  
    33require 'fixtures/reply' 
    44require 'fixtures/person' 
    55require 'fixtures/developer' 
     6require 'fixtures/warehouse_thing' 
    67 
    78# The following methods in Topic are used in test_conditional_validation_* 
    89class Topic 
     
    5455end 
    5556 
    5657class ValidationsTest < Test::Unit::TestCase 
    57   fixtures :topics, :developers 
     58  fixtures :topics, :developers, 'warehouse-things' 
    5859 
    5960  def setup 
    6061    Topic.write_inheritable_attribute(:validate, nil) 
     
    435436    assert t2.save, "should save with nil" 
    436437  end 
    437438 
     439  def test_validate_uniqueness_with_non_standard_table_names 
     440    i1 = WarehouseThing.create(:value => 1000) 
     441    assert !i1.valid?, "i1 should not be valid" 
     442    assert i1.errors.on(:value), "Should not be empty"   
     443  end 
     444 
     445 
    438446  def test_validate_straight_inheritance_uniqueness 
    439447    w1 = IneptWizard.create(:name => "Rincewind", :city => "Ankh-Morpork") 
    440448    assert w1.valid?, "Saving w1" 
  • activerecord/lib/active_record/validations.rb

    old new  
    653653 
    654654        validates_each(attr_names,configuration) do |record, attr_name, value| 
    655655          if value.nil? || (configuration[:case_sensitive] || !columns_hash[attr_name.to_s].text?) 
    656             condition_sql = "#{record.class.table_name}.#{attr_name} #{attribute_condition(value)}" 
     656            condition_sql = "#{record.class.quoted_table_name}.#{attr_name} #{attribute_condition(value)}" 
    657657            condition_params = [value] 
    658658          else 
    659             condition_sql = "LOWER(#{record.class.table_name}.#{attr_name}) #{attribute_condition(value)}" 
     659            condition_sql = "LOWER(#{record.class.quoted_table_name}.#{attr_name}) #{attribute_condition(value)}" 
    660660            condition_params = [value.downcase] 
    661661          end 
    662662 
    663663          if scope = configuration[:scope] 
    664664            Array(scope).map do |scope_item| 
    665665              scope_value = record.send(scope_item) 
    666               condition_sql << " AND #{record.class.table_name}.#{scope_item} #{attribute_condition(scope_value)}" 
     666              condition_sql << " AND #{record.class.quoted_table_name}.#{scope_item} #{attribute_condition(scope_value)}" 
    667667              condition_params << scope_value 
    668668            end 
    669669          end 
    670670 
    671671          unless record.new_record? 
    672             condition_sql << " AND #{record.class.table_name}.#{record.class.primary_key} <> ?" 
     672            condition_sql << " AND #{record.class.quoted_table_name}.#{record.class.primary_key} <> ?" 
    673673            condition_params << record.send(:id) 
    674674          end 
    675675 
  • activerecord/lib/active_record/reflection.rb

    old new  
    129129        @table_name ||= klass.table_name 
    130130      end 
    131131 
     132      def quoted_table_name 
     133        @quoted_table_name ||= klass.quoted_table_name 
     134      end 
     135 
    132136      def primary_key_name 
    133137        @primary_key_name ||= options[:foreign_key] || derive_primary_key_name 
    134138      end 
  • activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb

    old new  
    100100            end 
    101101 
    102102            sql = 
    103               "INSERT INTO #{@reflection.options[:join_table]} (#{@owner.send(:quoted_column_names, attributes).join(', ')}) " + 
     103              "INSERT INTO #{@owner.connection.quote_table_name @reflection.options[:join_table]} (#{@owner.send(:quoted_column_names, attributes).join(', ')}) " + 
    104104              "VALUES (#{attributes.values.join(', ')})" 
    105105 
    106106            @owner.connection.execute(sql) 
     
    114114            records.each { |record| @owner.connection.execute(interpolate_sql(sql, record)) } 
    115115          else 
    116116            ids = quoted_record_ids(records) 
    117             sql = "DELETE FROM #{@reflection.options[:join_table]} WHERE #{@reflection.primary_key_name} = #{@owner.quoted_id} AND #{@reflection.association_foreign_key} IN (#{ids})" 
     117            sql = "DELETE FROM #{@owner.connection.quote_table_name @reflection.options[:join_table]} WHERE #{@reflection.primary_key_name} = #{@owner.quoted_id} AND #{@reflection.association_foreign_key} IN (#{ids})" 
    118118            @owner.connection.execute(sql) 
    119119          end 
    120120        end 
     
    125125          if @reflection.options[:finder_sql] 
    126126            @finder_sql = @reflection.options[:finder_sql] 
    127127          else 
    128             @finder_sql = "#{@reflection.options[:join_table]}.#{@reflection.primary_key_name} = #{@owner.quoted_id} " 
     128            @finder_sql = "#{@owner.connection.quote_table_name @reflection.options[:join_table]}.#{@reflection.primary_key_name} = #{@owner.quoted_id} " 
    129129            @finder_sql << " AND (#{conditions})" if conditions 
    130130          end 
    131131 
    132           @join_sql = "INNER JOIN #{@reflection.options[:join_table]} ON #{@reflection.klass.table_name}.#{@reflection.klass.primary_key} = #{@reflection.options[:join_table]}.#{@reflection.association_foreign_key}" 
     132          @join_sql = "INNER JOIN #{@owner.connection.quote_table_name @reflection.options[:join_table]} ON #{@reflection.quoted_table_name}.#{@reflection.klass.primary_key} = #{@owner.connection.quote_table_name @reflection.options[:join_table]}.#{@reflection.association_foreign_key}" 
    133133        end 
    134134 
    135135        def construct_scope 
  • activerecord/lib/active_record/associations/has_one_association.rb

    old new  
    6161          case 
    6262            when @reflection.options[:as] 
    6363              @finder_sql =  
    64                 "#{@reflection.klass.table_name}.#{@reflection.options[:as]}_id = #{@owner.quoted_id} AND " +  
    65                 "#{@reflection.klass.table_name}.#{@reflection.options[:as]}_type = #{@owner.class.quote_value(@owner.class.base_class.name.to_s)}"           
     64                "#{@reflection.quoted_table_name}.#{@reflection.options[:as]}_id = #{@owner.quoted_id} AND " +  
     65                "#{@reflection.quoted_table_name}.#{@reflection.options[:as]}_type = #{@owner.class.quote_value(@owner.class.base_class.name.to_s)}"           
    6666            else 
    67               @finder_sql = "#{@reflection.table_name}.#{@reflection.primary_key_name} = #{@owner.quoted_id}" 
     67              @finder_sql = "#{@reflection.quoted_table_name}.#{@reflection.primary_key_name} = #{@owner.quoted_id}" 
    6868          end 
    6969          @finder_sql << " AND (#{conditions})" if conditions 
    7070        end 
  • activerecord/lib/active_record/associations/has_many_association.rb

    old new  
    144144 
    145145            when @reflection.options[:as] 
    146146              @finder_sql =  
    147                 "#{@reflection.klass.table_name}.#{@reflection.options[:as]}_id = #{@owner.quoted_id} AND " +  
    148                 "#{@reflection.klass.table_name}.#{@reflection.options[:as]}_type = #{@owner.class.quote_value(@owner.class.base_class.name.to_s)}" 
     147                "#{@reflection.quoted_table_name}.#{@reflection.options[:as]}_id = #{@owner.quoted_id} AND " +  
     148                "#{@reflection.quoted_table_name}.#{@reflection.options[:as]}_type = #{@owner.class.quote_value(@owner.class.base_class.name.to_s)}" 
    149149              @finder_sql << " AND (#{conditions})" if conditions 
    150150             
    151151            else 
    152               @finder_sql = "#{@reflection.klass.table_name}.#{@reflection.primary_key_name} = #{@owner.quoted_id}" 
     152              @finder_sql = "#{@reflection.quoted_table_name}.#{@reflection.primary_key_name} = #{@owner.quoted_id}" 
    153153              @finder_sql << " AND (#{conditions})" if conditions 
    154154          end 
    155155 
  • activerecord/lib/active_record/associations/has_many_through_association.rb

    old new  
    121121        column_name, options = @reflection.klass.send(:construct_count_options_from_args, *args) 
    122122        if @reflection.options[:uniq] 
    123123          # This is needed because 'SELECT count(DISTINCT *)..' is not valid sql statement. 
    124           column_name = "#{@reflection.klass.table_name}.#{@reflection.klass.primary_key}" if column_name == :all 
     124          column_name = "#{@reflection.quoted_table_name}.#{@reflection.klass.primary_key}" if column_name == :all 
    125125          options.merge!(:distinct => true)  
    126126        end 
    127127        @reflection.klass.send(:with_scope, construct_scope) { @reflection.klass.count(column_name, options) }  
     
    185185 
    186186        # Build SQL conditions from attributes, qualified by table name. 
    187187        def construct_conditions 
    188           table_name = @reflection.through_reflection.table_name 
     188          table_name = @reflection.through_reflection.quoted_table_name 
    189189          conditions = construct_quoted_owner_attributes(@reflection.through_reflection).map do |attr, value| 
    190190            "#{table_name}.#{attr} = #{value}" 
    191191          end 
     
    194194        end 
    195195 
    196196        def construct_from 
    197           @reflection.table_name 
     197          @reflection.quoted_table_name 
    198198        end 
    199199 
    200200        def construct_select(custom_select = nil) 
    201           selected = custom_select || @reflection.options[:select] || "#{@reflection.table_name}.*" 
     201          selected = custom_select || @reflection.options[:select] || "#{@reflection.quoted_table_name}.*" 
    202202        end 
    203203 
    204204        def construct_joins(custom_joins = nil) 
     
    208208            source_primary_key     = @reflection.source_reflection.primary_key_name 
    209209            if @reflection.options[:source_type] 
    210210              polymorphic_join = "AND %s.%s = %s" % [ 
    211                 @reflection.through_reflection.table_name, "#{@reflection.source_reflection.options[:foreign_type]}", 
     211                @reflection.through_reflection.quoted_table_name, "#{@reflection.source_reflection.options[:foreign_type]}", 
    212212                @owner.class.quote_value(@reflection.options[:source_type]) 
    213213              ] 
    214214            end 
     
    217217            source_primary_key     = @reflection.klass.primary_key 
    218218            if @reflection.source_reflection.options[:as] 
    219219              polymorphic_join = "AND %s.%s = %s" % [ 
    220                 @reflection.table_name, "#{@reflection.source_reflection.options[:as]}_type", 
     220                @reflection.quoted_table_name, "#{@reflection.source_reflection.options[:as]}_type", 
    221221                @owner.class.quote_value(@reflection.through_reflection.klass.name) 
    222222              ] 
    223223            end 
     
    246246            when @reflection.options[:finder_sql] 
    247247              @finder_sql = interpolate_sql(@reflection.options[:finder_sql]) 
    248248 
    249               @finder_sql = "#{@reflection.klass.table_name}.#{@reflection.primary_key_name} = #{@owner.quoted_id}" 
     249              @finder_sql = "#{@reflection.quoted_table_name}.#{@reflection.primary_key_name} = #{@owner.quoted_id}" 
    250250              @finder_sql << " AND (#{conditions})" if conditions 
    251251          end 
    252252 
     
    286286        end 
    287287 
    288288        def build_sti_condition 
    289           "#{@reflection.through_reflection.table_name}.#{@reflection.through_reflection.klass.inheritance_column} = #{@reflection.klass.quote_value(@reflection.through_reflection.klass.name.demodulize)}" 
     289          "#{@reflection.through_reflection.quoted_table_name}.#{@reflection.through_reflection.klass.inheritance_column} = #{@reflection.klass.quote_value(@reflection.through_reflection.klass.name.demodulize)}" 
    290290        end 
    291291 
    292292        alias_method :sql_conditions, :conditions 
  • activerecord/lib/active_record/base.rb

    old new  
    677677      #   Billing.update_all( "author = 'David'", "title LIKE '%Rails%'", 
    678678      #                         :order => 'created_at', :limit => 5 ) 
    679679      def update_all(updates, conditions = nil, options = {}) 
    680         sql  = "UPDATE #{table_name} SET #{sanitize_sql_for_assignment(updates)} " 
     680        sql  = "UPDATE #{quoted_table_name} SET #{sanitize_sql_for_assignment(updates)} " 
    681681        scope = scope(:find) 
    682682        add_conditions!(sql, conditions, scope) 
    683683        add_order!(sql, options[:order], nil)