Module: Anonymise::DbConnection

Included in:
DbFaker
Defined in:
lib/anonymise/db_connection.rb

Instance Method Summary collapse

Instance Method Details

#column_exists?(table, column) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
16
17
18
19
20
# File 'lib/anonymise/db_connection.rb', line 13

def column_exists?(table, column)
  res = connection.exec('SELECT TRUE FROM pg_attribute WHERE attrelid = (SELECT c.oid
FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE n.nspname = CURRENT_SCHEMA() AND c.relname =\''"#{table}"'\')
AND attname = \''"#{column}"'\' AND NOT attisdropped AND attnum > 0')
  records_count = res.count
  records_count.positive?
end

#connectionObject



22
23
24
25
26
27
# File 'lib/anonymise/db_connection.rb', line 22

def connection
  @connection ||= PG.connect db_args
rescue PG::Error => e
  puts e.message.colorize(:red)
  exit
end

#t_ablesObject



7
8
9
10
11
# File 'lib/anonymise/db_connection.rb', line 7

def t_ables
  t_ables = connection.exec "SELECT table_name FROM information_schema.tables
    WHERE table_schema = 'public'"
  @t_ables ||= t_ables.map { |m| m['table_name'] }
end