Class: Schemas::Descriptor

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

Constant Summary collapse

SCHEMA_REFRESH_INTERVAL =
APP_CONFIG['schema_refresh_interval'] || 1.day
LOWERCASE_DB_IDENTIFIERS =
APP_CONFIG['lowercase_db_identifiers'] || false
INFORMATION_SCHEMA_QUERY =
"  SELECT\n    \#{LOWERCASE_DB_IDENTIFIERS ? 'LOWER(table_schema) AS ': ''}table_schema,\n    \#{LOWERCASE_DB_IDENTIFIERS ? 'LOWER(table_name) AS ': ''}table_name,\n    \#{LOWERCASE_DB_IDENTIFIERS ? 'LOWER(column_name) AS ': ''}column_name,\n    (COALESCE(udt_name, data_type) || \n      CASE \n        WHEN character_maximum_length IS NOT NULL THEN '(' || character_maximum_length || ')'\n        WHEN numeric_precision IS NOT NULL THEN '(' || numeric_precision || ', ' || numeric_scale || ')'\n        ELSE ''\n      END) AS udt_name,\n    character_maximum_length\n  FROM information_schema.columns\n  WHERE table_schema NOT IN ('INFORMATION_SCHEMA', 'information_schema', 'pg_catalog', 'public')\n  ORDER BY table_schema, table_name, ordinal_position;\n"

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.



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

def initialize(role)
  @role = role

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

Instance Method Details

#columnsObject



40
41
42
43
44
45
46
47
48
49
# File 'lib/schemas/descriptor.rb', line 40

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



51
52
53
# File 'lib/schemas/descriptor.rb', line 51

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

#schemasObject



32
33
34
# File 'lib/schemas/descriptor.rb', line 32

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

#table_columns(schema) ⇒ Object



36
37
38
# File 'lib/schemas/descriptor.rb', line 36

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