diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-02-04 12:09:30 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-02-04 12:09:30 +0300 |
commit | 72241c5e0a5ced1416fcf69ed1a6d8f57b9bf3e2 (patch) | |
tree | d5369f41d14dcc3480c8eb3ac525ee907d8db7a6 /db/migrate | |
parent | a0806c73204d0fe3c3986b5c8140c582283f83c8 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'db/migrate')
-rw-r--r-- | db/migrate/20210127143025_add_oldest_merge_requests_index.rb | 23 | ||||
-rw-r--r-- | db/migrate/20210201140434_add_oldest_merge_requests_index_again.rb | 59 |
2 files changed, 61 insertions, 21 deletions
diff --git a/db/migrate/20210127143025_add_oldest_merge_requests_index.rb b/db/migrate/20210127143025_add_oldest_merge_requests_index.rb index acd690a5cce..c25e12d89af 100644 --- a/db/migrate/20210127143025_add_oldest_merge_requests_index.rb +++ b/db/migrate/20210127143025_add_oldest_merge_requests_index.rb @@ -1,32 +1,13 @@ # frozen_string_literal: true class AddOldestMergeRequestsIndex < ActiveRecord::Migration[6.0] - include Gitlab::Database::SchemaHelpers - include Gitlab::Database::MigrationHelpers - - disable_ddl_transaction! - - # Set this constant to true if this migration requires downtime. DOWNTIME = false - INDEX = 'index_on_merge_requests_for_latest_diffs' - def up - return if index_exists_by_name?('merge_requests', INDEX) - - execute "CREATE INDEX CONCURRENTLY #{INDEX} ON merge_requests " \ - 'USING btree (target_project_id) INCLUDE (id, latest_merge_request_diff_id)' - - create_comment( - 'INDEX', - INDEX, - 'Index used to efficiently obtain the oldest merge request for a commit SHA' - ) + # replaced by db/migrate/20210201140434_add_oldest_merge_requests_index_again.rb end def down - return unless index_exists_by_name?('merge_requests', INDEX) - - execute "DROP INDEX CONCURRENTLY #{INDEX}" + # replaced by db/migrate/20210201140434_add_oldest_merge_requests_index_again.rb end end diff --git a/db/migrate/20210201140434_add_oldest_merge_requests_index_again.rb b/db/migrate/20210201140434_add_oldest_merge_requests_index_again.rb new file mode 100644 index 00000000000..a3fed9e576a --- /dev/null +++ b/db/migrate/20210201140434_add_oldest_merge_requests_index_again.rb @@ -0,0 +1,59 @@ +# frozen_string_literal: true + +class AddOldestMergeRequestsIndexAgain < ActiveRecord::Migration[6.0] + include Gitlab::Database::SchemaHelpers + include Gitlab::Database::MigrationHelpers + + disable_ddl_transaction! + + DOWNTIME = false + + INDEX = 'index_on_merge_requests_for_latest_diffs' + + def up + execute "DROP INDEX CONCURRENTLY #{INDEX}" if invalid_index? + + return if index_exists_by_name?('merge_requests', INDEX) + + begin + disable_statement_timeout do + execute "CREATE INDEX CONCURRENTLY #{INDEX} ON merge_requests " \ + 'USING btree (target_project_id) INCLUDE (id, latest_merge_request_diff_id)' + end + rescue ActiveRecord::StatementInvalid => ex + # Due to https://github.com/lfittl/pg_query/issues/184, if the CREATE + # INDEX statement fails, we trigger a separate error due to the Gem not + # supporting the INCLUDE syntax. + # + # To work around this, we raise a custom error instead, as these won't + # have a query context injected. + raise "The index #{INDEX} couldn't be added: #{ex.message}" + end + + create_comment( + 'INDEX', + INDEX, + 'Index used to efficiently obtain the oldest merge request for a commit SHA' + ) + end + + def down + return unless index_exists_by_name?('merge_requests', INDEX) + + disable_statement_timeout do + execute "DROP INDEX CONCURRENTLY #{INDEX}" + end + end + + def invalid_index? + result = execute(<<~SQL) + SELECT pg_class.relname + FROM pg_class, pg_index + WHERE pg_index.indisvalid = false + AND pg_index.indexrelid = pg_class.oid + AND pg_class.relname = '#{INDEX}'; + SQL + + result.values.any? + end +end |