2018-08-03 13:22:24 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-05-01 04:06:44 -04:00
|
|
|
##
|
|
|
|
# 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.
|
|
|
|
#
|
2018-05-03 04:08:05 -04:00
|
|
|
# It's better to use `delete_all` as our best practice, however,
|
2018-10-30 06:53:01 -04:00
|
|
|
# if external data (e.g. ObjectStorage, FileStorage or Redis) are associated with database records,
|
2018-05-03 04:08:05 -04:00
|
|
|
# it is difficult to accomplish it.
|
2018-05-01 04:06:44 -04:00
|
|
|
#
|
2018-05-03 04:08:05 -04:00
|
|
|
# This module defines a format to use `delete_all` and delete associated external data.
|
2019-02-25 05:42:31 -05:00
|
|
|
# Here is an example
|
2018-05-03 04:08:05 -04:00
|
|
|
#
|
|
|
|
# Situation
|
|
|
|
# - `Project` has many `Ci::BuildTraceChunk` through `Ci::Build`
|
|
|
|
# - `Ci::BuildTraceChunk` stores 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 `Project` model.
|
|
|
|
# - Define `begin_fast_destroy` and `finalize_fast_destroy(params)` in `Ci::BuildTraceChunk` model.
|
|
|
|
# - Use `fast_destroy_all` instead of `destroy` and `destroy_all`
|
|
|
|
# - Remove `dependent: :destroy` and `before_destroy` as 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_all` is called, it also performns as same.
|
2018-05-01 04:06:44 -04:00
|
|
|
module FastDestroyAll
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
2018-05-02 02:55:55 -04:00
|
|
|
ForbiddenActionError = Class.new(StandardError)
|
|
|
|
|
2018-05-01 04:06:44 -04:00
|
|
|
included do
|
2018-05-02 02:55:55 -04:00
|
|
|
before_destroy do
|
2018-08-16 08:46:40 -04:00
|
|
|
raise ForbiddenActionError, '`destroy` and `destroy_all` are forbidden. Please use `fast_destroy_all`'
|
2018-05-02 08:30:39 -04:00
|
|
|
end
|
2018-05-01 04:06:44 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class_methods do
|
|
|
|
##
|
2018-05-03 04:08:05 -04:00
|
|
|
# This method delete rows and associated external data efficiently
|
2018-05-01 04:06:44 -04:00
|
|
|
#
|
2018-05-03 07:12:25 -04:00
|
|
|
# This method can replace `destroy` and `destroy_all` without having `after_destroy` hook
|
2018-05-03 04:08:05 -04:00
|
|
|
def fast_destroy_all
|
|
|
|
params = begin_fast_destroy
|
|
|
|
|
|
|
|
delete_all
|
|
|
|
|
|
|
|
finalize_fast_destroy(params)
|
2018-05-01 04:06:44 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
2018-05-03 04:08:05 -04:00
|
|
|
# This method returns identifiers to delete associated external data (e.g. file paths, redis keys)
|
2018-05-01 04:06:44 -04:00
|
|
|
#
|
2018-05-03 04:08:05 -04:00
|
|
|
# This method must be defined in fast destroyable model
|
|
|
|
def begin_fast_destroy
|
|
|
|
raise NotImplementedError
|
2018-05-01 04:06:44 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
2018-05-03 04:08:05 -04:00
|
|
|
# This method deletes associated external data with the identifiers returned by `begin_fast_destroy`
|
|
|
|
#
|
|
|
|
# This method must be defined in fast destroyable model
|
|
|
|
def finalize_fast_destroy(params)
|
|
|
|
raise NotImplementedError
|
2018-05-01 04:06:44 -04:00
|
|
|
end
|
|
|
|
end
|
2018-05-02 02:55:55 -04:00
|
|
|
|
|
|
|
module Helpers
|
|
|
|
extend ActiveSupport::Concern
|
2018-10-07 14:31:08 -04:00
|
|
|
include AfterCommitQueue
|
2018-05-02 02:55:55 -04:00
|
|
|
|
|
|
|
class_methods do
|
|
|
|
##
|
2018-05-03 04:08:05 -04:00
|
|
|
# This method is to be defined on models which have fast destroyable models as children,
|
|
|
|
# and let us avoid to use `dependent: :destroy` hook
|
2018-12-05 09:01:12 -05:00
|
|
|
def use_fast_destroy(relation, opts = {})
|
|
|
|
set_callback :destroy, :before, opts.merge(prepend: true) do
|
2018-05-03 04:08:05 -04:00
|
|
|
perform_fast_destroy(public_send(relation)) # rubocop:disable GitlabSecurity/PublicSend
|
2018-05-02 02:55:55 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-05-03 04:08:05 -04:00
|
|
|
|
|
|
|
def perform_fast_destroy(subject)
|
|
|
|
params = subject.begin_fast_destroy
|
|
|
|
|
|
|
|
run_after_commit do
|
|
|
|
subject.finalize_fast_destroy(params)
|
|
|
|
end
|
|
|
|
end
|
2018-05-02 02:55:55 -04:00
|
|
|
end
|
2018-05-01 04:06:44 -04:00
|
|
|
end
|