travis-backup 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 82c4a389c3ed4d73db6f9b50b4d80a17524f646b1ce4fa6737821a8e4cc7009d
4
- data.tar.gz: 514f7b4042b0ef3297371ec5062f3d0ebcb001b1916f35b2c66e0ea6ffb4a201
3
+ metadata.gz: dca7a53c9e3689c5f0988d074296449769a106b86ed6ddc26c6a8b7c0cba15b2
4
+ data.tar.gz: 25765ff21af6e3bbba2fbf5a971d678acd6447b59c54fc685ddaa0912e84f73a
5
5
  SHA512:
6
- metadata.gz: fc3c44b552f31d73d62469cb619c516297ed7a3b02ea4694c8bc39cd2fd48cecb80a5919ec5299f0ec2ea2fc4b31c7bfcc0182cce22721d685b68f1fae921c20
7
- data.tar.gz: 3440dff96a5940425568de23c2c714f663e2b351694d6dcd8ad70646f8627e69258386b2e415da3172cdde5ddb5b126019d555722de541d183edc187ee42289c
6
+ metadata.gz: bcc37fc6219ac84eda44f953879952567eb33981a9a2436b5922acefa515994262ab174e25e8fe9660153e73e3d55838eaa8fd375dc8f79cefff02f574f8e516
7
+ data.tar.gz: 377cef9823245bd5153bc291e6fb078c723544986e6fd85a79e50b64d8cc5110c1331ac8247a40b158022633672ad93e3a85719d2b70f4659e152913f0b4b729
data/.travis.yml CHANGED
@@ -30,4 +30,7 @@ before_script:
30
30
  - psql --version
31
31
  - psql -c 'CREATE DATABASE travis_test;' -U postgres
32
32
  - psql -t -c "SELECT 1 FROM pg_roles WHERE rolname='travis'" -U postgres | grep 1 || psql -c 'CREATE ROLE travis SUPERUSER LOGIN CREATEDB;' -U postgres
33
- - psql -f db/schema.sql -v ON_ERROR_STOP=1 travis_test
33
+ - psql -f db/schema.sql -v ON_ERROR_STOP=1 travis_test
34
+ - psql -c 'CREATE DATABASE travis_test_destination;' -U postgres
35
+ - psql -t -c "SELECT 1 FROM pg_roles WHERE rolname='travis'" -U postgres | grep 1 || psql -c 'CREATE ROLE travis SUPERUSER LOGIN CREATEDB;' -U postgres
36
+ - psql -f db/schema.sql -v ON_ERROR_STOP=1 travis_test_destination
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- travis-backup (0.0.1)
4
+ travis-backup (0.0.3)
5
5
  activerecord
6
6
  bootsnap
7
7
  pg
data/config/database.yml CHANGED
@@ -18,3 +18,6 @@ test:
18
18
  <<: *default
19
19
  url: 'postgresql://localhost/travis_test'
20
20
  eager_load: false
21
+ destination:
22
+ url: 'postgresql://localhost/travis_test_destination'
23
+ eager_load: false
data/lib/config.rb CHANGED
@@ -2,7 +2,17 @@
2
2
  require 'optparse'
3
3
 
4
4
  class Config
5
- attr_reader :if_backup, :dry_run, :limit, :threshold, :files_location, :database_url, :user_id, :repo_id, :org_id
5
+ attr_reader :if_backup,
6
+ :dry_run,
7
+ :limit,
8
+ :threshold,
9
+ :files_location,
10
+ :database_url,
11
+ :user_id,
12
+ :repo_id,
13
+ :org_id,
14
+ :move_logs,
15
+ :destination_db_url
6
16
 
7
17
  def initialize(args={}) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/MethodLength
8
18
  set_values(args)
@@ -43,7 +53,7 @@ class Config
43
53
  @files_location = first_not_nil(
44
54
  args[:files_location],
45
55
  argv_opts[:files_location],
46
- ENV['BACKUP_BACKUP_FILES_LOCATION'],
56
+ ENV['BACKUP_FILES_LOCATION'],
47
57
  config.dig('backup', 'files_location'),
48
58
  './dump'
49
59
  )
@@ -71,10 +81,23 @@ class Config
71
81
  ENV['ORG_ID'],
