2022-05-07 20:08:51 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Database
|
|
|
|
module MigrationHelpers
|
|
|
|
module RestrictGitlabSchema
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
|
|
|
class_attribute :allowed_gitlab_schemas
|
|
|
|
end
|
|
|
|
|
|
|
|
class_methods do
|
|
|
|
def restrict_gitlab_migration(gitlab_schema:)
|
|
|
|
unless Gitlab::Database::GitlabSchema.schema_names.include?(gitlab_schema)
|
|
|
|
raise "Unknown 'gitlab_schema: #{gitlab_schema}' specified. It needs to be one of: " \
|
|
|
|
"#{Gitlab::Database::GitlabSchema.schema_names.to_a}"
|
|
|
|
end
|
|
|
|
|
|
|
|
self.allowed_gitlab_schemas = [gitlab_schema]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
def exec_migration(conn, direction)
|
2022-05-07 20:08:51 +05:30
|
|
|
if unmatched_schemas.any?
|
2022-06-21 17:19:12 +05:30
|
|
|
migration_skipped
|
|
|
|
return
|
2022-05-07 20:08:51 +05:30
|
|
|
end
|
|
|
|
|
2022-07-16 23:28:13 +05:30
|
|
|
Gitlab::Database::QueryAnalyzer.instance.within([validator_class, connection_validator_class]) do
|
2022-05-07 20:08:51 +05:30
|
|
|
validator_class.allowed_gitlab_schemas = self.allowed_gitlab_schemas
|
|
|
|
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
def migration_skipped
|
2022-07-23 23:45:48 +05:30
|
|
|
say "The migration is skipped since it modifies the schemas: #{self.class.allowed_gitlab_schemas}."
|
|
|
|
say "This database can only apply migrations in one of the following schemas: " \
|
|
|
|
"#{allowed_schemas_for_connection}."
|
2022-06-21 17:19:12 +05:30
|
|
|
end
|
|
|
|
|
2022-05-07 20:08:51 +05:30
|
|
|
def validator_class
|
|
|
|
Gitlab::Database::QueryAnalyzers::RestrictAllowedSchemas
|
|
|
|
end
|
|
|
|
|
2022-07-16 23:28:13 +05:30
|
|
|
def connection_validator_class
|
|
|
|
Gitlab::Database::QueryAnalyzers::GitlabSchemasValidateConnection
|
|
|
|
end
|
|
|
|
|
2022-05-07 20:08:51 +05:30
|
|
|
def unmatched_schemas
|
|
|
|
(self.allowed_gitlab_schemas || []) - allowed_schemas_for_connection
|
|
|
|
end
|
|
|
|
|
|
|
|
def allowed_schemas_for_connection
|
|
|
|
Gitlab::Database.gitlab_schemas_for_connection(connection)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|