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

Changeset 6842

Show
Ignore:
Timestamp:
05/25/07 21:21:41 (2 years ago)
Author:
bitsweat
Message:

Migrations: add_column supports custom column types. Closes #7742. First-patch cheers to jsgarvin\!

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activerecord/CHANGELOG

    r6833 r6842  
    11*SVN* 
     2 
     3* Migrations: add_column supports custom column types.  #7742 [jsgarvin, Theory] 
    24 
    35* Load database adapters on demand. Eliminates config.connection_adapters and RAILS_CONNECTION_ADAPTERS. Add your lib directory to the $LOAD_PATH and put your custom adapter in lib/active_record/connection_adapters/adaptername_adapter.rb. This way you can provide custom adapters as plugins or gems without modifying Rails. [Jeremy Kemper] 
  • trunk/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb

    r5956 r6842  
    254254 
    255255      def type_to_sql(type, limit = nil, precision = nil, scale = nil) #:nodoc: 
    256         native = native_database_types[type] 
    257         column_type_sql = native.is_a?(Hash) ? native[:name] : native 
    258         if type == :decimal # ignore limit, use precison and scale 
    259           precision ||= native[:precision] 
    260           scale ||= native[:scale] 
    261           if precision 
    262             if scale 
    263               column_type_sql << "(#{precision},#{scale})" 
     256        if native = native_database_types[type] 
     257          column_type_sql = native.is_a?(Hash) ? native[:name] : native 
     258          if type == :decimal # ignore limit, use precison and scale 
     259            precision ||= native[:precision] 
     260            scale ||= native[:scale] 
     261            if precision 
     262              if scale 
     263                column_type_sql << "(#{precision},#{scale})" 
     264              else 
     265                column_type_sql << "(#{precision})" 
     266              end 
    264267            else 
    265               column_type_sql << "(#{precision})" 
     268              raise ArgumentError, "Error adding decimal column: precision cannot be empty if scale if specified" if scale 
    266269            end 
     270            column_type_sql 
    267271          else 
    268             raise ArgumentError, "Error adding decimal column: precision cannot be empty if scale if specified" if scale 
     272            limit ||= native[:limit] 
     273            column_type_sql << "(#{limit})" if limit 
     274            column_type_sql 
    269275          end 
    270           column_type_sql 
    271276        else 
    272           limit ||= native[:limit] 
    273           column_type_sql << "(#{limit})" if limit 
    274           column_type_sql 
     277          column_type_sql = type 
    275278        end 
    276279      end 
  • trunk/activerecord/test/migration_test.rb

    r6756 r6842  
    313313      assert_equal TrueClass, bob.male?.class 
    314314      assert_kind_of BigDecimal, bob.wealth 
     315    end 
     316 
     317    if current_adapter?(:MysqlAdapter) 
     318      def test_unabstracted_database_dependent_types 
     319        Person.delete_all 
     320 
     321        ActiveRecord::Migration.add_column :people, :intelligence_quotient, :tinyint 
     322        Person.create :intelligence_quotient => 300 
     323        jonnyg = Person.find(:first)  
     324        assert_equal 127, jonnyg.intelligence_quotient 
     325        jonnyg.destroy 
     326      ensure 
     327        ActiveRecord::Migration.remove_column :people, :intelligece_quotient rescue nil 
     328      end 
    315329    end 
    316330