Class: DataMask::Mask

Inherits:
Object
  • Object
show all
Defined in:
lib/data_mask.rb

Instance Method Summary collapse

Constructor Details

#initialize(path = 'config') ⇒ Mask

Returns a new instance of Mask.



8
9
10
11
# File 'lib/data_mask.rb', line 8

def initialize(path = 'config')
  @db_conf = Config.parse(path + '/database.yml')
  @tasks = Config.parse(path + '/tasks.yml')
end

Instance Method Details

#exportObject



27
28
29
# File 'lib/data_mask.rb', line 27

def export
  system DBShell.new(@db_conf[:to]).export(to_file = true)
end

#migrateObject



17
18
19
20
21
# File 'lib/data_mask.rb', line 17

def migrate
  remote = DBShell.new(@db_conf[:from]).export
  local = DBShell.new(@db_conf[:to]).import
  system "#{remote} | #{local}"
end

#operate_db(op) ⇒ Object



13
14
15
# File 'lib/data_mask.rb', line 13

def operate_db(op)
  execute_sql "#{op.upcase} DATABASE %{database}" % @db_conf[:to]
end

#playObject



23
24
25
# File 'lib/data_mask.rb', line 23

def play
  mask(@db_conf[:to], @tasks)
end

#runObject



41
42
43
44
45
46
# File 'lib/data_mask.rb', line 41

def run
  tmp_db_clear
  operate_db('create')
  migrate
  play
end

#tmp_db_clearObject



31
32
33
34
35
36
37
38
39
# File 'lib/data_mask.rb', line 31

def tmp_db_clear
  return if
  if @db_conf[:to][:adapter] == 'postgres'
    # Force drop db while others may be connected
    execute_sql 'select pg_terminate_backend(procpid)' \
      " from pg_stat_activity where datname=’%{database}’" % @db_conf[:to]
  end
  execute_sql "DROP DATABASE  IF EXISTS %{database}" % @db_conf[:to]
end