File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change @@ -320,6 +320,38 @@ def extensions
320
320
exec_query ( "SELECT extname FROM pg_extension" , "SCHEMA" ) . cast_values
321
321
end
322
322
323
+ # Returns a list of defined enum types, and their values.
324
+ def enum_types
325
+ query = <<~SQL
326
+ SELECT
327
+ type.typname AS name,
328
+ string_agg(enum.enumlabel, ',' ORDER BY enum.enumsortorder) AS value
329
+ FROM pg_enum AS enum
330
+ JOIN pg_type AS type
331
+ ON (type.oid = enum.enumtypid)
332
+ GROUP BY type.typname;
333
+ SQL
334
+ exec_query ( query , "SCHEMA" ) . cast_values
335
+ end
336
+
337
+ # Given a name and an array of values, creates an enum type.
338
+ def create_enum ( name , values )
339
+ sql_values = values . map { |s | "'#{ s } '" } . join ( ", " )
340
+ query = <<~SQL
341
+ DO $$
342
+ BEGIN
343
+ IF NOT EXISTS (
344
+ SELECT 1 FROM pg_type t
345
+ WHERE t.typname = '#{ name } '
346
+ ) THEN
347
+ CREATE TYPE \" #{ name } \" AS ENUM (#{ sql_values } );
348
+ END IF;
349
+ END
350
+ $$;
351
+ SQL
352
+ exec_query ( query )
353
+ end
354
+
323
355
# Returns the configured supported identifier length supported by PostgreSQL
324
356
def max_identifier_length
325
357
@max_identifier_length ||= query_value ( "SHOW max_identifier_length" , "SCHEMA" ) . to_i
You can’t perform that action at this time.
0 commit comments