From edf38308da1ffd133339568a4039e287648de47b Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Mon, 28 Oct 2024 11:34:34 -0500 Subject: [PATCH] Add Set DateTime To Include Time Zone In Migrations as a Rails TIL --- README.md | 3 +- ...time-to-include-time-zone-in-migrations.md | 45 +++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 rails/set-datetime-to-include-time-zone-in-migrations.md diff --git a/README.md b/README.md index 3111aa5..6e26df5 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ pairing with smart people at Hashrocket. For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186). -_1486 TILs and counting..._ +_1487 TILs and counting..._ --- @@ -1003,6 +1003,7 @@ _1486 TILs and counting..._ - [Select Value For SQL Counts](rails/select-value-for-sql-counts.md) - [Serialize With fast_jsonapi In A Rails App](rails/serialize-with-fast-jsonapi-in-a-rails-app.md) - [Set A Timestamp Field To The Current Time](rails/set-a-timestamp-field-to-the-current-time.md) +- [Set DateTime To Include Time Zone In Migrations](rails/set-datetime-to-include-time-zone-in-migrations.md) - [Set default_url_options For Entire Application](rails/set-default-url-options-for-entire-application.md) - [Set Schema Search Path](rails/set-schema-search-path.md) - [Set Statement Timeout For All Postgres Connections](rails/set-statement-timeout-for-all-postgres-connections.md) diff --git a/rails/set-datetime-to-include-time-zone-in-migrations.md b/rails/set-datetime-to-include-time-zone-in-migrations.md new file mode 100644 index 0000000..bfd0bce --- /dev/null +++ b/rails/set-datetime-to-include-time-zone-in-migrations.md @@ -0,0 +1,45 @@ +# Set Datetime To Include Time Zone In Migrations + +When using Rails and PostgreSQL, your migrations will contain DSL syntax like +`t.datetime` and `t.timestamps` which will produce columns using the +`timestamp` (`without time zone`) Postgres data type. + +While reading [A Simple Explanation of Postgres' Timestamp with Time +Zone](https://naildrivin5.com/blog/2024/10/10/a-simple-explanation-of-postgres-timestamp-with-time-zone.html), +I learned that there is a way to configure your app to instead use +`timestamptz` by default. This data type is widely recommended as a good +default, so it is nice that we can configure Rails to use it. + +First, add these lines to a new initializer (`config/initializers/postgres.rb`) +file. + +```ruby +require "active_record/connection_adapters/postgresql_adapter" +ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.datetime_type = :timestamptz +``` + +Alternatively, you can configure this via `config/application.rb` per the +[Configuring ActiveRecord +docs](https://guides.rubyonrails.org/configuring.html#activerecord-connectionadapters-postgresqladapter-datetime-type). + +Then, if you have a new migration like the following: + +```ruby +class AddEventsTable < ActiveRecord::Migration[7.2] + def change + create_table :events do |t| + t.string :title + t.text :description + t.datetime :start_time + t.datetime :end_time + t.timestamps + end + end +end +``` + +you can expect to have four `timestamptz` columns, namely `start_time`, +`end_time`, `created_at`, and `updated_at`. + +Here is the [Rails PR](https://github.com/rails/rails/pull/41084) that adds +this config option.