Class: Backup
- Inherits:
-
Object
- Object
- Backup
- Defined in:
- lib/travis-backup.rb
Overview
main travis-backup class
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
-
#dry_run_removed ⇒ Object
readonly
Returns the value of attribute dry_run_removed.
Instance Method Summary collapse
- #connect_db(url = @config.database_url) ⇒ Object
-
#initialize(config_args = {}) ⇒ Backup
constructor
A new instance of Backup.
- #move_logs ⇒ Object
-
#process_repo(repository) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/MethodLength.
- #run(args = {}) ⇒ Object
Constructor Details
#initialize(config_args = {}) ⇒ Backup
Returns a new instance of Backup.
14 15 16 17 18 19 20 21 22 |
# File 'lib/travis-backup.rb', line 14 def initialize(config_args={}) @config = Config.new(config_args) if @config.dry_run @dry_run_removed = {builds: [], jobs: [], logs: []} end connect_db end |
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
11 12 13 |
# File 'lib/travis-backup.rb', line 11 def config @config end |
#dry_run_removed ⇒ Object (readonly)
Returns the value of attribute dry_run_removed.
12 13 14 |
# File 'lib/travis-backup.rb', line 12 def dry_run_removed @dry_run_removed end |
Instance Method Details
#connect_db(url = @config.database_url) ⇒ Object
24 25 26 |
# File 'lib/travis-backup.rb', line 24 def connect_db(url=@config.database_url) ActiveRecord::Base.establish_connection(url) end |
#move_logs ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/travis-backup.rb', line 66 def move_logs connect_db(@config.database_url) Log.order(:id).in_groups_of(@config.limit.to_i, false).map do |logs_batch| log_hashes = logs_batch.as_json connect_db(@config.destination_db_url) log_hashes.each do |log_hash| new_log = Log.new(log_hash) new_log.save! end connect_db(@config.database_url) logs_batch.each(&:destroy) end end |
#process_repo(repository) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/MethodLength
83 84 85 86 87 88 89 90 |
# File 'lib/travis-backup.rb', line 83 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
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 |
# File 'lib/travis-backup.rb', line 28 def run(args={}) return move_logs if @config.move_logs 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}" puts " - logs: #{@dry_run_removed[:logs].to_json}" end end |