The datetime precision with zero should be dumped

`precision: 0` was not dumped by f1a0fa9e19b7e4ccaea191fc6cf0613880222ee7.
However, `precision: 0` is valid value for PostgreSQL timestamps.
This commit is contained in:
Ryuta Kamizono 2015-02-11 09:42:37 +09:00
parent 162581a448
commit 18e0ffe90f
3 changed files with 46 additions and 23 deletions

@ -29,7 +29,7 @@ def prepare_column_options(column)
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?

@ -73,6 +73,12 @@ def column_spec_for_primary_key(column)
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

@ -1,4 +1,5 @@
require 'cases/helper'
require 'support/schema_dumping_helper'
require 'models/developer'
require 'models/topic'
@ -46,8 +47,6 @@ def test_timestamp_with_zone_values_without_rails_time_zone_support
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 @@ def test_save_infinity_and_beyond
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 @@ def test_postgres_agrees_with_activerecord_about_precision
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 @@ def test_formatting_timestamp_according_to_precision
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