Skip to content

Fix flaky tests when DB is recreated

Adam Hegyi requested to merge run-partition-manager-after-recreating-the-db into master

What does this MR do and why?

In our migration tests, we often re-create the database schema from scratch. Relevant code: https://gitlab.com/gitlab-org/gitlab/-/blob/master/spec/support/database_cleaner.rb#L21

Optimization idea: this happens quite often and each DB recreation takes 5s.

When the database is recreated, the partition manager is not executed. This means the initial partitions that are not created. This is currently done by an initializer.

Relevant code: https://gitlab.com/gitlab-org/gitlab/-/blob/master/spec/support/db_cleaner.rb#L56

How to reproduce it

Modify the DB cleaner around hook to force DB recreation:

diff --git a/spec/support/database_cleaner.rb b/spec/support/database_cleaner.rb
index 8f706fdebc9..e8db2328397 100644
--- a/spec/support/database_cleaner.rb
+++ b/spec/support/database_cleaner.rb
@@ -34,6 +34,7 @@
   end

   config.around(:each, :migration) do |example|
+    recreate_all_databases!
     self.class.use_transactional_tests = false

     example.run

Run:

bundle exec rspec spec/migrations/20220202105733_delete_service_template_records_spec.rb

The tests should fail without the fix.

MR acceptance checklist

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

Related to #352676 (closed)

Edited by Adam Hegyi

Merge request reports