1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activerecord/test/cases/migration
Alex Ghiculescu 867d27dd64 Active Record + PostgreSQL: native support for timestamp with time zone
In https://github.com/rails/rails/issues/21126 it was suggested to make "timestamp with time zone" the default type for datetime columns in PostgreSQL. This is in line with PostgreSQL [best practices](https://wiki.postgresql.org/wiki/Don't_Do_This#Don.27t_use_timestamp_.28without_time_zone.29). This PR lays some groundwork for that.

This PR adds a configuration option, `ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.datetime_type`. The default is `:timestamp` which preserves current Rails behavior of using "timestamp without time zone" when you do `t.datetime` in a migration. If you change it to `:timestamptz`, you'll get "timestamp with time zone" columns instead.

If you change this setting in an existing app, you should immediately call `bin/rails db:migrate` to ensure your `schema.rb` file remains correct. If you do so, then existing columns will not be impacted, so for example if you have an app with a mixture of both types of columns, and you change the config, schema dumps will continue to output the correct types.

This PR also adds two new types that can be used in migrations: `t.timestamp` and `t.timestamptz`.

```ruby
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.datetime_type = :timestamp # default value is :timestamp

create_table("foo1") do |t|
  t.datetime :default_format # "timestamp without time zone"
  t.timestamp :without_time_zone # "timestamp without time zone"
  t.timestamptz :with_time_zone # "timestamp with time zone"
end

ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.datetime_type = :timestamptz

create_table("foo2") do |t|
  t.datetime :default_format # "timestamp with time zone" <-- note how this has changed!
  t.timestamp :without_time_zone # "timestamp without time zone"
  t.timestamptz :with_time_zone # "timestamp with time zone"
end

ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::NATIVE_DATABASE_TYPES[:my_custom_type] = { name: "custom_datetime_format_i_invented" }
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.datetime_type = :my_custom_type

create_table("foo3") do |t|
  t.datetime :default_format # "custom_datetime_format_i_invented"
  t.timestamp :without_time_zone # "timestamp without time zone"
  t.timestamptz :with_time_zone # "timestamp with time zone"
end
```

**Notes**

- This PR doesn't change the default `datetime` format. The default is still "timestamp without time zone". A future PR could do that, but there was enough code here just getting the config option right.
- See also https://github.com/rails/rails/pull/41395 which set some groundwork (and added some tests) for this.
- This reverts some of https://github.com/rails/rails/pull/15184. https://github.com/rails/rails/pull/15184 alluded to issues in XML serialization, but I couldn't find any related tests that this broke.
2021-05-06 17:41:55 -05:00
..
change_schema_test.rb Active Record + PostgreSQL: native support for timestamp with time zone 2021-05-06 17:41:55 -05:00
change_table_test.rb Remove all version checking for older Rubies in tests 2021-02-05 13:21:45 +09:00
check_constraint_test.rb Add support for check_constraints with the same name on different tables 2021-03-29 12:12:15 -07:00
column_attributes_test.rb
column_positioning_test.rb
columns_test.rb Fix typo mange -> manage [ci skip] 2021-05-01 16:18:02 +09:00
command_recorder_test.rb Add basic support for check constraints to database migrations 2020-06-02 22:11:27 -07:00
compatibility_test.rb Use major + minor AR versions in 'Directly inheriting' error message 2021-01-20 15:57:56 +00:00
create_join_table_test.rb
foreign_key_test.rb Raise database error when table doesn't exist 2020-11-01 12:31:39 +00:00
helper.rb
index_test.rb Fix rename_index removing old index with symbols 2020-10-21 14:09:34 +08:00
logger_test.rb
pending_migrations_test.rb Use FileUpdateChecker for Migration::CheckPending 2019-10-08 16:29:12 -07:00
references_foreign_key_test.rb Expand on specs of self join and add make doc changes to suggest users to also do foreign_key when doing self join 2020-06-05 01:09:50 +05:30
references_index_test.rb Optimise length of default index name for polymorphic references 2020-10-30 00:33:39 +00:00
references_statements_test.rb Add test that primary_key and reference columns should be identical type for add_reference 2020-11-13 20:06:42 +09:00
rename_table_test.rb Remove dead test code for unsupported adapters 2020-04-12 03:30:25 +09:00