aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2015-02-11 09:42:37 +0900
committerRyuta Kamizono <kamipo@gmail.com>2015-02-11 09:53:34 +0900
commit18e0ffe90f22dee975d40de40eb24d49765b14e8 (patch)
treef4de4b1fb578743ebca315c3d0dae02352196b73
parent162581a448ac6f3e52901a90000336f394226381 (diff)
downloadrails-18e0ffe90f22dee975d40de40eb24d49765b14e8.tar.gz
rails-18e0ffe90f22dee975d40de40eb24d49765b14e8.tar.bz2
rails-18e0ffe90f22dee975d40de40eb24d49765b14e8.zip
The datetime precision with zero should be dumped
`precision: 0` was not dumped by f1a0fa9e19b7e4ccaea191fc6cf0613880222ee7. However, `precision: 0` is valid value for PostgreSQL timestamps.
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract/schema_dumper.rb2
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb6
-rw-r--r--activerecord/test/cases/adapters/postgresql/timestamp_test.rb61
3 files changed, 46 insertions, 23 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_dumper.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_dumper.rb
index 4f6a1ec67b..932aaf7aa7 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract/schema_dumper.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_dumper.rb
@@ -29,7 +29,7 @@ module ActiveRecord
limit = column.limit || native_database_types[column.type][:limit]
spec[:limit] = limit.inspect if limit
- spec[:precision] = column.precision.inspect if column.precision && column.precision != 0
+ spec[:precision] = column.precision.inspect if column.precision
spec[:scale] = column.scale.inspect if column.scale
default = schema_default(column) if column.has_default?
diff --git a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
index 054c59ef5c..17d38da129 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
@@ -73,6 +73,12 @@ module ActiveRecord
spec
end
+ def prepare_column_options(column)
+ spec = super
+ spec.delete(:precision) if column.type == :datetime && column.precision == 0
+ spec
+ end
+
class Column < ConnectionAdapters::Column # :nodoc:
delegate :strict, :collation, :extra, to: :sql_type_metadata, allow_nil: true
diff --git a/activerecord/test/cases/adapters/postgresql/timestamp_test.rb b/activerecord/test/cases/adapters/postgresql/timestamp_test.rb
index 9e631fb4ca..3091ee136f 100644
--- a/activerecord/test/cases/adapters/postgresql/timestamp_test.rb
+++ b/activerecord/test/cases/adapters/postgresql/timestamp_test.rb
@@ -1,4 +1,5 @@
require 'cases/helper'
+require 'support/schema_dumping_helper'
require 'models/developer'
require 'models/topic'
@@ -46,8 +47,6 @@ end
class TimestampTest < ActiveRecord::TestCase
fixtures :topics
- class Foo < ActiveRecord::Base; end
-
def test_group_by_date
keys = Topic.group("date_trunc('month', created_at)").count.keys
assert_operator keys.length, :>, 0
@@ -72,6 +71,35 @@ class TimestampTest < ActiveRecord::TestCase
assert_equal(-1.0 / 0.0, d.updated_at)
end
+ def test_bc_timestamp
+ date = Date.new(0) - 1.week
+ Developer.create!(:name => "aaron", :updated_at => date)
+ assert_equal date, Developer.find_by_name("aaron").updated_at
+ end
+
+ def test_bc_timestamp_leap_year
+ date = Time.utc(-4, 2, 29)
+ Developer.create!(:name => "taihou", :updated_at => date)
+ assert_equal date, Developer.find_by_name("taihou").updated_at
+ end
+
+ def test_bc_timestamp_year_zero
+ date = Time.utc(0, 4, 7)
+ Developer.create!(:name => "yahagi", :updated_at => date)
+ assert_equal date, Developer.find_by_name("yahagi").updated_at
+ end
+end
+
+class TimestampPrecisionTest < ActiveRecord::TestCase
+ include SchemaDumpingHelper
+ self.use_transactional_fixtures = false
+
+ class Foo < ActiveRecord::Base; end
+
+ teardown do
+ ActiveRecord::Base.connection.drop_table(:foos, if_exists: true)
+ end
+
def test_default_datetime_precision
ActiveRecord::Base.connection.create_table(:foos)
ActiveRecord::Base.connection.add_column :foos, :created_at, :datetime
@@ -119,24 +147,6 @@ class TimestampTest < ActiveRecord::TestCase
assert_equal '4', pg_datetime_precision('foos', 'updated_at')
end
- def test_bc_timestamp
- date = Date.new(0) - 1.week
- Developer.create!(:name => "aaron", :updated_at => date)
- assert_equal date, Developer.find_by_name("aaron").updated_at
- end
-
- def test_bc_timestamp_leap_year
- date = Time.utc(-4, 2, 29)
- Developer.create!(:name => "taihou", :updated_at => date)
- assert_equal date, Developer.find_by_name("taihou").updated_at
- end
-
- def test_bc_timestamp_year_zero
- date = Time.utc(0, 4, 7)
- Developer.create!(:name => "yahagi", :updated_at => date)
- assert_equal date, Developer.find_by_name("yahagi").updated_at
- end
-
def test_formatting_timestamp_according_to_precision
ActiveRecord::Base.connection.create_table(:foos, force: true) do |t|
t.datetime :created_at, precision: 0
@@ -150,8 +160,15 @@ class TimestampTest < ActiveRecord::TestCase
assert_equal date.to_s, foo.updated_at.to_s
assert_equal 000000, foo.created_at.usec
assert_equal 999900, foo.updated_at.usec
- ensure
- ActiveRecord::Base.connection.drop_table(:foos, if_exists: true)
+ end
+
+ def test_datetime_precision_with_zero_should_be_dumped
+ ActiveRecord::Base.connection.create_table(:foos) do |t|
+ t.timestamps precision: 0
+ end
+ output = dump_table_schema("foos")
+ assert_match %r{t\.datetime\s+"created_at",\s+precision: 0,\s+null: false$}, output
+ assert_match %r{t\.datetime\s+"updated_at",\s+precision: 0,\s+null: false$}, output
end
private