Module: PruneAr

Defined in:
lib/prune_ar.rb,
lib/prune_ar/pruner.rb,
lib/prune_ar/version.rb,
lib/prune_ar/deleter_by_criteria.rb,
lib/prune_ar/foreign_key_handler.rb,
lib/prune_ar/belongs_to_association.rb,
lib/prune_ar/orphaned_selection_builder.rb,
lib/prune_ar/belongs_to_association_gatherer.rb

Overview

Namespace for all prune_ar code

Defined Under Namespace

Classes: BelongsToAssociation, BelongsToAssociationGatherer, DeleterByCriteria, ForeignKeyHandler, OrphanedSelectionBuilder, Pruner

Constant Summary collapse

VERSION =
'0.1.0'

Class Method Summary collapse

Class Method Details

.all_modelsObject



56
57
58
59
60
61
# File 'lib/prune_ar.rb', line 56

def self.all_models
  ActiveRecord::Base
    .descendants
    .reject { |c| ['ApplicationRecord'].any? { |start| c.name.start_with?(start) } }
    .uniq(&:table_name)
end

.prune_all_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 }) ⇒ Object

deletion_criteria

> The core pruning criteria that you want to execute (will be executed up front)

> {

> Account => [‘accounts.id NOT IN (1, 2)’]

> User => [“users.internal = ‘f’”, “users.active = ‘f’”]

> }

full_delete_models

> Models for which you want to purge all records

> [Model1, Model2]

pre_queries_to_run

> Arbitrary SQL statements to execute before pruning

> [ ‘UPDATE users SET invited_by_id = NULL WHERE invited_by_id IS NOT NULL’ ]

conjunctive_deletion_criteria

> Pruning criteria you want executed in conjunction with each iteration of pruning

> of orphaned records (one case where this is useful if pruning entities which

> don’t have a belongs_to chain to the entities we pruned but instead are associated

> via join tables)

> {

> Image => [‘NOT EXISTS (SELECT 1 FROM imagings WHERE imagings.image_id = images.id)’]

> }

perform_sanity_check

> Determines whether ‘PruneAr` sanity checks it’s own pruning by setting (& subsequently

> removing) foreign key constraints for all belongs_to relations. This is to prove that

> we maintained referential integrity.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/prune_ar.rb', line 37

def self.prune_all_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.new(
    models: all_models,
    deletion_criteria: deletion_criteria,
    full_delete_models: full_delete_models,
    pre_queries_to_run: pre_queries_to_run,
    conjunctive_deletion_criteria: conjunctive_deletion_criteria,
    perform_sanity_check: perform_sanity_check,
    logger: logger
  ).prune
end