Skip to content
This repository
Browse code

preserve decimal column attributes after migration

  • Loading branch information...
commit f092be821db4a2e8f142e8f0b9d08e497ccf2eb2 1 parent 79d01a8
Greg Reinacker greinacker authored
3  activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb
@@ -413,6 +413,8 @@ def change_column(table_name, column_name, type, options = {}) #:nodoc:
413 413 self.limit = options[:limit] if options.include?(:limit)
414 414 self.default = options[:default] if include_default
415 415 self.null = options[:null] if options.include?(:null)
  416 + self.precision = options[:precision] if options.include?(:precision)
  417 + self.scale = options[:scale] if options.include?(:scale)
416 418 end
417 419 end
418 420 end
@@ -467,6 +469,7 @@ def copy_table(from, to, options = {}) #:nodoc:
467 469
468 470 @definition.column(column_name, column.type,
469 471 :limit => column.limit, :default => column.default,
  472 + :precision => column.precision, :scale => column.scale,
470 473 :null => column.null)
471 474 end
472 475 @definition.primary_key(primary_key(from)) if primary_key(from)
36 activerecord/test/cases/migration_test.rb
@@ -518,6 +518,42 @@ def test_add_column_with_precision_and_scale
518 518 assert_equal 7, wealth_column.scale
519 519 end
520 520
  521 + # Test SQLite adapter specifically for decimal types with precision and scale
  522 + # attributes, since these need to be maintained in schema but aren't actually
  523 + # used in SQLite itself
  524 + if current_adapter?(:SQLite3Adapter)
  525 + def test_change_column_with_new_precision_and_scale
  526 + Person.delete_all
  527 + Person.connection.add_column 'people', 'wealth', :decimal, :precision => 9, :scale => 7
  528 + Person.reset_column_information
  529 +
  530 + Person.connection.change_column 'people', 'wealth', :decimal, :precision => 12, :scale => 8
  531 + Person.reset_column_information
  532 +
  533 + wealth_column = Person.columns_hash['wealth']
  534 + assert_equal 12, wealth_column.precision
  535 + assert_equal 8, wealth_column.scale
  536 + end
  537 +
  538 + def test_change_column_preserve_other_column_precision_and_scale
  539 + Person.delete_all
  540 + Person.connection.add_column 'people', 'last_name', :string
  541 + Person.connection.add_column 'people', 'wealth', :decimal, :precision => 9, :scale => 7
  542 + Person.reset_column_information
  543 +
  544 + wealth_column = Person.columns_hash['wealth']
  545 + assert_equal 9, wealth_column.precision
  546 + assert_equal 7, wealth_column.scale
  547 +
  548 + Person.connection.change_column 'people', 'last_name', :string, :null => false
  549 + Person.reset_column_information
  550 +
  551 + wealth_column = Person.columns_hash['wealth']
  552 + assert_equal 9, wealth_column.precision
  553 + assert_equal 7, wealth_column.scale
  554 + end
  555 + end
  556 +
521 557 def test_native_types
522 558 Person.delete_all
523 559 Person.connection.add_column "people", "last_name", :string

0 comments on commit f092be8

Please sign in to comment.
Something went wrong with that request. Please try again.