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

Ticket #6868: make_postgres_honor_scale_and_precision.diff

File make_postgres_honor_scale_and_precision.diff, 1.8 kB (added by manfred, 2 years ago)
  • lib/active_record/connection_adapters/postgresql_adapter.rb

    old new  
    318318        notnull = options[:null] == false 
    319319 
    320320        # Add the column. 
    321         execute("ALTER TABLE #{table_name} ADD COLUMN #{column_name} #{type_to_sql(type, options[:limit])}") 
     321        execute("ALTER TABLE #{table_name} ADD COLUMN #{column_name} #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}") 
    322322 
    323323        # Set optional default. If not null, update nulls to the new default. 
    324324        unless default.nil? 
  • test/migration_test.rb

    old new  
    199199    ensure 
    200200      Person.connection.drop_table :testings rescue nil 
    201201    end 
     202     
     203    def test_add_column_with_precision_and_scale 
     204        Person.connection.add_column Person.table_name, :cost, :decimal, :precision => 3, :scale => 2 
     205        # Unfortunately it's very difficult to test whether the database engine actually raises 
     206        # StatementInvalid because this differs a lot between database engines and versions. 
     207        Person.reset_column_information 
     208        column = Person.columns.detect { |c| c.name == 'cost' } 
     209        assert_equal 3, column.precision 
     210        assert_equal 2, column.scale 
     211      ensure 
     212        Person.connection.remove_column Person.table_name, :cost rescue nil 
     213    end 
    202214 
    203215    # We specifically do a manual INSERT here, and then test only the SELECT 
    204216    # functionality. This allows us to more easily catch INSERT being broken,