Class: Masker::Configurations::Postgres

Inherits:
Object
  • Object
show all
Defined in:
lib/masker/configurations/postgres.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(conn, config_path, logger, opts = {}) ⇒ Postgres

Returns a new instance of Postgres.



6
7
8
9
10
11
12
# 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

Instance Attribute Details

#tablesObject (readonly)

Returns the value of attribute tables.



4
5
6
# File 'lib/masker/configurations/postgres.rb', line 4

def tables
  @tables
end

Instance Method Details

#ids_to_maskObject



14
15
16
17
18
19
20
21
# 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_columnsObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# 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_tablesObject



23
24
25
26
27
28
29
30
31
32
# 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_columnsObject



60
61
62
63
64
65
66
# 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_tablesObject



54
55
56
57
58
# File 'lib/masker/configurations/postgres.rb', line 54

def remove_missing_tables
  missing_tables.each do |table|
    tables.delete(table)
  end
end

#tables_to_truncateObject



68
69
70
# File 'lib/masker/configurations/postgres.rb', line 68

def tables_to_truncate
  config['truncate']
end