Module: FastDestroyAll
- Extended by:
- ActiveSupport::Concern
- Included in:
- Ci::BuildTraceChunk, Deployment
- Defined in:
- app/models/concerns/fast_destroy_all.rb
Overview
This module is for replacing ‘dependent: :destroy` and before_destroy hooks.
In general, destroy_all is inefficient because it calls each callback with DELETE queries i.e. O(n), whereas, delete_all is efficient as it deletes all rows with a single DELETE query.
It’s better to use delete_all as our best practice, however, if external data (e.g. ObjectStorage, FileStorage or Redis) are associated with database records, it is difficult to accomplish it.
This module defines a format to use delete_all and delete associated external data. Here is an example
Situation
-
Projecthas manyCi::BuildTraceChunkthroughCi::Build -
Ci::BuildTraceChunkstores associated data in Redis,so it relies on `dependent: :destroy` and `before_destroy` for the deletion
How to use
-
Define ‘use_fast_destroy :build_trace_chunks` in
Projectmodel. -
Define
begin_fast_destroyand ‘finalize_fast_destroy(params)` inCi::BuildTraceChunkmodel. -
Use
fast_destroy_allinstead ofdestroyanddestroy_all -
Remove ‘dependent: :destroy` and
before_destroyas it’s no longer need
Expectation
-
When a project is ‘destroy`ed, the associated trace_chunks will be deleted by
delete_all, and the associated data will be removed, too. -
When
fast_destroy_allis called, it also performns as same.
Defined Under Namespace
Modules: Helpers
Constant Summary collapse
- ForbiddenActionError =
Class.new(StandardError)