Class: Schemas::Descriptor

Inherits:
Object
  • Object
show all
Includes:
RedisStore
Defined in:
lib/schemas/descriptor.rb

Constant Summary collapse

INFORMATION_SCHEMA_QUERY =
<<-SQL
  SELECT
    table_schema,
    table_name,
    column_name,
    udt_name,
    character_maximum_length
  FROM information_schema.columns
  WHERE table_schema NOT IN ('information_schema', 'pg_catalog', 'public')
  ORDER BY table_schema, table_name, ordinal_position;
SQL
SCHEMA_REFRESH_INTERVAL =
APP_CONFIG['schema_refresh_interval'] || 1.day

Constants included from RedisStore

RedisStore::EXPIRE

Instance Method Summary collapse

Methods included from RedisStore

#redis_retrieve, #redis_store!

Constructor Details

#initialize(role) ⇒ Descriptor

Returns a new instance of Descriptor.



18
19
20
21
22
23
24
# File 'lib/schemas/descriptor.rb', line 18

def initialize(role)
  @role = role
  @cache = []

  Rails.logger.info("Start schema refresher thread for #{@role}")
  @refresher_thread = Thread.new{ schema_refresher }
end

Instance Method Details

#columnsObject



34
35
36
37
38
39
40
41
42
43
# File 'lib/schemas/descriptor.rb', line 34

def columns
  retrieve.map do |column|
    HashWithIndifferentAccess.new(
      column: column['column_name'],
      table: column['table_name'],
      schema: column['table_schema'],
      type: column['udt_name']
    )
  end
end

#keyObject



45
46
47
# File 'lib/schemas/descriptor.rb', line 45

def key
  @key ||= "#{@role}_schema_descriptor"
end

#schemasObject



26
27
28
# File 'lib/schemas/descriptor.rb', line 26

def schemas
  retrieve.map { |row| row['table_schema'] }.uniq
end

#table_columns(schema) ⇒ Object



30
31
32
# File 'lib/schemas/descriptor.rb', line 30

def table_columns(schema)
  retrieve.select { |row| row['table_schema'] == schema }.group_by { |row| row['table_name'] }
end