From 3be7aa3d7aabc4cd287e17612539651c6512dc66 Mon Sep 17 00:00:00 2001 From: Greg Date: Fri, 21 Feb 2025 05:02:56 -0500 Subject: [PATCH 1/5] start types cheatsheet --- guides/cheatsheets/types.cheatmd | 39 ++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 guides/cheatsheets/types.cheatmd diff --git a/guides/cheatsheets/types.cheatmd b/guides/cheatsheets/types.cheatmd new file mode 100644 index 0000000000..cb3374a8ce --- /dev/null +++ b/guides/cheatsheets/types.cheatmd @@ -0,0 +1,39 @@ +# Types + +## Adapter-Specific Configuration + +### Using Duration with Postgrex + +#### Define migration with Postgres interval type + +```elixir +create table("movies") do + add :running_time, :interval +end +``` + +#### Define schema with Ecto duration type + +```elixir +defmodule Movie do + use Ecto.Schema + + schema "movies" do + field :running_time, :duration + end +end +``` + +#### Define Postgrex type module in its own file + +```elixir +# Inside lib/my_app/postgrex_types.ex + +Postgrex.Types.define(MyApp.PostgrexTypes, [], interval_decode_type: Duration) +``` + +#### Make Ecto aware of Postgrex type module in configuration + +```elixir +config :my_app, MyApp.Repo, types: MyApp.PostgresTypes +``` \ No newline at end of file From 3604a9188d340eba5bb3e7bd77c48d62e64ba395 Mon Sep 17 00:00:00 2001 From: Greg Date: Fri, 21 Feb 2025 05:05:21 -0500 Subject: [PATCH 2/5] cleanup --- guides/cheatsheets/types.cheatmd | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/guides/cheatsheets/types.cheatmd b/guides/cheatsheets/types.cheatmd index cb3374a8ce..cdf9f1c079 100644 --- a/guides/cheatsheets/types.cheatmd +++ b/guides/cheatsheets/types.cheatmd @@ -24,7 +24,7 @@ defmodule Movie do end ``` -#### Define Postgrex type module in its own file +#### Define Postgrex type module and specify intervals should be decoded to Duration ```elixir # Inside lib/my_app/postgrex_types.ex @@ -36,4 +36,4 @@ Postgrex.Types.define(MyApp.PostgrexTypes, [], interval_decode_type: Duration) ```elixir config :my_app, MyApp.Repo, types: MyApp.PostgresTypes -``` \ No newline at end of file +``` From 6b9dc2df49ec4e863f82a589d6c9f45259cb69a5 Mon Sep 17 00:00:00 2001 From: Greg Date: Fri, 21 Feb 2025 05:07:04 -0500 Subject: [PATCH 3/5] oops --- mix.exs | 1 + 1 file changed, 1 insertion(+) diff --git a/mix.exs b/mix.exs index 5cb4cbd853..b1190b6bb8 100644 --- a/mix.exs +++ b/mix.exs @@ -158,6 +158,7 @@ defmodule Ecto.MixProject do "guides/howtos/Test factories.md", "guides/cheatsheets/crud.cheatmd", "guides/cheatsheets/associations.cheatmd", + "guides/cheatsheets/types.cheatmd", "CHANGELOG.md" ] end From cd31985c8ff0663ea873e93fed18ecff9891ccc6 Mon Sep 17 00:00:00 2001 From: Greg Date: Fri, 21 Feb 2025 12:01:33 -0500 Subject: [PATCH 4/5] review comment --- guides/cheatsheets/types.cheatmd | 39 ------------------- guides/howtos/Duration Types with Postgrex.md | 38 ++++++++++++++++++ mix.exs | 2 +- 3 files changed, 39 insertions(+), 40 deletions(-) delete mode 100644 guides/cheatsheets/types.cheatmd create mode 100644 guides/howtos/Duration Types with Postgrex.md diff --git a/guides/cheatsheets/types.cheatmd b/guides/cheatsheets/types.cheatmd deleted file mode 100644 index cdf9f1c079..0000000000 --- a/guides/cheatsheets/types.cheatmd +++ /dev/null @@ -1,39 +0,0 @@ -# Types - -## Adapter-Specific Configuration - -### Using Duration with Postgrex - -#### Define migration with Postgres interval type - -```elixir -create table("movies") do - add :running_time, :interval -end -``` - -#### Define schema with Ecto duration type - -```elixir -defmodule Movie do - use Ecto.Schema - - schema "movies" do - field :running_time, :duration - end -end -``` - -#### Define Postgrex type module and specify intervals should be decoded to Duration - -```elixir -# Inside lib/my_app/postgrex_types.ex - -Postgrex.Types.define(MyApp.PostgrexTypes, [], interval_decode_type: Duration) -``` - -#### Make Ecto aware of Postgrex type module in configuration - -```elixir -config :my_app, MyApp.Repo, types: MyApp.PostgresTypes -``` diff --git a/guides/howtos/Duration Types with Postgrex.md b/guides/howtos/Duration Types with Postgrex.md new file mode 100644 index 0000000000..0b650b78e6 --- /dev/null +++ b/guides/howtos/Duration Types with Postgrex.md @@ -0,0 +1,38 @@ +# Duration Types with Postgrex + +As of Ecto 3.12.0, Ecto supports a `:duration` type which maps to Elixir's `Duration` struct (available as of Elixir 1.17). + +One natural use case for this is when using Postgres's `interval` type. Historically, Postgrex loads intervals from the database into a custom `Postgrex.Interval` struct. With the introduction of `Duration`, there is now the option to choose between the two. Please follow the steps below to enable mapping to `Duration`. + +1. Define your migration + +```elixir +create table("movies") do + add :running_time, :interval +end +``` + +2. Define your schema + +```elixir +defmodule Movie do + use Ecto.Schema + + schema "movies" do + field :running_time, :duration + end +end +``` + +3. Define your custom Postgrex type module and specify intervals should decode to `Duration` +```elixir +# Inside lib/my_app/postgrex_types.ex + +Postgrex.Types.define(MyApp.PostgrexTypes, [], interval_decode_type: Duration) +``` + +4. Make Ecto aware of the Postgrex type module in your configuration + +```elixir +config :my_app, MyApp.Repo, types: MyApp.PostgresTypes +``` diff --git a/mix.exs b/mix.exs index b1190b6bb8..1c57bbeb2e 100644 --- a/mix.exs +++ b/mix.exs @@ -148,6 +148,7 @@ defmodule Ecto.MixProject do "guides/howtos/Composable transactions with Multi.md", "guides/howtos/Constraints and Upserts.md", "guides/howtos/Data mapping and validation.md", + "guides/howtos/Duration Types with Postgrex.md", "guides/howtos/Dynamic queries.md", "guides/howtos/Multi tenancy with query prefixes.md", "guides/howtos/Multi tenancy with foreign keys.md", @@ -158,7 +159,6 @@ defmodule Ecto.MixProject do "guides/howtos/Test factories.md", "guides/cheatsheets/crud.cheatmd", "guides/cheatsheets/associations.cheatmd", - "guides/cheatsheets/types.cheatmd", "CHANGELOG.md" ] end From eaeb6a62053e3e259ef34b2ce5b60b3e8a48594d Mon Sep 17 00:00:00 2001 From: Greg Date: Fri, 21 Feb 2025 12:03:27 -0500 Subject: [PATCH 5/5] oops --- guides/howtos/Duration Types with Postgrex.md | 1 + 1 file changed, 1 insertion(+) diff --git a/guides/howtos/Duration Types with Postgrex.md b/guides/howtos/Duration Types with Postgrex.md index 0b650b78e6..4820185deb 100644 --- a/guides/howtos/Duration Types with Postgrex.md +++ b/guides/howtos/Duration Types with Postgrex.md @@ -25,6 +25,7 @@ end ``` 3. Define your custom Postgrex type module and specify intervals should decode to `Duration` + ```elixir # Inside lib/my_app/postgrex_types.ex