diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-05-20 14:34:42 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-05-20 14:34:42 +0000 |
commit | 9f46488805e86b1bc341ea1620b866016c2ce5ed (patch) | |
tree | f9748c7e287041e37d6da49e0a29c9511dc34768 /lib/gitlab/database/partitioning_migration_helpers | |
parent | dfc92d081ea0332d69c8aca2f0e745cb48ae5e6d (diff) | |
download | gitlab-ce-9f46488805e86b1bc341ea1620b866016c2ce5ed.tar.gz |
Add latest changes from gitlab-org/gitlab@13-0-stable-ee
Diffstat (limited to 'lib/gitlab/database/partitioning_migration_helpers')
-rw-r--r-- | lib/gitlab/database/partitioning_migration_helpers/partitioned_foreign_key.rb | 13 | ||||
-rw-r--r-- | lib/gitlab/database/partitioning_migration_helpers/partitioned_foreign_key_validator.rb | 28 |
2 files changed, 41 insertions, 0 deletions
diff --git a/lib/gitlab/database/partitioning_migration_helpers/partitioned_foreign_key.rb b/lib/gitlab/database/partitioning_migration_helpers/partitioned_foreign_key.rb new file mode 100644 index 00000000000..f9a90511f9b --- /dev/null +++ b/lib/gitlab/database/partitioning_migration_helpers/partitioned_foreign_key.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +module Gitlab + module Database + module PartitioningMigrationHelpers + class PartitionedForeignKey < ApplicationRecord + validates_with PartitionedForeignKeyValidator + + scope :by_referenced_table, ->(table) { where(to_table: table) } + end + end + end +end diff --git a/lib/gitlab/database/partitioning_migration_helpers/partitioned_foreign_key_validator.rb b/lib/gitlab/database/partitioning_migration_helpers/partitioned_foreign_key_validator.rb new file mode 100644 index 00000000000..089cf2b8931 --- /dev/null +++ b/lib/gitlab/database/partitioning_migration_helpers/partitioned_foreign_key_validator.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +module Gitlab + module Database + module PartitioningMigrationHelpers + class PartitionedForeignKeyValidator < ActiveModel::Validator + def validate(record) + validate_key_part(record, :from_table, :from_column) + validate_key_part(record, :to_table, :to_column) + end + + private + + def validate_key_part(record, table_field, column_field) + if !connection.table_exists?(record[table_field]) + record.errors.add(table_field, 'must be a valid table') + elsif !connection.column_exists?(record[table_field], record[column_field]) + record.errors.add(column_field, 'must be a valid column') + end + end + + def connection + ActiveRecord::Base.connection + end + end + end + end +end |