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

Ticket #6868: make_postgres_honor_scale_and_precision_with_documentation.diff

File make_postgres_honor_scale_and_precision_with_documentation.diff, 3.0 kB (added by manfred, 1 year ago)

New version of the patch, now with documentation.

  • test/migration_test.rb

    old new  
    208208      Person.connection.drop_table :testings rescue nil 
    209209    end 
    210210 
     211    def test_add_column_with_precision_and_scale 
     212        Person.connection.add_column Person.table_name, :cost, :decimal, :precision => 3, :scale => 2 
     213        # Unfortunately it's very difficult to test whether the database engine actually raises 
     214        # StatementInvalid because this differs a lot between database engines and versions. 
     215        Person.reset_column_information 
     216        column = Person.columns.detect { |c| c.name == 'cost' } 
     217        assert_equal 3, column.precision 
     218        assert_equal 2, column.scale 
     219      ensure 
     220        Person.connection.remove_column Person.table_name, :cost rescue nil 
     221    end 
     222 
    211223    # We specifically do a manual INSERT here, and then test only the SELECT 
    212224    # functionality. This allows us to more easily catch INSERT being broken, 
    213225    # but SELECT actually working fine. 
  • lib/active_record/connection_adapters/postgresql_adapter.rb

    old new  
    321321        execute "ALTER TABLE #{name} RENAME TO #{new_name}" 
    322322      end 
    323323 
     324      # Add a column to a table. 
     325      # 
     326      # Options: 
     327      # 
     328      # * <tt>:limit</tt> -- Length limit for the column 
     329      # * <tt>:precision</tt> -- Precision for the column, this only applies on certain columns 
     330      # * <tt>:scale</tt> -- Scale for the column, this only applies on certain columns 
     331      # * <tt>:default</tt> -- The default value for the column when an INSERT doesn't specify a value 
     332      # * <tt>:null</tt> -- Set to false if you don't want the database to accept NULL for this column 
     333      # 
     334      # See the PosgreSQL documentation on what precision and scale exactly mean for numeric columns. 
     335      # 
     336      # Example: 
     337      #   add_column 'users', 'first_name', 'string', :limit => 20 
     338      #   add_column 'option', 'votes', 'integer' 
     339      #   add_column 'emissions', 'carbon_offset', 'decimal', :precision => 16, :scale => 8 
     340      #   add_column 'car', 'name', 'string', :null => false 
    324341      def add_column(table_name, column_name, type, options = {}) 
    325342        default = options[:default] 
    326343        notnull = options[:null] == false 
    327344 
    328345        quoted_column_name = quote_column_name(column_name) 
    329346 
    330         # Add the column. 
    331         execute("ALTER TABLE #{table_name} ADD COLUMN #{quoted_column_name} #{type_to_sql(type, options[:limit])}") 
     347        # Add the column with limit, precision and scale 
     348        execute("ALTER TABLE #{table_name} ADD COLUMN #{column_name} #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}") 
    332349 
    333350        # Set optional default. If not null, update nulls to the new default. 
    334351        if options_include_default?(options)