Module: DynamicMigrations::Postgres::Server::Database::EnumsLoader

Included in:
DynamicMigrations::Postgres::Server::Database
Defined in:
lib/dynamic_migrations/postgres/server/database/enums_loader.rb

Instance Method Summary collapse

Instance Method Details

#fetch_enumsObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/dynamic_migrations/postgres/server/database/enums_loader.rb', line 8

def fetch_enums
  rows = connection.exec(<<~SQL)
    SELECT
      n.nspname AS schema_name,
      t.typname AS enum_name,
      e.enumlabel AS enum_value,
      obj_description(e.enumtypid, 'pg_type') as enum_description
    FROM pg_type t
      JOIN pg_enum e ON t.oid = e.enumtypid
      JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace
      ORDER BY enumsortorder
  SQL

  schemas = {}
  rows.each do |row|
    schema_name = row["schema_name"].to_sym
    enum_name = row["enum_name"].to_sym
    enum_value = row["enum_value"]

    schema = schemas[schema_name] ||= {}
    enum = schema[enum_name] ||= {
      values: [],
      description: row["enum_description"]
    }
    enum[:values] << enum_value
  end
  schemas
end