Class: PruneAr::Pruner

Inherits:
Object
  • Object
show all
Defined in:
lib/prune_ar/pruner.rb

Overview

Core of this gem. Prunes records based on parameters given.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(models:, deletion_criteria: {}, full_delete_models: [], pre_queries_to_run: [], conjunctive_deletion_criteria: {}, perform_sanity_check: true, logger: Logger.new(STDOUT).tap { |l| l.level = Logger::WARN }) ⇒ Pruner

Returns a new instance of Pruner.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/prune_ar/pruner.rb', line 22

def initialize(
  models:,
  deletion_criteria: {},
  full_delete_models: [],
  pre_queries_to_run: [],
  conjunctive_deletion_criteria: {},
  perform_sanity_check: true,
  logger: Logger.new(STDOUT).tap { |l| l.level = Logger::WARN }
)
  @associations = BelongsToAssociationGatherer.new(models, connection: connection).associations
  @full_delete_models = full_delete_models
  @pre_queries_to_run = pre_queries_to_run
  @deletion_criteria = deletion_criteria
  @conjunctive_deletion_criteria = conjunctive_deletion_criteria
  @logger = logger
  @perform_sanity_check = perform_sanity_check
  @foreign_key_handler = ForeignKeyHandler.new(
    models: models,
    connection: connection,
    logger: logger
  )
end

Instance Attribute Details

#associationsObject (readonly)

Returns the value of attribute associations.



13
14
15
# File 'lib/prune_ar/pruner.rb', line 13

def associations
  @associations
end

#conjunctive_deletion_criteriaObject (readonly)

Returns the value of attribute conjunctive_deletion_criteria.



13
14
15
# File 'lib/prune_ar/pruner.rb', line 13

def conjunctive_deletion_criteria
  @conjunctive_deletion_criteria
end

#deletion_criteriaObject (readonly)

Returns the value of attribute deletion_criteria.



13
14
15
# File 'lib/prune_ar/pruner.rb', line 13

def deletion_criteria
  @deletion_criteria
end

#foreign_key_handlerObject (readonly)

Returns the value of attribute foreign_key_handler.



13
14
15
# File 'lib/prune_ar/pruner.rb', line 13

def foreign_key_handler
  @foreign_key_handler
end

#full_delete_modelsObject (readonly)

Returns the value of attribute full_delete_models.



13
14
15
# File 'lib/prune_ar/pruner.rb', line 13

def full_delete_models
  @full_delete_models
end

#loggerObject (readonly)

Returns the value of attribute logger.



13
14
15
# File 'lib/prune_ar/pruner.rb', line 13

def logger
  @logger
end

#perform_sanity_checkObject (readonly)

Returns the value of attribute perform_sanity_check.



13
14
15
# File 'lib/prune_ar/pruner.rb', line 13

def perform_sanity_check
  @perform_sanity_check
end

#pre_queries_to_runObject (readonly)

Returns the value of attribute pre_queries_to_run.



13
14
15
# File 'lib/prune_ar/pruner.rb', line 13

def pre_queries_to_run
  @pre_queries_to_run
end

Instance Method Details

#pruneObject



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/prune_ar/pruner.rb', line 45

def prune
  # Can't wrap in transaction if DDL is not supported in transactions. We use ALTER TABLE
  # => statements (which are DDL)
  return prune_core unless connection.supports_ddl_transactions?

  # This transaction is helpful when developing/working on this code. If a SQL statement errors,
  # => it leaves the database untouched.
  connection.transaction do
    prune_core
  end
end