Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove configs #23

Open
wants to merge 11 commits into
base: remove_specified_in_3.0
Choose a base branch
from
Open
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
travis-backup-for-v3 (0.1.0)
travis-backup-for-v3 (0.1.1)
activerecord
bootsnap
pg
Expand Down
120 changes: 103 additions & 17 deletions lib/backup/remove_specified/remove_heavy_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,30 @@ def remove_heavy_data_for_repos_owned_by(owner_id, owner_type)
end

def remove_heavy_data_for_repo(repository)
remove_repo_builds(repository)
# remove_repo_builds(repository)
remove_repo_requests(repository)
end

def remove_repo_builds(repository) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
threshold = @config.threshold.to_i.months.ago.to_datetime
builds_to_remove = repository.builds.where('created_at < ?', threshold)
# def remove_repo_builds(repository) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
# threshold = @config.threshold.to_i.months.ago.to_datetime
# builds_to_remove = repository.builds.where('created_at < ?', threshold)

builds_dependencies = builds_to_remove.map do |build|
result = build.ids_of_all_dependencies(dependencies_to_filter, :without_parents)
result.add(:build, build.id)
result
end.compact
# builds_dependencies = builds_to_remove.map do |build|
# result = build.ids_of_all_dependencies(dependencies_to_filter, :without_parents)
# result.add(:build, build.id)
# result
# end.compact

ids_to_remove = IdHash.join(*builds_dependencies)
@subfolder = "repository_#{repository.id}_old_builds_#{current_time_for_subfolder}"
# ids_to_remove = IdHash.join(*builds_dependencies)
# @subfolder = "repository_#{repository.id}_old_builds_#{current_time_for_subfolder}"

unless @config.dry_run
nullified_rels = builds_to_remove&.map(&:nullify_default_dependencies)&.flatten
save_nullified_rels_to_file(build: nullified_rels) if @config.if_backup
end
# unless @config.dry_run
# nullified_rels = builds_to_remove&.map(&:nullify_default_dependencies)&.flatten
# save_nullified_rels_to_file(build: nullified_rels) if @config.if_backup
# end

process_ids_to_remove(ids_to_remove)
end
# process_ids_to_remove(ids_to_remove)
# end

def remove_repo_requests(repository)
threshold = @config.threshold.to_i.months.ago.to_datetime
Expand All @@ -61,11 +61,97 @@ def remove_repo_requests(repository)
end

ids_to_remove = IdHash.join(*(requests_dependencies))
orphaned_ids = orphaned_configs_to_remove(repository, ids_to_remove)
ids_to_remove.join(orphaned_ids)
process_ids_to_remove(ids_to_remove)
end

private

def find_config_ids_orphaned_now(model1:, model2:, repository:, foreign_key:, ids_to_remove:)
ids_to_exclude = ids_to_remove[model2.table_name.singularize.to_sym].join(', ')

orphaned_earlier = model1.find_by_sql(%{
select a.id
from #{model1.table_name} as a
left join #{model2.table_name} as b on
a.id = b.#{foreign_key}
where a.repository_id = #{repository.id}
group by a.id
having count(b.id) = 0;
}).map { |x| x.id }


all_orphaned = model1.find_by_sql(%{
select a.id
from #{model1.table_name} as a
left join #{model2.table_name} as b on
a.id = b.#{foreign_key} and b.id not in (#{ids_to_exclude})
where a.repository_id = #{repository.id}
group by a.id
having count(b.id) = 0;
}).map { |x| x.id }

all_orphaned - orphaned_earlier
end

def orphaned_configs_to_remove(repository, ids_to_remove)
if ids_to_remove[:request]
request_config_ids = find_config_ids_orphaned_now(
model1: RequestConfig,
model2: Request,
repository: repository,
foreign_key: 'config_id',
ids_to_remove: ids_to_remove
)
request_yaml_config_ids = find_config_ids_orphaned_now(
model1: RequestYamlConfig,
model2: Request,
repository: repository,
foreign_key: 'yaml_config_id',
ids_to_remove: ids_to_remove
)
end

if ids_to_remove[:request_raw_configuration]
request_raw_config_ids = find_config_ids_orphaned_now(
model1: RequestRawConfig,
model2: RequestRawConfiguration,
repository: repository,
foreign_key: 'request_raw_config_id',
ids_to_remove: ids_to_remove
)
end

