class Schemas::Descriptor

Constants

INFORMATION_SCHEMA_QUERY
LOWERCASE_DB_IDENTIFIERS
SCHEMA_REFRESH_INTERVAL

Public Class Methods

new(role) click to toggle source
# 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 }
  @refresher_thread.abort_on_exception = true
end

Public Instance Methods

columns() click to toggle source
# File lib/schemas/descriptor.rb, line 41
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() click to toggle source
# File lib/schemas/descriptor.rb, line 52
def key
  @key ||= "#{@role}_schema_descriptor"
end
schemas() click to toggle source
# File lib/schemas/descriptor.rb, line 33
def schemas
  retrieve.map { |row| row['table_schema'] }.uniq
end
table_columns(schema) click to toggle source
# File lib/schemas/descriptor.rb, line 37
def table_columns(schema)
  retrieve.select { |row| row['table_schema'] == schema }.group_by { |row| row['table_name'] }
end

Private Instance Methods

exec_schema_query() click to toggle source
# File lib/schemas/descriptor.rb, line 82
def exec_schema_query
  connection = AnalyticDBConnectionPool.instance.get(@role)
  connection.fetch_all_hash(INFORMATION_SCHEMA_QUERY)
end
filter_tables(schemas) click to toggle source
# File lib/schemas/descriptor.rb, line 87
def filter_tables(schemas)
  return schemas unless TABLE_BLACKLIST
  
  schemas.reject do |column|
    schema_blacklist = TABLE_BLACKLIST[column['table_schema']]
    next unless schema_blacklist
    schema_blacklist.any? { |bl_item| Regexp.new(bl_item).match(column['table_name']) }
  end
end
refresh_schema() click to toggle source
# File lib/schemas/descriptor.rb, line 69
def refresh_schema
  Rails.logger.info('Schemas::Descriptor.refresh_schema')
  result = nil
  Pester.schema_refresh.retry do
    result = exec_schema_query
  end

  if result
    redis_store!(filter_tables(result.to_a))
    redis_retrieve
  end
end
retrieve() click to toggle source
# File lib/schemas/descriptor.rb, line 58
def retrieve
  redis_retrieve
end
schema_refresher() click to toggle source
# File lib/schemas/descriptor.rb, line 62
def schema_refresher
  loop do
    refresh_schema
    sleep(SCHEMA_REFRESH_INTERVAL.to_i)
  end
end