Class: Schemas::Descriptor
- Inherits:
-
Object
- Object
- Schemas::Descriptor
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 =
<<-SQL
SELECT
#{LOWERCASE_DB_IDENTIFIERS ? 'LOWER(table_schema) AS ': ''}table_schema,
#{LOWERCASE_DB_IDENTIFIERS ? 'LOWER(table_name) AS ': ''}table_name,
#{LOWERCASE_DB_IDENTIFIERS ? 'LOWER(column_name) AS ': ''}column_name,
udt_name,
character_maximum_length
FROM information_schema.columns
WHERE table_schema NOT IN ('INFORMATION_SCHEMA', 'information_schema', 'pg_catalog', 'public')
ORDER BY table_schema, table_name, ordinal_position;
SQL
Constants included
from RedisStore
RedisStore::EXPIRE
Instance Method Summary
collapse
Methods included from RedisStore
#redis_retrieve, #redis_store!
Constructor Details
Returns a new instance of Descriptor.
20
21
22
23
24
25
26
|
# File 'lib/schemas/descriptor.rb', line 20
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
#columns ⇒ Object
36
37
38
39
40
41
42
43
44
45
|
# File 'lib/schemas/descriptor.rb', line 36
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
|
#key ⇒ Object
47
48
49
|
# File 'lib/schemas/descriptor.rb', line 47
def key
@key ||= "#{@role}_schema_descriptor"
end
|
#schemas ⇒ Object
28
29
30
|
# File 'lib/schemas/descriptor.rb', line 28
def schemas
retrieve.map { |row| row['table_schema'] }.uniq
end
|
#table_columns(schema) ⇒ Object
32
33
34
|
# File 'lib/schemas/descriptor.rb', line 32
def table_columns(schema)
retrieve.select { |row| row['table_schema'] == schema }.group_by { |row| row['table_name'] }
end
|