Skip to content

Commit

Permalink
Enable introspection of electrified enum types in the proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
alco committed Nov 15, 2023
1 parent cb0e8ad commit cd13b02
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions components/electric/lib/electric/postgres/proxy/prisma/query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,22 @@ defmodule Electric.Postgres.Proxy.Prisma.Query.ViewV5_2 do
end
end

defmodule Electric.Postgres.Proxy.Prisma.Query.Type do
def data_rows(nspname_array, schema) do
namespaces = Electric.Postgres.Proxy.Prisma.Query.parse_name_array(nspname_array)

for table <- schema.tables,
table.name.schema != Electric.Postgres.Extension.schema(),
%{type: col_type} <- table.columns,
col_type.kind == :ENUM,
[schema_name, unqualified_name] = String.split(col_type.name, "."),
schema_name in namespaces,
val <- col_type.values do
[unqualified_name, val, schema_name]
end
end
end

defmodule Electric.Postgres.Proxy.Prisma.Query.TypeV4_8 do
@moduledoc """
SELECT t.typname as name, e.enumlabel as value, n.nspname as namespace
Expand Down Expand Up @@ -485,9 +501,8 @@ defmodule Electric.Postgres.Proxy.Prisma.Query.TypeV4_8 do
]
end

# we don't support custom types
def data_rows([_nspname], _schema, _config) do
[]
def data_rows([nspname_array], schema, _config) do
Electric.Postgres.Proxy.Prisma.Query.Type.data_rows(nspname_array, schema)
end
end

Expand Down Expand Up @@ -526,9 +541,9 @@ defmodule Electric.Postgres.Proxy.Prisma.Query.TypeV5_2 do
]
end

# we don't support custom types
def data_rows([_nspname], _schema, _config) do
[]
def data_rows([nspname_array], schema, _config) do
Electric.Postgres.Proxy.Prisma.Query.Type.data_rows(nspname_array, schema)
|> Enum.map(fn [name, value, namespace] -> [name, value, namespace, nil] end)
end
end

Expand Down

0 comments on commit cd13b02

Please sign in to comment.