class DataMask::Mask
Public Class Methods
new(path = 'config')
click to toggle source
# 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
Public Instance Methods
export()
click to toggle source
# File lib/data_mask.rb, line 27 def export system DBShell.new(@db_conf[:to]).export(to_file = true) end
migrate()
click to toggle source
# 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)
click to toggle source
# File lib/data_mask.rb, line 13 def operate_db(op) execute_sql "#{op.upcase} DATABASE %{database}" % @db_conf[:to] end
play()
click to toggle source
# File lib/data_mask.rb, line 23 def play mask(@db_conf[:to], @tasks) end
run()
click to toggle source
# File lib/data_mask.rb, line 41 def run tmp_db_clear operate_db('create') migrate play end
tmp_db_clear()
click to toggle source
# 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
Private Instance Methods
build_url_without_db(data)
click to toggle source
# File lib/data_mask.rb, line 51 def build_url_without_db(data) return "%{adapter}://%{host}:%{port}" % data if data[:port] "%{adapter}://%{host}" % data end
execute_sql(sql)
click to toggle source
# File lib/data_mask.rb, line 84 def execute_sql(sql) Sequel.connect(build_url_without_db(@db_conf[:to])) do |db| begin db.run sql rescue end end end
mask(config, tasks)
click to toggle source
# File lib/data_mask.rb, line 64 def mask(config, tasks) db = Sequel.connect(config) tasks.each do |table, task| table = db[table] task.each do |key, value| if key == :each_row value.each do |sub_k, sub_v| # Iterate table and update each item table.each { |row| table.where(id: row[:id]).update(sub_k => parse_mask(sub_v, binding)) } end else table.update(key => parse_mask(value, binding)) end end end db.disconnect end
parse_mask(mask, binding)
click to toggle source
# File lib/data_mask.rb, line 56 def parse_mask(mask, binding) return mask unless mask.is_a?(String) && mask.start_with?('%=') result = eval(mask[2..-1], binding) return result if result.is_a?(String) || result.is_a?(Integer) throw ArgumentError('wrong value type') end