diff --git a/activerecord/lib/active_record/connection_adapters/mysql/schema_dumper.rb b/activerecord/lib/active_record/connection_adapters/mysql/schema_dumper.rb index 0eff3131b6e76..0871aabf8a4e5 100644 --- a/activerecord/lib/active_record/connection_adapters/mysql/schema_dumper.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql/schema_dumper.rb @@ -53,7 +53,9 @@ def schema_limit(column) end def schema_precision(column) - super unless /\A(?:date)?time(?:stamp)?\b/.match?(column.sql_type) && column.precision == 0 + return if /\Adatetime\b/.match(column.sql_type) && column.precision == 6 + return if /\Atime(?:stamp)?\b/.match?(column.sql_type) && column.precision == 0 + super end def schema_collation(column) diff --git a/activerecord/test/cases/date_time_precision_test.rb b/activerecord/test/cases/date_time_precision_test.rb index 67304b392324d..f86945117a13e 100644 --- a/activerecord/test/cases/date_time_precision_test.rb +++ b/activerecord/test/cases/date_time_precision_test.rb @@ -193,13 +193,15 @@ def test_writing_a_blank_attribute_timestamptz end end - def test_schema_dump_includes_datetime_precision + def test_schema_dump_includes_non_default_datetime_precision @connection.create_table(:foos, force: true) do |t| + t.datetime :datetime_zero, precision: 0 t.timestamps precision: 6 end output = dump_table_schema("foos") - assert_match %r{t\.datetime\s+"created_at",\s+precision: 6,\s+null: false$}, output - assert_match %r{t\.datetime\s+"updated_at",\s+precision: 6,\s+null: false$}, output + assert_match %r{t\.datetime\s+"datetime_zero",\s+precision: 0$}, output + assert_match %r{t\.datetime\s+"created_at",\s+null: false$}, output + assert_match %r{t\.datetime\s+"updated_at",\s+null: false$}, output end if current_adapter?(:PostgreSQLAdapter, :SQLServerAdapter) diff --git a/activerecord/test/cases/defaults_test.rb b/activerecord/test/cases/defaults_test.rb index b140b2f83eb63..0b010ff8716cc 100644 --- a/activerecord/test/cases/defaults_test.rb +++ b/activerecord/test/cases/defaults_test.rb @@ -143,7 +143,7 @@ class MysqlDefaultExpressionTest < ActiveRecord::TestCase if supports_datetime_with_precision? test "schema dump datetime includes default expression" do output = dump_table_schema("datetime_defaults") - assert_match %r/t\.datetime\s+"modified_datetime",\s+default: -> { "CURRENT_TIMESTAMP(?:\(\))?" }/i, output + assert_match %r/t\.datetime\s+"modified_datetime",\s+precision: 0,\s+default: -> { "CURRENT_TIMESTAMP(?:\(\))?" }/i, output end test "schema dump datetime includes precise default expression" do diff --git a/activerecord/test/cases/primary_keys_test.rb b/activerecord/test/cases/primary_keys_test.rb index 1707126f125cd..67dc72f02e9b8 100644 --- a/activerecord/test/cases/primary_keys_test.rb +++ b/activerecord/test/cases/primary_keys_test.rb @@ -321,7 +321,7 @@ def test_any_type_primary_key test "schema typed primary key column" do @connection.create_table(:scheduled_logs, id: :timestamp, precision: 6, force: true) schema = dump_table_schema("scheduled_logs") - assert_match %r/create_table "scheduled_logs", id: { type: :timestamp, precision: 6.* }/, schema + assert_match %r/create_table "scheduled_logs", id: { type: :timestamp.* }/, schema end end end diff --git a/activerecord/test/cases/schema_dumper_test.rb b/activerecord/test/cases/schema_dumper_test.rb index 19d339ea15150..9ee88adf64bce 100644 --- a/activerecord/test/cases/schema_dumper_test.rb +++ b/activerecord/test/cases/schema_dumper_test.rb @@ -840,7 +840,7 @@ def test_schema_dump_defaults_with_universally_supported_types assert_match %r{t\.date\s+"date_with_default",\s+default: "2014-06-05"}, output if supports_datetime_with_precision? - assert_match %r{t\.datetime\s+"datetime_with_default",\s+precision: 6,\s+default: "2014-06-05 07:17:04"}, output + assert_match %r{t\.datetime\s+"datetime_with_default",\s+default: "2014-06-05 07:17:04"}, output else assert_match %r{t\.datetime\s+"datetime_with_default",\s+default: "2014-06-05 07:17:04"}, output end