if ids_to_remove[:build]
build_config_ids = find_config_ids_orphaned_now(
model1: BuildConfig,
model2: Build,
repository: repository,
foreign_key: 'config_id',
ids_to_remove: ids_to_remove
)
end

if ids_to_remove[:job]
job_config_ids = find_config_ids_orphaned_now(
model1: JobConfig,
model2: Job,
repository: repository,
foreign_key: 'config_id',
ids_to_remove: ids_to_remove
)
end

orphaned_ids = IdHash.new
orphaned_ids.add(:request_config, *request_config_ids)
orphaned_ids.add(:request_yaml_config, *request_yaml_config_ids)
orphaned_ids.add(:request_raw_config, *request_raw_config_ids)
orphaned_ids.add(:build_config, *build_config_ids)
orphaned_ids.add(:job_config, *job_config_ids)
orphaned_ids
end

def process_ids_to_remove(ids_to_remove)
if @config.dry_run
@dry_run_reporter.add_to_report(ids_to_remove.with_table_symbols)
Expand Down
1 change: 0 additions & 1 deletion lib/backup/save_id_hash_to_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ def save_id_hash_to_file(id_hash)

def save_ids_batch_to_file(name, ids_batch)
model = Model.get_model(name)

export = {}
export[:table_name] = model.table_name
export[:data] = ids_batch.map do |id|
Expand Down
14 changes: 3 additions & 11 deletions lib/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def set_values(args)
end

def check_values
if !@move_logs && !@remove_orphans && !@threshold && !@user_id && !@org_id && !@repo_id && !@load_from_files
if !@threshold && !@user_id && !@org_id && !@repo_id && !@load_from_files
message = abort_message("Please provide the threshold argument. Data younger than it will be omitted. " +
"Threshold defines number of months from now. Alternatively you can define user_id, org_id or repo_id " +
"to remove whole user, organization or repository with all dependencies.")
Expand All @@ -140,20 +140,12 @@ def check_values
message = abort_message("Please provide proper database URL.")
abort message
end

if (@move_logs && !@destination_db_url)
abort "\nFor moving logs you need to specify your destination database. Example usage:\n" +
"\n $ bin/travis_backup 'postgres://source_url' --move_logs --destination_db_url 'postgres://destination_url'\n" +
"\nor using in code:\n" +
"\n Backup.new(database_url: 'postgres://source_url', destination_db_url: 'postgres://destination_url', move_logs: true)\n" +
"\nYou can also set it using environment variables or configuration files.\n"
end
end

def abort_message(intro)
"\n#{intro}\n\nExample usage:\n"+
"\n $ bin/travis_backup 'postgres://my_database_url' --threshold 6" +
"\n $ bin/travis_backup 'postgres://my_database_url' --user_id 1\n" +
"\n $ travis_backup_for_v3 'postgres://my_database_url' --threshold 6" +
"\n $ travis_backup_for_v3 'postgres://my_database_url' --user_id 1\n" +
"\nor using in code:\n" +
"\n Backup.new(database_url: 'postgres://my_database_url', threshold: 6)" +
"\n Backup.new(database_url: 'postgres://my_database_url', user_id: 1)\n" +
Expand Down
6 changes: 5 additions & 1 deletion lib/db_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@ def do_in_other_db(config_or_url)
result
end

def do_without_triggers
def self.do_without_triggers
ActiveRecord::Base.connection.execute('set session_replication_role = replica;')
result = yield
ActiveRecord::Base.connection.execute('set session_replication_role = default;')
result
end

def do_without_triggers(&block)
self.class.do_without_triggers(&block)
end
end
2 changes: 2 additions & 0 deletions lib/models/abuse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require 'model'

class Abuse < Model
self.inheritance_column = :_type_disabled

belongs_to :owner, polymorphic: true
belongs_to :request
end
2 changes: 2 additions & 0 deletions lib/models/beta_migration_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require 'model'

class BetaMigrationRequest < Model
self.inheritance_column = :_type_disabled

belongs_to :owner, polymorphic: true
has_many :organizations
end
2 changes: 2 additions & 0 deletions lib/models/branch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require 'model'

class Branch < Model
self.inheritance_column = :_type_disabled

belongs_to :last_build, foreign_key: :last_build_id, class_name: 'Build'
belongs_to :repository
has_many :builds
Expand Down
2 changes: 2 additions & 0 deletions lib/models/broadcast.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
require 'model'

class Broadcast < Model
self.inheritance_column = :_type_disabled

