Class: DBPurger::DynamicPlanBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/db-purger/dynamic_plan_builder.rb

Overview

DBPurger::DynamicPlanBuilder generates a purge plan based on the database relations

Constant Summary collapse

INDENT =
'  '

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(database) ⇒ DynamicPlanBuilder

Returns a new instance of DynamicPlanBuilder.



10
11
12
13
14
15
# File 'lib/db-purger/dynamic_plan_builder.rb', line 10

def initialize(database)
  @database = database
  @output = ''.dup
  @indent_depth = 0
  @tables = []
end

Instance Attribute Details

#outputObject (readonly)

Returns the value of attribute output.



8
9
10
# File 'lib/db-purger/dynamic_plan_builder.rb', line 8

def output
  @output
end

Instance Method Details

#build(base_table_name, field) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/db-purger/dynamic_plan_builder.rb', line 17

def build(base_table_name, field)
  write_table('base', base_table_name.to_s, field, [], nil)
  line_break
  model = find_model_for_table(base_table_name)
  foreign_key = foreign_key_name(model)
  add_parent_tables(base_table_name, foreign_key)
  unless (child_models = find_child_models(model, foreign_key)).empty?
    line_break
    add_child_tables(child_models, foreign_key, 0)
  end
  ignore_missing_tables
  @output
end