Skip to content

Instantly share code, notes, and snippets.

@AquaGeek
Created May 14, 2011 02:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AquaGeek/971639 to your computer and use it in GitHub Desktop.
Save AquaGeek/971639 to your computer and use it in GitHub Desktop.
Rails Lighthouse ticket #2872
diff --git a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb
index 61ee470..89fff48 100644
--- a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb
@@ -360,6 +360,7 @@ module ActiveRecord
@definition.column(column_name, column.type,
:limit => column.limit, :default => column.default,
+ :precision => column.precision, :scale => column.scale,
:null => column.null)
end
@definition.primary_key(primary_key(from)) if primary_key(from)
diff -ru activerecord-2.3.2-orig/lib/active_record/connection_adapters/sqlite_adapter.rb activerecord-2.3.2/lib/active_record/connection_adapters/sqlite_adapter.rb
--- activerecord-2.3.2-orig/lib/active_record/connection_adapters/sqlite_adapter.rb 2009-07-06 15:43:43.000000000 +0600
+++ activerecord-2.3.2/lib/active_record/connection_adapters/sqlite_adapter.rb 2009-07-06 15:20:07.000000000 +0600
@@ -285,6 +285,8 @@
self.limit = options[:limit] if options.include?(:limit)
self.default = options[:default] if include_default
self.null = options[:null] if options.include?(:null)
+ self.precision = options[:precision] if options.include?(:precision)
+ self.scale = options[:scale] if options.include?(:scale)
end
end
end
From a92bbdf12656346f87fb33ef9a09426184f577db Mon Sep 17 00:00:00 2001
From: taryn <teast@globalpersonals.co.uk>
Date: Wed, 22 Jul 2009 12:14:26 +0100
Subject: [PATCH] A test that successfully reproduces the precision/scale error in SQLite - along with the patch from Andrey Zaikin that fixes it
---
.../connection_adapters/sqlite_adapter.rb | 2 ++
activerecord/test/cases/migration_test.rb | 20 ++++++++++++++++++++
2 files changed, 22 insertions(+), 0 deletions(-)
diff --git a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb
index 5e5e307..61ee470 100644
--- a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb
@@ -295,6 +295,8 @@ module ActiveRecord
self.type = type
self.limit = options[:limit] if options.include?(:limit)
self.default = options[:default] if include_default
+ self.precision = options[:precision] if options.include?(:precision)
+ self.scale = options[:scale] if options.include?(:scale)
self.null = options[:null] if options.include?(:null)
end
end
diff --git a/activerecord/test/cases/migration_test.rb b/activerecord/test/cases/migration_test.rb
index 215b5a4..61173cb 100644
--- a/activerecord/test/cases/migration_test.rb
+++ b/activerecord/test/cases/migration_test.rb
@@ -770,6 +770,26 @@ if ActiveRecord::Base.connection.supports_migrations?
Person.connection.remove_column("people", "administrator") rescue nil
end
+ if current_adapter?(:SQLiteAdapter)
+ def test_change_column_adding_precision_and_scale
+ # first add a column without any precision/scale
+ Person.connection.add_column "people", "my_decimal", :decimal
+ Person.reset_column_information
+
+ new_columns = Person.connection.columns(Person.table_name, "#{name} Columns")
+ assert(new_columns.find { |c| c.name == 'my_decimal' && c.type == :decimal && c.scale.nil? && c.precision.nil? }, "should have found a decimal column with nil precision and scale")
+
+ # now try to change the column by adding a precision/scale
+ assert_nothing_raised { Person.connection.change_column "people", "my_decimal", :decimal, :precision => 5, :scale => 2 }
+ Person.reset_column_information
+
+ new_columns = Person.connection.columns(Person.table_name, "#{name} Columns")
+ assert(new_columns.find { |c| c.name == 'my_decimal' && c.type == :decimal && c.scale == 2 && c.precision == 5 }, "should now have found a decimal column with the given precision and scale")
+ ensure
+ Person.connection.remove_column("people", "my_decimal") rescue nil
+ end
+ end
+
def test_change_column_default
Person.connection.change_column_default "people", "first_name", "Tester"
Person.reset_column_information
--
1.6.0.4
diff --git a/activerecord/test/cases/migration_test.rb b/activerecord/test/cases/migration_test.rb
index 2ec3d40..70446c4 100644
--- a/activerecord/test/cases/migration_test.rb
+++ b/activerecord/test/cases/migration_test.rb
@@ -788,6 +788,20 @@ if ActiveRecord::Base.connection.supports_migrations?
assert_nil Person.new.first_name
end
+ def test_change_column_does_not_lose_decimal_precision_and_scale
+ Person.connection.create_table :testings do |t|
+ t.column :price_on_head, :decimal, :scale => 8, :precision => 2
+ end
+ person_klass = Class.new(Person)
+ person_klass.set_table_name 'testings'
+ person_klass.connection.change_column "testings", "price_on_head", :decimal, :precision => 12, :scale => 5
+ person_klass.reset_column_information
+ assert_equal 12, person_klass.columns_hash["price_on_head"].precision
+ assert_equal 5, person_klass.columns_hash["price_on_head"].scale
+ ensure
+ Person.connection.drop_table :testings rescue nil
+ end
+
def test_add_table
assert !Reminder.table_exists?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment