Skip to content

Delete project events before the project

Vasilii Iakliushin requested to merge 346169_batch_delete_events into master

What does this MR do and why?

Contributes to #346169 (closed)

Problem

We delete events via cascade delete. But it leads to a higher chance of the statement timeout error for DELETE FROM project query.

Solution

Delete events before the project in a separate query to avoid a timeout.

Screenshots or screen recordings

Before

...
Project Destroy (154.9ms)  DELETE FROM "projects" WHERE "projects"."id" = 79 /*application:console,db_config_name:main,line:/app/services/projects/destroy_service.rb:136:in `destroy_project_related_records'*/
...

After

...
Event Destroy (64.6ms)  DELETE FROM "events" WHERE "events"."project_id" = 79 /*application:console,db_config_name:main,line:/app/services/events/destroy_service.rb:10:in `execute'*/
...
Project Destroy (94.5ms)  DELETE FROM "projects" WHERE "projects"."id" = 79 /*application:console,db_config_name:main,line:/app/services/projects/destroy_service.rb:149:in `destroy_project_related_records'*/

How to set up and validate locally

  1. Create a new project
  2. Generate events for this project (I used the Rails console to create them)
1_000.times { project.events.create(author: user, action: 1) }
  1. Try to delete the project
# Show SQL queries in console 
ActiveRecord::Base.logger = Logger.new(STDOUT)

# Choose project to delete and a user record
user = User.first
project = Project.last

# Execute project deletion process and roll it back (to avoid constant project recreation)
ActiveRecord::Base.transaction { Projects::DestroyService.new(project, user, {}).execute; raise ActiveRecord::Rollback; }
  1. Verify executed SQL queries. You should see DELETE FROM events before DELETE FROM projects query.

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Vasilii Iakliushin

Merge request reports