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) |
|---|
-
test/migration_test.rb
old new 208 208 Person.connection.drop_table :testings rescue nil 209 209 end 210 210 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 211 223 # We specifically do a manual INSERT here, and then test only the SELECT 212 224 # functionality. This allows us to more easily catch INSERT being broken, 213 225 # but SELECT actually working fine. -
lib/active_record/connection_adapters/postgresql_adapter.rb
old new 321 321 execute "ALTER TABLE #{name} RENAME TO #{new_name}" 322 322 end 323 323 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 324 341 def add_column(table_name, column_name, type, options = {}) 325 342 default = options[:default] 326 343 notnull = options[:null] == false 327 344 328 345 quoted_column_name = quote_column_name(column_name) 329 346 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])}") 332 349 333 350 # Set optional default. If not null, update nulls to the new default. 334 351 if options_include_default?(options)