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
path: root/spec/db
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2020-06-18 15:09:25 +0300
committerGitLab Bot <gitlab-bot@gitlab.com>2020-06-18 15:09:25 +0300
commit8eef083ccd59505b606e9733d38589cbd3d01fdf (patch)
tree72cc132b2bb3a60a8802d6af80c49953d7084afd /spec/db
parente1d53af7ad463decf1df2944343c66b27512d4e1 (diff)
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/db')
-rw-r--r--spec/db/schema_spec.rb51
1 files changed, 51 insertions, 0 deletions
diff --git a/spec/db/schema_spec.rb b/spec/db/schema_spec.rb
index 95d7981b85c..2322babcd53 100644
--- a/spec/db/schema_spec.rb
+++ b/spec/db/schema_spec.rb
@@ -8,6 +8,7 @@ RSpec.describe 'Database schema' do
let(:connection) { ActiveRecord::Base.connection }
let(:tables) { connection.tables }
+ let(:columns_name_with_jsonb) { retrieve_columns_name_with_jsonb }
# Use if you are certain that this column should not have a foreign key
# EE: edit the ee/spec/db/schema_support.rb
@@ -169,8 +170,54 @@ RSpec.describe 'Database schema' do
end
end
+ # These pre-existing columns does not use a schema validation yet
+ IGNORED_JSONB_COLUMNS = {
+ "ApplicationSetting" => %w[repository_storages_weighted],
+ "AlertManagement::Alert" => %w[payload],
+ "Ci::BuildMetadata" => %w[config_options config_variables],
+ "Geo::Event" => %w[payload],
+ "GeoNodeStatus" => %w[status],
+ "Operations::FeatureFlagScope" => %w[strategies],
+ "Operations::FeatureFlags::Strategy" => %w[parameters],
+ "Packages::Composer::Metadatum" => %w[composer_json],
+ "Releases::Evidence" => %w[summary]
+ }.freeze
+
+ # We are skipping GEO models for now as it adds up complexity
+ describe 'for jsonb columns' do
+ it 'uses json schema validator' do
+ columns_name_with_jsonb.each do |hash|
+ next if models_by_table_name[hash["table_name"]].nil?
+
+ models_by_table_name[hash["table_name"]].each do |model|
+ jsonb_columns = [hash["column_name"]] - ignored_jsonb_columns(model.name)
+
+ expect(model).to validate_jsonb_schema(jsonb_columns)
+ end
+ end
+ end
+ end
+
private
+ def retrieve_columns_name_with_jsonb
+ sql = <<~SQL
+ SELECT table_name, column_name, data_type
+ FROM information_schema.columns
+ WHERE table_catalog = '#{ApplicationRecord.connection_config[:database]}'
+ AND table_schema = 'public'
+ AND table_name NOT LIKE 'pg_%'
+ AND data_type = 'jsonb'
+ ORDER BY table_name, column_name, data_type
+ SQL
+
+ ApplicationRecord.connection.select_all(sql).to_a
+ end
+
+ def models_by_table_name
+ @models_by_table_name ||= ApplicationRecord.descendants.reject(&:abstract_class).group_by(&:table_name)
+ end
+
def ignored_fk_columns(column)
IGNORED_FK_COLUMNS.fetch(column, [])
end
@@ -178,4 +225,8 @@ RSpec.describe 'Database schema' do
def ignored_limit_enums(model)
IGNORED_LIMIT_ENUMS.fetch(model, [])
end
+
+ def ignored_jsonb_columns(model)
+ IGNORED_JSONB_COLUMNS.fetch(model, [])
+ end
end