72
82
  config.dig('backup', 'org_id')
73
83
  )
84
+ @move_logs = first_not_nil(
85
+ args[:move_logs],
86
+ argv_opts[:move_logs],
87
+ ENV['BACKUP_MOVE_LOGS'],
88
+ config.dig('backup', 'move_logs'),
89
+ false
90
+ )
91
+ @destination_db_url = first_not_nil(
92
+ args[:destination_db_url],
93
+ argv_opts[:destination_db_url],
94
+ ENV['BACKUP_DESTINATION_DB_URL'],
95
+ connection_details.dig(ENV['RAILS_ENV'], 'destination')
96
+ )
74
97
  end
75
98
 
76
99
  def check_values
77
- if !@threshold
100
+ if !@move_logs && !@threshold
78
101
  message = abort_message("Please provide the threshold argument. Data younger than it will be omitted. " +
79
102
  "Threshold defines number of months from now.")
80
103
  abort message
@@ -84,6 +107,14 @@ class Config
84
107
  message = abort_message("Please provide proper database URL.")
85
108
  abort message
86
109
  end
110
+
111
+ if (@move_logs && !@destination_db_url)
112
+ abort "\nFor moving logs you need to specify your destination database. Example usage:\n" +
113
+ "\n $ bin/travis_backup 'postgres://source_url' --move_logs --destination_db_url 'postgres://destination_url'\n" +
114
+ "\nor using in code:\n" +
115
+ "\n Backup.new(database_url: 'postgres://source_url', destination_db_url: 'postgres://destination_url', move_logs: true)\n" +
116
+ "\nYou can also set it using environment variables or configuration files.\n"
117
+ end
87
118
  end
88
119
 
89
120
  def abort_message(intro)
@@ -91,7 +122,7 @@ class Config
91
122
  "\n $ bin/travis_backup 'postgres://my_database_url' --threshold 6\n" +
92
123
  "\nor using in code:\n" +
93
124
  "\n Backup.new(database_url: 'postgres://my_database_url', threshold: 6)\n" +
94
- "\nYou can also set it using environment variables or config/database.yml file.\n"
125
+ "\nYou can also set it using environment variables or configuration files.\n"
95
126
  end
96
127
 
97
128
  def argv_options
@@ -106,6 +137,8 @@ class Config
106
137
  opt.on('-u', '--user_id X') { |o| options[:user_id] = o.to_i }
107
138
  opt.on('-r', '--repo_id X') { |o| options[:repo_id] = o.to_i }
108
139
  opt.on('-o', '--org_id X') { |o| options[:org_id] = o.to_i }
140
+ opt.on('--move_logs') { |o| options[:move_logs] = o }
141
+ opt.on('--destination_db_url X') { |o| options[:destination_db_url] = o }
109
142
  end.parse!
110
143
 
111
144
  options[:database_url] = ARGV.shift if ARGV[0]
data/lib/models/build.rb CHANGED
@@ -7,7 +7,7 @@ require 'models/repository'
7
7
  # Build model
8
8
  class Build < Model
9
9
  belongs_to :repository
10
- has_many :jobs, -> { order('id') }, foreign_key: :source_id, dependent: :delete_all, class_name: 'Job'
10
+ has_many :jobs, -> { order('id') }, foreign_key: :source_id, dependent: :destroy, class_name: 'Job'
11
11
 
12
12
  self.table_name = 'builds'
13
13
  end
data/lib/models/job.rb CHANGED
@@ -2,12 +2,14 @@
2
2
 
3
3
  require 'models/model'
4
4
  require 'models/repository'
5
+ require 'models/log'
5
6
 
6
7
  # Job model
7
8
  class Job < Model
8
9
  self.inheritance_column = :_type_disabled
9
10
 
10
11
  belongs_to :repository
12
+ has_many :logs, -> { order('id') }, foreign_key: :job_id, dependent: :destroy, class_name: 'Log'
11
13
 
12
14
  self.table_name = 'jobs'
13
15
  end
data/lib/models/log.rb ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'models/model'
4
+
5
+ class Log < Model
6
+ belongs_to :job
7
+
8
+ self.table_name = 'logs'
9
+ end
data/lib/travis-backup.rb CHANGED
@@ -4,24 +4,30 @@ require 'active_support/core_ext/array'
4
4
  require 'active_support/time'
