Class: Backup

Inherits:
Object
  • Object
show all
Defined in:
lib/travis-backup.rb

Overview

main travis-backup class

Instance Method Summary collapse

Constructor Details

#initialize(config_args = {}) ⇒ Backup

Returns a new instance of Backup.



10
11
12
13
14
15
16
17
18
# File 'lib/travis-backup.rb', line 10

def initialize(config_args={})
  @config = Config.new(config_args)

  if @config.dry_run
    @dry_run_removed = {builds: [], jobs: []}
  end

  connect_db
end

Instance Method Details

#connect_dbObject



20
21
22
# File 'lib/travis-backup.rb', line 20

def connect_db
  ActiveRecord::Base.establish_connection(@config.database_url)
end

#process_repo(repository) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/MethodLength



59
60
61
62
63
64
65
66
# File 'lib/travis-backup.rb', line 59

def process_repo(repository) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
  threshold = @config.threshold.to_i.months.ago.to_datetime
  current_build_id = repository.current_build_id || -1
  repository.builds.where('created_at < ? and id != ?', threshold, current_build_id)
            .in_groups_of(@config.limit.to_i, false).map do |builds_batch|
    @config.if_backup ? save_and_destroy_batch(builds_batch, repository) : destroy_batch(builds_batch)
  end.compact
end

#run(args = {}) ⇒ Object



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
# File 'lib/travis-backup.rb', line 24

def run(args={})
  user_id = args[:user_id] || @config.user_id
  repo_id = args[:repo_id] || @config.repo_id
  org_id = args[:org_id] || @config.org_id

  if user_id
    owner_id = user_id
    owner_type = 'User'
  elsif org_id
    owner_id = org_id
    owner_type = 'Organization'
  elsif repo_id
    repo_id = repo_id
  end

  if owner_id
    Repository.where('owner_id = ? and owner_type = ?', owner_id, owner_type).order(:id).each do |repository|
      process_repo(repository)
    end
  elsif repo_id
    repository = Repository.find(repo_id)
    process_repo(repository)
  else
    Repository.order(:id).each do |repository|
      process_repo(repository)
    end
  end

  if @config.dry_run
    puts 'Dry run active. The following data would be removed in normal mode:'
    puts " - builds: #{@dry_run_removed[:builds].to_json}"
    puts " - jobs: #{@dry_run_removed[:jobs].to_json}"
  end
end