Class: DataDumpJob

Inherits:
ApplicationJob show all
Defined in:
/var/apps/qpixel/app/jobs/data_dump_job.rb

Constant Summary collapse

DEFAULT_TIMESTAMP =
'1970-01-01 00:00:00'.freeze

Instance Method Summary collapse

Methods inherited from ApplicationJob

#exec, #initialize, #logger

Constructor Details

This class inherits a constructor from ApplicationJob

Instance Method Details

#perform(drop_db_after: true) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File '/var/apps/qpixel/app/jobs/data_dump_job.rb', line 6

def perform(drop_db_after: true)
  permitted = YAML.safe_load_file(Rails.root.join('db/scripts/dump_permitted_columns.yml'))
  logger.info "Found #{permitted&.size} tables to dump."

  begin
    exec('SET FOREIGN_KEY_CHECKS = 0;')
    exec('DROP DATABASE IF EXISTS qpixel_dump;')
    exec('CREATE DATABASE qpixel_dump;')

    @db_creds = Rails.configuration.database_configuration[Rails.env]
    @username = @db_creds['username']
    @password = @db_creds['password']
    @database = @db_creds['database']
    @port = @db_creds['port']
    @host = @db_creds['host']

    mysqldump_command = build_command('mysqldump', '-h', @host, '-u', @username, "-p#{@password}", '-d', @database,
                                      '--no-tablespaces', "--port=#{@port}", ssl_state)
    mysql_command = build_command('mysql', '-h', @host, '-u', @username, "-p#{@password}", "--port=#{@port}",
                                  '-D', 'qpixel_dump', ssl_state)
    logger.debug 'Running system command:'
    logger.debug "#{mysqldump_command} | #{mysql_command}"
    copy_success = system("#{mysqldump_command} | #{mysql_command}")

    unless copy_success
      logger.fatal "Couldn't replicate database: nonzero exit code"
      return
    end

    logger.info 'Copied database structure.'

    initialize_defaults

    logger.info 'Initialized defaults.'

    permitted&.each do |table, data|
      migrate_table(table, data)
    end

    logger.info 'Migrated data.'

    file_path = Rails.root.join('tmp/qpixel_export.sql')
    export_cmd = build_command('mysqldump', '-h', @host, '-u', @username, "-p#{@password}", "--port=#{@port}",
                               'qpixel_dump', '--no-tablespaces', ssl_state, '>', file_path)
    logger.debug 'Running system command:'
    logger.debug export_cmd
    export_success = system(export_cmd)

    unless export_success
      logger.fatal "Couldn't export database: nonzero exit code"
      return
    end

    logger.info 'Exported database.'

    raw_checksum = `sha256sum #{file_path}`.split[0]
    logger.debug "Export checksum: #{raw_checksum}"

    dump = Dump.create(title: "Data Dump #{Time.now.strftime('%Y-%m-%d')}",
                       comment: "Automatically generated data dump as of #{Time.now.strftime('%Y-%m-%d %H:%M:%S')}.",
                       file: File.open(file_path),
                       automatic: true,
                       checksum: "SHA256:#{raw_checksum.chars.in_groups_of(8).map(&:join).join('-')}")
    Dump.where(automatic: true).where.not(id: dump.id).destroy_all
  rescue ActiveRecord::ConnectionFailed
    logger.fatal "Couldn't connect to database. Have you run `GRANT ALL ON qpixel_dump.*` for your DB user?"
  ensure
    exec('SET FOREIGN_KEY_CHECKS = 1;')
    if drop_db_after
      exec('DROP DATABASE qpixel_dump;')
    end
  end
end