Module: DbSchema::Reader::Postgres

Defined in:
lib/db_schema/reader/postgres.rb,
lib/db_schema/reader/postgres/table.rb,
lib/db_schema/reader/postgres/version.rb

Defined Under Namespace

Classes: Table

Constant Summary collapse

ENUMS_QUERY =
<<-SQL.freeze
  SELECT t.typname AS name,
   array_agg(e.enumlabel ORDER BY e.enumsortorder) AS values
    FROM pg_enum AS e
    JOIN pg_type AS t
ON t.oid = e.enumtypid
GROUP BY name
SQL
EXTENSIONS_QUERY =
<<-SQL.freeze
SELECT extname
  FROM pg_extension
 WHERE extname != 'plpgsql'
SQL
VERSION =
'0.1'

Class Method Summary collapse

Class Method Details

.read_enums(connection) ⇒ Object



42
43
44
45
46
# File 'lib/db_schema/reader/postgres.rb', line 42

def read_enums(connection)
  connection[ENUMS_QUERY].map do |enum_data|
    Definitions::Enum.new(enum_data[:name].to_sym, enum_data[:values].map(&:to_sym))
  end
end

.read_extensions(connection) ⇒ Object



48
49
50
51
52
# File 'lib/db_schema/reader/postgres.rb', line 48

def read_extensions(connection)
  connection[EXTENSIONS_QUERY].map do |extension_data|
    Definitions::Extension.new(extension_data[:extname].to_sym)
  end
end

.read_schema(connection) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/db_schema/reader/postgres.rb', line 24

def read_schema(connection)
  Definitions::Schema.new(
    tables:     read_tables(connection),
    enums:      read_enums(connection),
    extensions: read_extensions(connection)
  )
end

.read_table(table_name, connection) ⇒ Object



38
39
40
# File 'lib/db_schema/reader/postgres.rb', line 38

def read_table(table_name, connection)
  Table.new(connection, table_name).read
end

.read_tables(connection) ⇒ Object



32
33
34
35
36
# File 'lib/db_schema/reader/postgres.rb', line 32

def read_tables(connection)
  connection.tables.map do |table_name|
    read_table(table_name, connection)
  end
end