belongs_to :recipient, polymorphic: true
end
2 changes: 2 additions & 0 deletions lib/models/build.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require 'model'

class Build < Model
self.inheritance_column = :_type_disabled

belongs_to :repository
belongs_to :owner, polymorphic: true
belongs_to :sender, polymorphic: true
Expand Down
2 changes: 2 additions & 0 deletions lib/models/build_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require 'model'

class BuildConfig < Model
self.inheritance_column = :_type_disabled

belongs_to :repository
has_many :builds, foreign_key: :config_id, class_name: 'Build'
has_many :deleted_builds, foreign_key: :config_id, class_name: 'DeletedBuild'
Expand Down
2 changes: 2 additions & 0 deletions lib/models/cancellation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require 'model'

class Cancellation < Model
self.inheritance_column = :_type_disabled

belongs_to :subscription
belongs_to :user
end
2 changes: 2 additions & 0 deletions lib/models/commit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require 'model'

class Commit < Model
self.inheritance_column = :_type_disabled

belongs_to :related_branch, foreign_key: :branch_id, class_name: 'Branch'
belongs_to :repository
belongs_to :tag
Expand Down
2 changes: 2 additions & 0 deletions lib/models/cron.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
require 'model'

class Cron < Model
self.inheritance_column = :_type_disabled

belongs_to :branch
end
2 changes: 2 additions & 0 deletions lib/models/deleted_build.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require 'model'

class DeletedBuild < Model
self.inheritance_column = :_type_disabled

belongs_to :repository
belongs_to :owner, polymorphic: true
belongs_to :sender, polymorphic: true
Expand Down
2 changes: 2 additions & 0 deletions lib/models/deleted_build_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require 'model'

class DeletedBuildConfig < Model
self.inheritance_column = :_type_disabled

belongs_to :repository
self.primary_key = 'id'
end
2 changes: 2 additions & 0 deletions lib/models/deleted_commit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require 'model'

class DeletedCommit < Model
self.inheritance_column = :_type_disabled

belongs_to :related_branch, foreign_key: :branch_id, class_name: 'Branch'
belongs_to :repository
belongs_to :tag
Expand Down
2 changes: 2 additions & 0 deletions lib/models/deleted_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require 'model'

class DeletedJob < Model
self.inheritance_column = :_type_disabled

belongs_to :source, polymorphic: true
belongs_to :owner, polymorphic: true
belongs_to :repository
Expand Down
2 changes: 2 additions & 0 deletions lib/models/deleted_job_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require 'model'

class DeletedJobConfig < Model
self.inheritance_column = :_type_disabled

belongs_to :repository
self.primary_key = 'id'
end
2 changes: 2 additions & 0 deletions lib/models/deleted_pull_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require 'model'

class DeletedPullRequest < Model
self.inheritance_column = :_type_disabled

belongs_to :repository
self.primary_key = 'id'
end
2 changes: 2 additions & 0 deletions lib/models/deleted_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require 'model'

class DeletedRequest < Model
self.inheritance_column = :_type_disabled

belongs_to :owner, polymorphic: true
belongs_to :sender, polymorphic: true
belongs_to :repository
Expand Down
2 changes: 2 additions & 0 deletions lib/models/deleted_request_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require 'model'

class DeletedRequestConfig < Model
self.inheritance_column = :_type_disabled

belongs_to :repository
self.primary_key = 'id'
end
2 changes: 2 additions & 0 deletions lib/models/deleted_request_payload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require 'model'

class DeletedRequestPayload < Model
self.inheritance_column = :_type_disabled

belongs_to :request
self.primary_key = 'id'
end
2 changes: 2 additions & 0 deletions lib/models/deleted_request_raw_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require 'model'

class DeletedRequestRawConfig < Model
self.inheritance_column = :_type_disabled

belongs_to :repository
self.primary_key = 'id'
end
2 changes: 2 additions & 0 deletions lib/models/deleted_request_raw_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require 'model'

class DeletedRequestRawConfiguration < Model
self.inheritance_column = :_type_disabled

belongs_to :request_raw_configs
belongs_to :requests
self.primary_key = 'id'
Expand Down
2 changes: 2 additions & 0 deletions lib/models/deleted_request_yaml_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require 'model'

class DeletedRequestYamlConfig < Model
self.inheritance_column = :_type_disabled

belongs_to :repository
self.primary_key = 'id'
end
Loading