Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'lib/gitlab/database/tables_sorted_by_foreign_keys.rb')
-rw-r--r--lib/gitlab/database/tables_sorted_by_foreign_keys.rb27
1 files changed, 22 insertions, 5 deletions
diff --git a/lib/gitlab/database/tables_sorted_by_foreign_keys.rb b/lib/gitlab/database/tables_sorted_by_foreign_keys.rb
index 9f096904d31..b2a7f5442e9 100644
--- a/lib/gitlab/database/tables_sorted_by_foreign_keys.rb
+++ b/lib/gitlab/database/tables_sorted_by_foreign_keys.rb
@@ -26,15 +26,32 @@ module Gitlab
# it maps the tables to the tables that depend on it
def tables_dependencies
- @tables.to_h do |table_name|
- [table_name, all_foreign_keys[table_name]&.map(&:from_table).to_a]
+ @tables.index_with do |table_name|
+ all_foreign_keys[table_name]
end
end
def all_foreign_keys
- @all_foreign_keys ||= @tables.flat_map do |table_name|
- @connection.foreign_keys(table_name)
- end.group_by(&:to_table)
+ @all_foreign_keys ||= @tables.each_with_object(Hash.new { |h, k| h[k] = [] }) do |table, hash|
+ foreign_keys_for(table).each do |fk|
+ hash[fk.to_table] << table
+ end
+ end
+ end
+
+ def foreign_keys_for(table)
+ # Detached partitions like gitlab_partitions_dynamic._test_gitlab_partition_20220101
+ # store their foreign keys in the public schema.
+ #
+ # See spec/lib/gitlab/database/tables_sorted_by_foreign_keys_spec.rb
+ # for an example
+ name = ActiveRecord::ConnectionAdapters::PostgreSQL::Utils.extract_schema_qualified_name(table)
+
+ if name.schema == ::Gitlab::Database::DYNAMIC_PARTITIONS_SCHEMA.to_s
+ @connection.foreign_keys(name.identifier)
+ else
+ @connection.foreign_keys(table)
+ end
end
end
end