db-purger 0.1.3 → 0.2.0

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: 47c73f3315ea20145d1aa9b443fa2b53082170c8dea927fb6635d1e921afe8eb
4
- data.tar.gz: 0ce2fbd7a325b6399dfce5bcc3767a0e01fb1775984ce2e33350f72353228c62
3
+ metadata.gz: f85bace783d6c2c4bf0526a608326a9d15ee3dc0babd439a2dd86c22ab304343
4
+ data.tar.gz: fa1f889ecf967b73357d2764764e01a3ba7a52a9dea5737b7008e9c87edf953b
5
5
  SHA512:
6
- metadata.gz: 2e6e74e4de36039bab64bf9dff3f5887f032ab7f3f045ecfcf18b1a62e8c891e28366ef9378f8f508512b5913ac3244ea3b2ce12404c60df41b491dbcdd482a6
7
- data.tar.gz: b9480ec3c2c581161904cc569eba5ee3fb046385bacddc7b7be0311b698c316018085cf29c02b351a7fa0cfdec33488409da747a2395e200a70c73be803ec5e4
6
+ metadata.gz: cc7a13db98536483613543736ed8a0bd03dc5dd1076060a4b1735e7b5499634d1e80e955cd714ce67785dcf42b0841b47ebefbaecae9143b4366c7d27be80786
7
+ data.tar.gz: bf6d5fa8f36eaf89087056673d1e49bfa74d4141321c078c5a0b0414fe58b8d64f1409b80838a2d05a7ae163cf11f0cf345dd2d812020a8adb4a1e5020ed1202
@@ -0,0 +1,98 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DBPurger
4
+ # DBPurger::DynamicPlanBuilder generates a purge plan based on the database relations
5
+ class DynamicPlanBuilder
6
+ INDENT = ' '
7
+
8
+ attr_reader :output
9
+
10
+ def initialize(database)
11
+ @database = database
12
+ @output = ''.dup
13
+ @indent_depth = 0
14
+ write('DBPurger::PlanBuilder.build do')
15
+ @indent_depth += 1
16
+ end
17
+
18
+ def build(base_table_name, field)
19
+ write("base_table(#{base_table_name.to_sym.inspect}, #{field.to_sym.inspect})")
20
+ line_break
21
+ model = find_model_for_table(base_table_name)
22
+ foreign_key = foreign_key_name(model)
23
+ add_parent_tables(base_table_name, foreign_key)
24
+ unless (child_models = find_child_models(model, foreign_key)).empty?
25
+ line_break
26
+ add_child_tables(child_models, foreign_key, 0)
27
+ end
28
+ @indent_depth -= 1
29
+ write('end')
30
+ @output
31
+ end
32
+
33
+ private
34
+
35
+ def find_model_for_table(base_table_name)
36
+ @database.models.detect { |m| m.table_name == base_table_name.to_s }
37
+ end
38
+
39
+ def write(str)
40
+ @output << (INDENT * @indent_depth) + str + "\n"
41
+ end
42
+
43
+ def line_break
44
+ @output << "\n"
45
+ end
46
+
47
+ def add_parent_tables(base_table_name, field)
48
+ @database.models.each do |model|
49
+ next if model.table_name == base_table_name.to_s
50
+ next unless column?(model, field)
51
+
52
+ foreign_key = foreign_key_name(model)
53
+ write_table('parent', model.table_name, field, find_child_models(model, foreign_key), foreign_key)
54
+ end
55
+ end
56
+
57
+ def add_child_tables(child_models, field, change_indent_by = 1)
58
+ @indent_depth += change_indent_by
59
+ child_models.each do |model|
60
+ add_child_table(model, field)
61
+ end
62
+ @indent_depth -= change_indent_by
63
+ end
64
+
65
+ def add_child_table(model, field)
66
+ foreign_key = foreign_key_name(model)
67
+ write_table('child', model.table_name, field, find_child_models(model, foreign_key), foreign_key)
68
+ end
69
+
70
+ def find_child_models(model, field)
71
+ model_has_many_associations(model).map(&:klass).select { |m| column?(m, field) }
72
+ end
73
+
74
+ def model_has_many_associations(model)
75
+ model.reflect_on_all_associations.select do |assoc|
76
+ assoc.is_a?(ActiveRecord::Reflection::HasManyReflection)
77
+ end
78
+ end
79
+
80
+ def foreign_key_name(model)
81
+ model.table_name.singularize + '_id'
82
+ end
83
+
84
+ def column?(model, field)
85
+ model.columns.detect { |c| c.name == field.to_s } != nil
86
+ end
87
+
88
+ def write_table(table_type, table_name, field, child_models, foreign_key)
89
+ if child_models.empty?
90
+ write("#{table_type}_table(#{table_name.to_sym.inspect}, #{field.to_sym.inspect})")
91
+ else
92
+ write("#{table_type}_table(#{table_name.to_sym.inspect}, #{field.to_sym.inspect}) do")
93
+ add_child_tables(child_models, foreign_key)
94
+ write('end')
95
+ end
96
+ end
97
+ end
98
+ end
data/lib/db-purger.rb CHANGED
@@ -3,6 +3,7 @@
3
3
  # DBPurger is a tool to delete data from tables based on a initial purge value
4
4
  module DBPurger
5
5
  autoload :Config, 'db-purger/config'
6
+ autoload :DynamicPlanBuilder, 'db-purger/dynamic_plan_builder'
6
7
  autoload :Metrics, 'db-purger/metrics'
7
8
  autoload :MetricSubscriber, 'db-purger/metric_subscriber'
8
9
  autoload :PurgeTable, 'db-purger/purge_table'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: db-purger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Doug Youch
@@ -46,6 +46,7 @@ extra_rdoc_files: []
46
46
  files:
47
47
  - lib/db-purger.rb
48
48
  - lib/db-purger/config.rb
49
+ - lib/db-purger/dynamic_plan_builder.rb
49
50
  - lib/db-purger/metric_subscriber.rb
50
51
  - lib/db-purger/metrics.rb
51
52
  - lib/db-purger/plan.rb