Class: Backup::RemoveOrphans

Inherits:
Object
  • Object
show all
Includes:
SaveIdHashToFile, SaveNullifiedRelsToFile
Defined in:
lib/backup/remove_orphans.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SaveNullifiedRelsToFile

#save_nullified_rels_to_file, #save_rels_batch_to_file

Methods included from SaveFile

#current_time_for_subfolder, #ensure_path, #folder_path, #full_file_path, #save_file

Methods included from SaveIdHashToFile

#get_exported_object, #save_id_hash_to_file, #save_ids_batch_to_file

Constructor Details

#initialize(config, dry_run_reporter = nil) ⇒ RemoveOrphans

Returns a new instance of RemoveOrphans.



14
15
16
17
18
# File 'lib/backup/remove_orphans.rb', line 14

def initialize(config, dry_run_reporter=nil)
  @config = config
  @dry_run_reporter = dry_run_reporter
  @ids_to_remove = IdHash.new
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



12
13
14
# File 'lib/backup/remove_orphans.rb', line 12

def config
  @config
end

Instance Method Details

#casesObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/backup/remove_orphans.rb', line 101

def cases
  [
    {
      main_model: Repository,
      relations: [
        {related_model: Build, fk_name: 'current_build_id'},
        {related_model: Build, fk_name: 'last_build_id'}
      ]
    }, {
      main_model: Build,
      relations: [
        {related_model: Repository, fk_name: 'repository_id'},
        {related_model: Commit, fk_name: 'commit_id'},
        {related_model: Request, fk_name: 'request_id'},
        {related_model: PullRequest, fk_name: 'pull_request_id'},
        {related_model: Branch, fk_name: 'branch_id'},
        {related_model: Tag, fk_name: 'tag_id'}
      ]
    }, {
      main_model: Job,
      relations: [
        {related_model: Repository, fk_name: 'repository_id'},
        {related_model: Commit, fk_name: 'commit_id'},
        {related_model: Stage, fk_name: 'stage_id'},
      ]
    }, {
      main_model: Branch,
      relations: [
        {related_model: Repository, fk_name: 'repository_id'},
        {related_model: Build, fk_name: 'last_build_id'}
      ]
    }, {
      main_model: Tag,
      relations: [
        {related_model: Repository, fk_name: 'repository_id'},
        {related_model: Build, fk_name: 'last_build_id'}
      ]
    }, {
      main_model: Commit,
      relations: [
        {related_model: Repository, fk_name: 'repository_id'},
        {related_model: Branch, fk_name: 'branch_id'},
        {related_model: Tag, fk_name: 'tag_id'}
      ]
    }, {
      main_model: Cron,
      relations: [
        {related_model: Branch, fk_name: 'branch_id'}
      ]
    }, {
      main_model: PullRequest,
      relations: [
        {related_model: Repository, fk_name: 'repository_id'}
      ]
    }, {
      main_model: SslKey,
      relations: [
        {related_model: Repository, fk_name: 'repository_id'}
      ]
    }, {
      main_model: Request,
      relations: [
        {related_model: Commit, fk_name: 'commit_id'},
        {related_model: PullRequest, fk_name: 'pull_request_id'},
        {related_model: Branch, fk_name: 'branch_id'},
        {related_model: Tag, fk_name: 'tag_id'}
      ]
    }, {
      main_model: Stage,
      relations: [
        {related_model: Build, fk_name: 'build_id'}
      ]
    }
  ]
end

#check_allObject



34
35
36
37
38
# File 'lib/backup/remove_orphans.rb', line 34

def check_all
  cases.each do |model_block|
    check_model_block(model_block)
  end
end

#check_model_block(model_block) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/backup/remove_orphans.rb', line 45

def check_model_block(model_block)
  model_block[:relations].each do |relation|
    check_relationship(
      main_model: model_block[:main_model],
      related_model: relation[:related_model],
      fk_name: relation[:fk_name],
    )
  end
end

#check_relationship(args) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/backup/remove_orphans.rb', line 55

def check_relationship(args)
  main_model = args[:main_model]
  related_model = args[:related_model]
  fk_name = args[:fk_name]

  main_table = main_model.table_name
  related_table = related_model.table_name

  for_delete = main_model.find_by_sql(%{
    select a.*
    from #{main_table} a
    left join #{related_table} b
    on a.#{fk_name} = b.id
    where
      a.#{fk_name} is not null
      and b.id is null;
  })

  key = main_model.name.underscore.to_sym
  ids = for_delete.map(&:id)
  @ids_to_remove.add(key, *ids)
end

#check_specified(table_name) ⇒ Object



40
41
42
43
# File 'lib/backup/remove_orphans.rb', line 40

def check_specified(table_name)
  model_block = cases.find { |c| c[:main_model] == Model.get_model_by_table_name(table_name) }
  check_model_block(model_block)
end

#dry_run_reportObject



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

def dry_run_report
  @dry_run_reporter.report
end

#nullify_builds_dependenciesObject



92
93
94
95
96
97
98
99
# File 'lib/backup/remove_orphans.rb', line 92

def nullify_builds_dependencies
  nullified = @ids_to_remove[:build]&.map do |build_id|
    build = Build.find(build_id)
    build.nullify_all_dependencies
  end

  nullified&.flatten || []
end

#process_ids_to_removeObject



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/backup/remove_orphans.rb', line 78

def process_ids_to_remove
  return @dry_run_reporter.add_to_report(@ids_to_remove.with_table_symbols) if @config.dry_run

  nullified_rels = nullify_builds_dependencies

  if @config.if_backup
    @subfolder = "remove_orphans_#{current_time_for_subfolder}"
    save_nullified_rels_to_file(build: nullified_rels)
    save_id_hash_to_file(@ids_to_remove)
  end

  @ids_to_remove.remove_entries_from_db
end

#runObject



24
25
26
27
28
29
30
31
32
# File 'lib/backup/remove_orphans.rb', line 24

def run
  if @config.orphans_table
    check_specified(@config.orphans_table)
  else
    check_all
  end

  process_ids_to_remove
end