Class: DbSchema::Reader::Postgres

Inherits:
Object
  • Object
show all
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.1'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ Postgres

Returns a new instance of Postgres.



25
26
27
# File 'lib/db_schema/reader/postgres.rb', line 25

def initialize(connection)
  @connection = connection
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



23
24
25
# File 'lib/db_schema/reader/postgres.rb', line 23

def connection
  @connection
end

Instance Method Details

#read_enumsObject



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

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

#read_extensionsObject



53
54
55
56
57
# File 'lib/db_schema/reader/postgres.rb', line 53

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

#read_schemaObject



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

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

#read_table(table_name) ⇒ Object



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

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

#read_tablesObject



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

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