Commit 736b1b6f authored by Adam Hegyi's avatar Adam Hegyi

Merge branch '341783-move-gitlab-schema-and-detection-to-lib' into 'master'

Move gitlab_schema yml/code from spec/support to lib/gitlab

See merge request gitlab-org/gitlab!73148
parents 199b9314 715b921c
# frozen_string_literal: true
# This module gathers information about table to schema mapping
# to understand table affinity
module Gitlab
module Database
module GitlabSchema
def self.table_schemas(tables)
tables.map { |table| table_schema(table) }.to_set
end
def self.table_schema(name)
# When undefined it's best to return a unique name so that we don't incorrectly assume that 2 undefined schemas belong on the same database
tables_to_schema[name] || :"undefined_#{name}"
end
def self.tables_to_schema
@tables_to_schema ||= YAML.load_file(Rails.root.join('lib/gitlab/database/gitlab_schemas.yml'))
end
end
end
end
# frozen_string_literal: true # frozen_string_literal: true
require 'spec_helper' require 'spec_helper'
RSpec.describe Database::GitlabSchema do RSpec.describe Gitlab::Database::GitlabSchema do
it 'matches all the tables in the database', :aggregate_failures do it 'matches all the tables in the database', :aggregate_failures do
# These tables do not need a gitlab_schema # These tables do not need a gitlab_schema
excluded_tables = %w(ar_internal_metadata schema_migrations) excluded_tables = %w(ar_internal_metadata schema_migrations)
......
# frozen_string_literal: true
# This module gathes information about table to schema mapping
# to understand table affinity
module Database
module GitlabSchema
def self.table_schemas(tables)
tables.map { |table| table_schema(table) }.to_set
end
def self.table_schema(name)
# When undefined it's best to return a unique name so that we don't incorrectly assume that 2 undefined schemas belong on the same database
tables_to_schema[name] || :"undefined_#{name}"
end
def self.tables_to_schema
@tables_to_schema ||= YAML.load_file(Rails.root.join('spec/support/database/gitlab_schemas.yml'))
end
end
end
...@@ -100,7 +100,7 @@ module Database ...@@ -100,7 +100,7 @@ module Database
cross_database_context[:modified_tables_by_db][database].merge(tables) cross_database_context[:modified_tables_by_db][database].merge(tables)
all_tables = cross_database_context[:modified_tables_by_db].values.map(&:to_a).flatten all_tables = cross_database_context[:modified_tables_by_db].values.map(&:to_a).flatten
schemas = Database::GitlabSchema.table_schemas(all_tables) schemas = ::Gitlab::Database::GitlabSchema.table_schemas(all_tables)
if schemas.many? if schemas.many?
message = "Cross-database data modification of '#{schemas.to_a.join(", ")}' were detected within " \ message = "Cross-database data modification of '#{schemas.to_a.join(", ")}' were detected within " \
......
...@@ -35,7 +35,7 @@ module Database ...@@ -35,7 +35,7 @@ module Database
# https://github.com/pganalyze/pg_query/issues/209 # https://github.com/pganalyze/pg_query/issues/209
tables = PgQuery.parse(sql).tables tables = PgQuery.parse(sql).tables
schemas = Database::GitlabSchema.table_schemas(tables) schemas = ::Gitlab::Database::GitlabSchema.table_schemas(tables)
if schemas.include?(:gitlab_ci) && schemas.include?(:gitlab_main) if schemas.include?(:gitlab_ci) && schemas.include?(:gitlab_main)
Thread.current[:has_cross_join_exception] = true Thread.current[:has_cross_join_exception] = true
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment