From 3c801e7b26b4a1fffa94d6e44735ddc8fc4937c2 Mon Sep 17 00:00:00 2001 From: yota Date: Fri, 14 Jan 2022 16:00:28 +0900 Subject: [PATCH 1/2] Fix schema dumper behavior about datetime precision value By #42297, the default value of datetime precision in rails is now 6. With this change, if the value of datetime precision is 0, it should be dumped into schema, but it is not. So I modified it to dump to schema when the value of datetime precision is 0, and not to dump when the value is 6, which is the default value of rails. --- .../connection_adapters/mysql/schema_dumper.rb | 4 +++- activerecord/test/cases/date_time_precision_test.rb | 8 +++++--- activerecord/test/cases/defaults_test.rb | 2 +- activerecord/test/cases/primary_keys_test.rb | 2 +- activerecord/test/cases/schema_dumper_test.rb | 2 +- 5 files changed, 11 insertions(+), 7 deletions(-) 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 From 3432d6b03e8d5a7aae873520013489e853e9a631 Mon Sep 17 00:00:00 2001 From: yota Date: Fri, 14 Jan 2022 18:03:59 +0900 Subject: [PATCH 2/2] Dump datetime precision always in MySQL --- .../connection_adapters/mysql/schema_dumper.rb | 4 +--- activerecord/test/cases/date_time_precision_test.rb | 8 +++----- activerecord/test/cases/primary_keys_test.rb | 2 +- activerecord/test/cases/schema_dumper_test.rb | 2 +- 4 files changed, 6 insertions(+), 10 deletions(-) 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 0871aabf8a4e5..6713251abd150 100644 --- a/activerecord/lib/active_record/connection_adapters/mysql/schema_dumper.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql/schema_dumper.rb @@ -53,9 +53,7 @@ def schema_limit(column) end def schema_precision(column) - return if /\Adatetime\b/.match(column.sql_type) && column.precision == 6 - return if /\Atime(?:stamp)?\b/.match?(column.sql_type) && column.precision == 0 - super + super unless /\Atime(?:stamp)?\b/.match?(column.sql_type) && column.precision == 0 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 f86945117a13e..67304b392324d 100644 --- a/activerecord/test/cases/date_time_precision_test.rb +++ b/activerecord/test/cases/date_time_precision_test.rb @@ -193,15 +193,13 @@ def test_writing_a_blank_attribute_timestamptz end end - def test_schema_dump_includes_non_default_datetime_precision + def test_schema_dump_includes_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+"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 + 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 end if current_adapter?(:PostgreSQLAdapter, :SQLServerAdapter) diff --git a/activerecord/test/cases/primary_keys_test.rb b/activerecord/test/cases/primary_keys_test.rb index 67dc72f02e9b8..1707126f125cd 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.* }/, schema + assert_match %r/create_table "scheduled_logs", id: { type: :timestamp, precision: 6.* }/, schema end end end diff --git a/activerecord/test/cases/schema_dumper_test.rb b/activerecord/test/cases/schema_dumper_test.rb index 9ee88adf64bce..19d339ea15150 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+default: "2014-06-05 07:17:04"}, output + assert_match %r{t\.datetime\s+"datetime_with_default",\s+precision: 6,\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