Skip to content

Commit 521ed87

Browse files
committed
Postgres, update enum_types and create_enum method to fix some enum tests
1 parent 4551603 commit 521ed87

File tree

1 file changed

+35
-21
lines changed

1 file changed

+35
-21
lines changed

lib/arjdbc/postgresql/adapter.rb

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -335,33 +335,47 @@ def extensions
335335
# Returns a list of defined enum types, and their values.
336336
def enum_types
337337
query = <<~SQL
338-
SELECT
339-
type.typname AS name,
340-
string_agg(enum.enumlabel, ',' ORDER BY enum.enumsortorder) AS value
341-
FROM pg_enum AS enum
342-
JOIN pg_type AS type
343-
ON (type.oid = enum.enumtypid)
344-
GROUP BY type.typname;
338+
SELECT
339+
type.typname AS name,
340+
type.OID AS oid,
341+
n.nspname AS schema,
342+
string_agg(enum.enumlabel, ',' ORDER BY enum.enumsortorder) AS value
343+
FROM pg_enum AS enum
344+
JOIN pg_type AS type ON (type.oid = enum.enumtypid)
345+
JOIN pg_namespace n ON type.typnamespace = n.oid
346+
WHERE n.nspname = ANY (current_schemas(false))
347+
GROUP BY type.OID, n.nspname, type.typname;
345348
SQL
346-
exec_query(query, "SCHEMA").cast_values
349+
350+
internal_exec_query(query, "SCHEMA", allow_retry: true, materialize_transactions: false).cast_values.each_with_object({}) do |row, memo|
351+
name, schema = row[0], row[2]
352+
schema = nil if schema == current_schema
353+
full_name = [schema, name].compact.join(".")
354+
memo[full_name] = row.last
355+
end.to_a
347356
end
348357

349358
# Given a name and an array of values, creates an enum type.
350-
def create_enum(name, values)
351-
sql_values = values.map { |s| "'#{s}'" }.join(", ")
359+
def create_enum(name, values, **options)
360+
sql_values = values.map { |s| quote(s) }.join(", ")
361+
scope = quoted_scope(name)
352362
query = <<~SQL
353-
DO $$
354-
BEGIN
355-
IF NOT EXISTS (
356-
SELECT 1 FROM pg_type t
357-
WHERE t.typname = '#{name}'
358-
) THEN
359-
CREATE TYPE \"#{name}\" AS ENUM (#{sql_values});
360-
END IF;
361-
END
362-
$$;
363+
DO $$
364+
BEGIN
365+
IF NOT EXISTS (
366+
SELECT 1
367+
FROM pg_type t
368+
JOIN pg_namespace n ON t.typnamespace = n.oid
369+
WHERE t.typname = #{scope[:name]}
370+
AND n.nspname = #{scope[:schema]}
371+
) THEN
372+
CREATE TYPE #{quote_table_name(name)} AS ENUM (#{sql_values});
373+
END IF;
374+
END
375+
$$;
363376
SQL
364-
exec_query(query)
377+
378+
internal_exec_query(query).tap { reload_type_map }
365379
end
366380

367381
# Returns the configured supported identifier length supported by PostgreSQL

0 commit comments

Comments
 (0)