Class: OnlineMigrations::BackgroundMigrations::DeleteOrphanedRecords

Inherits:
OnlineMigrations::BackgroundMigration show all
Defined in:
lib/online_migrations/background_migrations/delete_orphaned_records.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from OnlineMigrations::BackgroundMigration

named

Constructor Details

#initialize(model_name, associations, _options = {}) ⇒ DeleteOrphanedRecords

Returns a new instance of DeleteOrphanedRecords.



9
10
11
12
# File 'lib/online_migrations/background_migrations/delete_orphaned_records.rb', line 9

def initialize(model_name, associations, _options = {})
  @model = Object.const_get(model_name, false)
  @associations = associations.map(&:to_sym)
end

Instance Attribute Details

#associationsObject (readonly)

Returns the value of attribute associations.



7
8
9
# File 'lib/online_migrations/background_migrations/delete_orphaned_records.rb', line 7

def associations
  @associations
end

#modelObject (readonly)

Returns the value of attribute model.



7
8
9
# File 'lib/online_migrations/background_migrations/delete_orphaned_records.rb', line 7

def model
  @model
end

Instance Method Details

#countObject



22
23
24
# File 'lib/online_migrations/background_migrations/delete_orphaned_records.rb', line 22

def count
  Utils.estimated_count(model.connection, model.table_name)
end

#process_batch(relation) ⇒ Object



18
19
20
# File 'lib/online_migrations/background_migrations/delete_orphaned_records.rb', line 18

def process_batch(relation)
  relation.delete_all
end

#relationObject



14
15
16
# File 'lib/online_migrations/background_migrations/delete_orphaned_records.rb', line 14

def relation
  model.unscoped.where.missing(*associations)
end