Migration testing for down database migrations
What does this MR do and why?
This MR adds two new rake tasks - gitlab:db:migration_testing:up
and gitlab:db:migration_testing:down
. These will be used by the migration testing pipeline (https://gitlab.com/gitlab-org/database-team/gitlab-com-database-testing) to test down
migrations.
gitlab:db:migration_testing:up
performs the same migration testing that gitlab:db:migration_testing
currently performs, but puts the results in an /up
subdirectory.
gitlab:db:migration_testing:down
runs the down versions of migrations that were introduced in the current branch, in reverse order, and records them in the /down
subdirectory.
So that this MR can be merged without coordination with the gitlab-com-database-testing project, the old gitlab:db:migration_testing
rake task still exists. Once this is merged, we will migrate that project to call the :up
and :down
variants, then remove the old task.
To facilitate running both up and down migrations, the majority of this MR diff is extracting and refactoring the existing gitlab:db:migration_testing
rake task to be handled by a new Gitlab::Database::Migrations::Runner
class, capable of running sequences of migrations either up or down.
This is the first step in running down migrations for gitlab-org/database-team/gitlab-com-database-testing#18.
Screenshots or screen recordings
This is the file structure when running gitlab:db:migration_testing:up
then gitlab:db:migration_testing:down
:
tree tmp/migration-testing
tmp/migration-testing/
├── down
│ ├── 20210923183313_TestDownMigrations-query-details.json
│ ├── 20210923183313_TestDownMigrations.log
│ └── migration-stats.json
└── up
├── 20210923183313_TestDownMigrations-query-details.json
├── 20210923183313_TestDownMigrations.log
└── migration-stats.json
2 directories, 6 files
How to set up and validate locally
- Apply the following patch to add a migration on your current branch that we can test. Save it to a file and run
git am < file-i-saved-patch-to.patch
. Note that this creates a new commit, and that the down migration detection only works on migrations that you have already committed.
Patch
From 8bfbf5076ee7a37ac483152dc26c6927e3c857dc Mon Sep 17 00:00:00 2001
From: Simon Tomlinson <stomlinson@gitlab.com>
Date: Thu, 23 Sep 2021 13:34:52 -0500
Subject: [PATCH] temp down migration file must be in a commit
---
.../20210923183313_test_down_migrations.rb | 17 +++++++++++++++++
1 file changed, 17 insertions(+)
create mode 100644 db/migrate/20210923183313_test_down_migrations.rb
diff --git a/db/migrate/20210923183313_test_down_migrations.rb b/db/migrate/20210923183313_test_down_migrations.rb
new file mode 100644
index 00000000000..401a2f84ec5
--- /dev/null
+++ b/db/migrate/20210923183313_test_down_migrations.rb
@@ -0,0 +1,17 @@
+# frozen_string_literal: true
+
+
+class TestDownMigrations < Gitlab::Database::Migration[1.0]
+
+ def up
+ (1..5).each do |i|
+ execute("SELECT #{i}, 'up'")
+ end
+ end
+
+ def down
+ (1..5).each do |i|
+ execute("SELECT #{i}, 'down'")
+ end
+ end
+end
--
2.32.0
- Run
bundle exec rake gitlab:db:migration_testing:up
and see the output intmp/migration-testing/up
- Run
bundle exec rake gitlab:db:migration_testing:down
and see the output intmp/migration_testing/down
- Run
rm -r tmp/migration-testing
to clean the directory before running the old rake task - Run
bundle exec rake gitlab:db:migration_testing
and see that the migration'sup
was run again, and saved intmp/migration-testing
.
Numbered steps to set up and validate the change are strongly suggested.
MR acceptance checklist
This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.
-
I have evaluated the MR acceptance checklist for this MR.