Class: Brillo::Scrubber

Inherits:
Object
  • Object
show all
Includes:
Helpers::ExecHelper, Logger
Defined in:
lib/brillo/scrubber.rb

Overview

Responsible for creating a fresh scrubbed SQL copy of the database, as specified via config, and uploading to S3

Constant Summary collapse

JUMBLE_PRNG =
Random.new
LATEST_LIMIT =
1_000
SCRUBBERS =

Define some procs as scrubbing strategies for Polo

{
  default_time: ->(t) { t.nil? ? Time.now.to_s(:sql) : t },
  email:        ->(e) { e.match(/@caring.com/) ? e : Digest::MD5.hexdigest(e) + "@example.com".freeze },
  jumble:       ->(j) { j.downcase.chars.shuffle!(random: JUMBLE_PRNG.clone).join },
  phone:        ->(n) { n = n.split(' ').first; n && n.length > 9 ? n[0..-5] + n[-1] + n[-2] + n[-3] + n[-4] : n},   # strips extensions
  name:         ->(n) { n.downcase.split(' ').map do |word|
      word.chars.shuffle!(random: JUMBLE_PRNG.clone).join
    end.each(&:capitalize!).join(' ')
  },
}
TACTICS =
{
  latest: -> (klass) { klass.order('id desc').limit(LATEST_LIMIT).pluck(:id) },
  all:    -> (klass) { klass.pluck(:id) }
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logger

#logger, logger, logger=

Methods included from Helpers::ExecHelper

#execute, #execute!

Constructor Details

#initialize(config) ⇒ Scrubber

Returns a new instance of Scrubber.



29
30
31
32
# File 'lib/brillo/scrubber.rb', line 29

def initialize(config)
  @config = config
  @adapter = config.adapter
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



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

def adapter
  @adapter
end

#configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
end

#transferrerObject (readonly)

Returns the value of attribute transferrer.



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

def transferrer
  @transferrer
end

Instance Method Details

#explore_all_classesObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/brillo/scrubber.rb', line 43

def explore_all_classes
  File.open(config.dump_path, "a") do |sql_file|
    sql_file.puts(adapter.header)
    klass_association_map.each do |klass, options|
      begin
        klass = deserialize_class(klass)
        tactic = deserialize_tactic(options)
      rescue ConfigParseError => e
        logger.error "Error in brillo.yml: #{e.message}"
        next
      end
      associations = options.fetch("associations", [])
      explore_class(klass, tactic, associations) do |insert|
        sql_file.puts(insert)
      end
    end
    sql_file.puts(adapter.footer)
  end
end

#scrub!Object



34
35
36
37
38
39
40
41
# File 'lib/brillo/scrubber.rb', line 34

def scrub!
  FileUtils.rm config.compressed_filename, force: true
  configure_polo
  adapter.dump_structure_and_migrations(config)
  explore_all_classes
  compress
  config.transferrer.upload
end