5
5
  require 'config'
6
6
  require 'models/repository'
7
+ require 'models/log'
7
8
 
8
9
  # main travis-backup class
9
10
  class Backup
11
+ attr_accessor :config
12
+ attr_reader :dry_run_removed
13
+
10
14
  def initialize(config_args={})
11
15
  @config = Config.new(config_args)
12
16
 
13
17
  if @config.dry_run
14
- @dry_run_removed = {builds: [], jobs: []}
18
+ @dry_run_removed = {builds: [], jobs: [], logs: []}
15
19
  end
16
20
 
17
21
  connect_db
18
22
  end
19
23
 
20
- def connect_db
21
- ActiveRecord::Base.establish_connection(@config.database_url)
24
+ def connect_db(url=@config.database_url)
25
+ ActiveRecord::Base.establish_connection(url)
22
26
  end
23
27
 
24
28
  def run(args={})
29
+ return move_logs if @config.move_logs
30
+
25
31
  user_id = args[:user_id] || @config.user_id
26
32
  repo_id = args[:repo_id] || @config.repo_id
27
33
  org_id = args[:org_id] || @config.org_id
@@ -53,6 +59,24 @@ class Backup
53
59
  puts 'Dry run active. The following data would be removed in normal mode:'
54
60
  puts " - builds: #{@dry_run_removed[:builds].to_json}"
55
61
  puts " - jobs: #{@dry_run_removed[:jobs].to_json}"
62
+ puts " - logs: #{@dry_run_removed[:logs].to_json}"
63
+ end
64
+ end
65
+
66
+ def move_logs
67
+ connect_db(@config.database_url)
68
+ Log.order(:id).in_groups_of(@config.limit.to_i, false).map do |logs_batch|
69
+ log_hashes = logs_batch.as_json
70
+ connect_db(@config.destination_db_url)
71
+
72
+ log_hashes.each do |log_hash|
73
+ new_log = Log.new(log_hash)
74
+ new_log.save!
75
+ end
76
+
77
+ connect_db(@config.database_url)
78
+
79
+ logs_batch.each(&:destroy)
56
80
  end
57
81
  end
58
82
 
@@ -85,10 +109,20 @@ class Backup
85
109
 
86
110
  def destroy_batch_dry(builds_batch)
87
111
  @dry_run_removed[:builds].concat(builds_batch.map(&:id))
112
+
88
113
  jobs = builds_batch.map do |build|
89
114
  build.jobs.map(&:id) || []
90
115
  end.flatten
116
+
91
117
  @dry_run_removed[:jobs].concat(jobs)
118
+
119
+ logs = builds_batch.map do |build|
120
+ build.jobs.map do |job|
121
+ job.logs.map(&:id) || []
122
+ end.flatten || []
123
+ end.flatten
124
+
125
+ @dry_run_removed[:logs].concat(logs)
92
126
  end
93
127
 
94
128
  def save_file(file_name, content) # rubocop:disable Metrics/MethodLength
@@ -127,8 +161,17 @@ class Backup
127
161
  def export_jobs(jobs)
128
162
  jobs.map do |job|
129
163
  job_export = job.attributes
164
+ job_export[:logs] = export_logs(job.logs)
130
165
 
131
166
  job_export
132
167
  end
133
168
  end
169
+
170
+ def export_logs(logs)
171
+ logs.map do |log|
172
+ log_export = log.attributes
173
+
174
+ log_export
175
+ end
176
+ end
134
177
  end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'travis-backup'
3
- s.version = '0.0.2'
3
+ s.version = '0.0.3'
4
4
  s.summary = 'Travis CI backup tool'
5
5
  s.authors = ['Karol Selak']
6
6
  s.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: travis-backup
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Karol Selak
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-08-24 00:00:00.000000000 Z
11
+ date: 2021-09-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -250,6 +250,7 @@ files:
250
250
  - lib/config.rb
251
251
  - lib/models/build.rb
252
252
  - lib/models/job.rb
253
+ - lib/models/log.rb
253
254
  - lib/models/model.rb
254
255
  - lib/models/organization.rb
255
256
  - lib/models/repository.rb