diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2023-03-28 03:11:35 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2023-03-28 03:11:35 +0300 |
commit | 9968d394403ad6601fe8fdf24072fdb1ec08e1a3 (patch) | |
tree | 166816d8e27a067d55203641da6d4bc8a693c549 /lib/gitlab/database | |
parent | ff2a881e2038a523c71f0d3f044c496bb7617fd5 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'lib/gitlab/database')
-rw-r--r-- | lib/gitlab/database/async_indexes/migration_helpers.rb | 29 |
1 files changed, 27 insertions, 2 deletions
diff --git a/lib/gitlab/database/async_indexes/migration_helpers.rb b/lib/gitlab/database/async_indexes/migration_helpers.rb index d14b6be3844..d7128a20a0b 100644 --- a/lib/gitlab/database/async_indexes/migration_helpers.rb +++ b/lib/gitlab/database/async_indexes/migration_helpers.rb @@ -77,11 +77,13 @@ module Gitlab async_index end - def prepare_async_index_from_sql(table_name, index_name, definition) + def prepare_async_index_from_sql(definition) Gitlab::Database::QueryAnalyzers::RestrictAllowedSchemas.require_ddl_mode! return unless async_index_creation_available? + table_name, index_name = extract_table_and_index_names_from_concurrent_index!(definition) + if index_name_exists?(table_name, index_name) Gitlab::AppLogger.warn( message: 'Index not prepared because it already exists', @@ -137,7 +139,30 @@ module Gitlab end def async_index_creation_available? - connection.table_exists?(:postgres_async_indexes) + table_exists?(:postgres_async_indexes) + end + + private + + delegate :table_exists?, to: :connection, private: true + + def extract_table_and_index_names_from_concurrent_index!(definition) + statement = index_statement_from!(definition) + + raise 'Index statement not found!' unless statement + raise 'Index must be created concurrently!' unless statement.concurrent + raise 'Table does not exist!' unless table_exists?(statement.relation.relname) + + [statement.relation.relname, statement.idxname] + end + + # This raises `PgQuery::ParseError` if the given statement + # is syntactically incorrect, therefore, validates that the + # index definition is correct. + def index_statement_from!(definition) + parsed_query = PgQuery.parse(definition) + + parsed_query.tree.stmts[0].stmt.index_stmt end end end |