Skip to content

Correctly handle generated array columns #661

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/ecto/adapters/postgres/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1750,8 +1750,10 @@ if Code.ensure_loaded?(Postgrex) do
defp options_expr(options),
do: [?\s, options]

defp column_type({:array, type}, opts),
do: [column_type(type, opts), "[]"]
defp column_type({:array, type}, opts) do
[type, opts] = column_type(type, opts)
[type, "[]", opts]
end

defp column_type(type, opts) when type in ~w(time utc_datetime naive_datetime)a do
generated = Keyword.get(opts, :generated)
Expand Down
5 changes: 3 additions & 2 deletions test/ecto/adapters/postgres_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2331,12 +2331,13 @@ defmodule Ecto.Adapters.PostgresTest do
primary_key: true,
generated: "ALWAYS AS IDENTITY (MINVALUE 0 START WITH 0 INCREMENT BY 1)"
]},
{:add, :id_str, :string, [generated: ~s|ALWAYS AS (id) STORED|]}
{:add, :id_str, :string, [generated: ~s|ALWAYS AS (id) STORED|]},
{:add, :tags, {:array, :text}, [generated: ~s|ALWAYS AS (ARRAY['foo','bar']) STORED|]}
]}

assert execute_ddl(create) == [
"""
CREATE TABLE "posts" ("id" integer GENERATED ALWAYS AS IDENTITY (MINVALUE 0 START WITH 0 INCREMENT BY 1), "id_str" varchar(255) GENERATED ALWAYS AS (id) STORED, PRIMARY KEY ("id"))
CREATE TABLE "posts" ("id" integer GENERATED ALWAYS AS IDENTITY (MINVALUE 0 START WITH 0 INCREMENT BY 1), "id_str" varchar(255) GENERATED ALWAYS AS (id) STORED, \"tags\" text[] GENERATED ALWAYS AS (ARRAY['foo','bar']) STORED, PRIMARY KEY ("id"))
"""
|> remove_newlines
]
Expand Down
Loading