class Masker::Configurations::Postgres

Attributes

config[R]
conn[R]
logger[R]
opts[R]
tables[R]

Public Class Methods

new(conn, config_path, logger, opts = {}) click to toggle source
# File lib/masker/configurations/postgres.rb, line 6
def initialize(conn, config_path, logger, opts = {})
  @config = Configuration.load(config_path)
  @conn = conn
  @logger = logger
  @opts = opts
  @tables = config['mask']
end

Public Instance Methods

ids_to_mask() click to toggle source
# File lib/masker/configurations/postgres.rb, line 14
def ids_to_mask
  @ids_to_mask ||=
    tables.keys.each_with_object(Hash.new { |k, v| k[v] = [] }) do |table, ids|
      conn.exec("SELECT id FROM #{table};") do |result|
        ids[table].concat(result.values.flatten - Array(opts.dig(:safe_ids, table.to_sym)).map(&:to_s))
      end
    end
end
missing_columns() click to toggle source
# File lib/masker/configurations/postgres.rb, line 34
      def missing_columns
        tables.each_with_object(Hash.new { |h, k| h[k] = [] }) do |(table_name, columns), missing_columns|
          columns.keys.each do |column_name|
            sql = <<~SQL
              SELECT EXISTS (
                SELECT 1 FROM information_schema.columns
                WHERE table_name='#{table_name}'
                AND column_name='#{column_name}'
              );
            SQL
            conn.exec(sql) do |result|
              if result[0]['exists'] == 'f'
                missing_columns[table_name] << column_name
                logger.warn "Column: #{table_name}:#{column_name} exists in configuration but not in database."
              end
            end
          end
        end
      end
missing_tables() click to toggle source
# File lib/masker/configurations/postgres.rb, line 23
def missing_tables
  tables.keys.each_with_object([]) do |table_name, missing_tables|
    conn.exec("SELECT EXISTS (SELECT 1 FROM pg_tables WHERE tablename = '#{table_name}');") do |result|
      if result[0]['exists'] == 'f'
        missing_tables << table_name
        logger.warn "Table: #{table_name} exists in configuration but not in database."
      end
    end
  end
end
remove_missing_columns() click to toggle source
# File lib/masker/configurations/postgres.rb, line 60
def remove_missing_columns
  missing_columns.each do |table, columns|
    columns.each do |column|
      tables[table].delete(column)
    end
  end
end
remove_missing_tables() click to toggle source
# File lib/masker/configurations/postgres.rb, line 54
def remove_missing_tables
  missing_tables.each do |table|
    tables.delete(table)
  end
end
tables_to_truncate() click to toggle source
# File lib/masker/configurations/postgres.rb, line 68
def tables_to_truncate
  config['truncate']
end