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) { Digest::MD5.hexdigest(e) + "@example.com".freeze },
  jumble:       ->(j) { j.downcase.chars.shuffle!(random: JUMBLE_PRNG.clone).join },
  # strips extensions
  phone:        ->(n) { n = n.split(' ').first; n && n.length > 9 ? n[0..-5] + n[-1] + n[-2] + n[-3] + n[-4] : n},
  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("#{klass.primary_key} desc").limit(LATEST_LIMIT).pluck(klass.primary_key) },
  all:    -> (klass) { klass.pluck(klass.primary_key) }
}

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



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

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

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



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

def adapter
  @adapter
end

#configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
end

#transferrerObject (readonly)

Returns the value of attribute transferrer.



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

def transferrer
  @transferrer
end

Instance Method Details

#explore_all_classesObject



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

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(klass, 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
    ActiveRecord::Base.descendants.each do |klass|
      sql_file.puts(adapter.table_footer(klass))
    end
    sql_file.puts(adapter.footer)
  end
end

#scrub!Object



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

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