2019-02-15 15:39:39 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
2019-10-12 21:52:04 +05:30
|
|
|
require Rails.root.join('ee', 'spec', 'db', 'schema_support') if Gitlab.ee?
|
2019-02-15 15:39:39 +05:30
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
RSpec.describe 'Database schema' do
|
2019-12-04 20:38:33 +05:30
|
|
|
prepend_if_ee('EE::DB::SchemaSupport')
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
let(:connection) { ActiveRecord::Base.connection }
|
|
|
|
let(:tables) { connection.tables }
|
|
|
|
|
|
|
|
# Use if you are certain that this column should not have a foreign key
|
2019-10-12 21:52:04 +05:30
|
|
|
# EE: edit the ee/spec/db/schema_support.rb
|
2019-02-15 15:39:39 +05:30
|
|
|
IGNORED_FK_COLUMNS = {
|
|
|
|
abuse_reports: %w[reporter_id user_id],
|
2019-12-26 22:10:19 +05:30
|
|
|
application_settings: %w[performance_bar_allowed_group_id slack_app_id snowplow_app_id eks_account_id eks_access_key_id],
|
2019-09-04 21:01:54 +05:30
|
|
|
approvals: %w[user_id],
|
|
|
|
approver_groups: %w[target_id],
|
2020-05-24 23:13:21 +05:30
|
|
|
approvers: %w[target_id user_id],
|
2019-02-15 15:39:39 +05:30
|
|
|
audit_events: %w[author_id entity_id],
|
|
|
|
award_emoji: %w[awardable_id user_id],
|
2019-12-21 20:55:43 +05:30
|
|
|
aws_roles: %w[role_external_id],
|
2019-09-04 21:01:54 +05:30
|
|
|
boards: %w[milestone_id],
|
2020-04-22 19:07:51 +05:30
|
|
|
chat_names: %w[chat_id team_id user_id],
|
2019-02-15 15:39:39 +05:30
|
|
|
chat_teams: %w[team_id],
|
|
|
|
ci_builds: %w[erased_by_id runner_id trigger_request_id user_id],
|
|
|
|
ci_pipelines: %w[user_id],
|
|
|
|
ci_runner_projects: %w[runner_id],
|
|
|
|
ci_trigger_requests: %w[commit_id],
|
2019-12-21 20:55:43 +05:30
|
|
|
cluster_providers_aws: %w[security_group_id vpc_id access_key_id],
|
2019-02-15 15:39:39 +05:30
|
|
|
cluster_providers_gcp: %w[gcp_project_id operation_id],
|
2020-05-24 23:13:21 +05:30
|
|
|
commit_user_mentions: %w[commit_id],
|
2019-02-15 15:39:39 +05:30
|
|
|
deploy_keys_projects: %w[deploy_key_id],
|
|
|
|
deployments: %w[deployable_id environment_id user_id],
|
2019-10-12 21:52:04 +05:30
|
|
|
draft_notes: %w[discussion_id commit_id],
|
2019-02-15 15:39:39 +05:30
|
|
|
emails: %w[user_id],
|
2020-03-13 15:44:24 +05:30
|
|
|
epics: %w[updated_by_id last_edited_by_id state_id],
|
2020-05-24 23:13:21 +05:30
|
|
|
events: %w[target_id],
|
2019-02-15 15:39:39 +05:30
|
|
|
forked_project_links: %w[forked_from_project_id],
|
2019-09-04 21:01:54 +05:30
|
|
|
geo_event_log: %w[hashed_storage_attachments_event_id],
|
|
|
|
geo_job_artifact_deleted_events: %w[job_artifact_id],
|
|
|
|
geo_lfs_object_deleted_events: %w[lfs_object_id],
|
|
|
|
geo_node_statuses: %w[last_event_id cursor_last_event_id],
|
|
|
|
geo_nodes: %w[oauth_application_id],
|
|
|
|
geo_repository_deleted_events: %w[project_id],
|
|
|
|
geo_upload_deleted_events: %w[upload_id model_id],
|
2020-01-01 13:55:28 +05:30
|
|
|
gitlab_subscription_histories: %w[gitlab_subscription_id hosted_plan_id namespace_id],
|
2019-02-15 15:39:39 +05:30
|
|
|
identities: %w[user_id],
|
2020-05-24 23:13:21 +05:30
|
|
|
import_failures: %w[project_id],
|
2019-07-07 11:18:12 +05:30
|
|
|
issues: %w[last_edited_by_id state_id],
|
2019-09-04 21:01:54 +05:30
|
|
|
jira_tracker_data: %w[jira_issue_transition_id],
|
2019-02-15 15:39:39 +05:30
|
|
|
keys: %w[user_id],
|
|
|
|
label_links: %w[target_id],
|
2019-09-04 21:01:54 +05:30
|
|
|
ldap_group_links: %w[group_id],
|
2020-05-24 23:13:21 +05:30
|
|
|
lfs_objects_projects: %w[lfs_object_id project_id],
|
2019-02-15 15:39:39 +05:30
|
|
|
members: %w[source_id created_by_id],
|
2019-07-07 11:18:12 +05:30
|
|
|
merge_requests: %w[last_edited_by_id state_id],
|
2019-02-15 15:39:39 +05:30
|
|
|
namespaces: %w[owner_id parent_id],
|
2020-03-13 15:44:24 +05:30
|
|
|
notes: %w[author_id commit_id noteable_id updated_by_id resolved_by_id confirmed_by_id discussion_id],
|
2019-02-15 15:39:39 +05:30
|
|
|
notification_settings: %w[source_id],
|
|
|
|
oauth_access_grants: %w[resource_owner_id application_id],
|
|
|
|
oauth_access_tokens: %w[resource_owner_id application_id],
|
|
|
|
oauth_applications: %w[owner_id],
|
2020-04-08 14:13:33 +05:30
|
|
|
open_project_tracker_data: %w[closed_status_id],
|
2019-02-15 15:39:39 +05:30
|
|
|
project_group_links: %w[group_id],
|
|
|
|
project_statistics: %w[namespace_id],
|
2020-05-24 23:13:21 +05:30
|
|
|
projects: %w[creator_id ci_id mirror_user_id],
|
2019-02-15 15:39:39 +05:30
|
|
|
redirect_routes: %w[source_id],
|
|
|
|
repository_languages: %w[programming_language_id],
|
|
|
|
routes: %w[source_id],
|
|
|
|
sent_notifications: %w[project_id noteable_id recipient_id commit_id in_reply_to_discussion_id],
|
2020-05-24 23:13:21 +05:30
|
|
|
slack_integrations: %w[team_id user_id],
|
2019-02-15 15:39:39 +05:30
|
|
|
snippets: %w[author_id],
|
|
|
|
spam_logs: %w[user_id],
|
|
|
|
subscriptions: %w[user_id subscribable_id],
|
2020-05-24 23:13:21 +05:30
|
|
|
suggestions: %w[commit_id],
|
2019-02-15 15:39:39 +05:30
|
|
|
taggings: %w[tag_id taggable_id tagger_id],
|
|
|
|
timelogs: %w[user_id],
|
|
|
|
todos: %w[target_id commit_id],
|
|
|
|
uploads: %w[model_id],
|
|
|
|
user_agent_details: %w[subject_id],
|
2019-09-04 21:01:54 +05:30
|
|
|
users: %w[color_scheme_id created_by_id theme_id email_opted_in_source_id],
|
2019-02-15 15:39:39 +05:30
|
|
|
users_star_projects: %w[user_id],
|
2019-09-04 21:01:54 +05:30
|
|
|
vulnerability_identifiers: %w[external_id],
|
|
|
|
vulnerability_scanners: %w[external_id],
|
2020-05-24 23:13:21 +05:30
|
|
|
web_hooks: %w[service_id group_id]
|
2019-02-15 15:39:39 +05:30
|
|
|
}.with_indifferent_access.freeze
|
|
|
|
|
|
|
|
context 'for table' do
|
|
|
|
ActiveRecord::Base.connection.tables.sort.each do |table|
|
|
|
|
describe table do
|
|
|
|
let(:indexes) { connection.indexes(table) }
|
|
|
|
let(:columns) { connection.columns(table) }
|
|
|
|
let(:foreign_keys) { connection.foreign_keys(table) }
|
2020-06-23 00:09:42 +05:30
|
|
|
# take the first column in case we're using a composite primary key
|
|
|
|
let(:primary_key_column) { Array(connection.primary_key(table)).first }
|
2019-02-15 15:39:39 +05:30
|
|
|
|
|
|
|
context 'all foreign keys' do
|
|
|
|
# for index to be effective, the FK constraint has to be at first place
|
|
|
|
it 'are indexed' do
|
|
|
|
first_indexed_column = indexes.map(&:columns).map(&:first)
|
|
|
|
foreign_keys_columns = foreign_keys.map(&:column)
|
|
|
|
|
|
|
|
# Add the primary key column to the list of indexed columns because
|
|
|
|
# postgres and mysql both automatically create an index on the primary
|
|
|
|
# key. Also, the rails connection.indexes() method does not return
|
|
|
|
# automatically generated indexes (like the primary key index).
|
|
|
|
first_indexed_column = first_indexed_column.push(primary_key_column)
|
|
|
|
|
|
|
|
expect(first_indexed_column.uniq).to include(*foreign_keys_columns)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'columns ending with _id' do
|
|
|
|
let(:column_names) { columns.map(&:name) }
|
|
|
|
let(:column_names_with_id) { column_names.select { |column_name| column_name.ends_with?('_id') } }
|
|
|
|
let(:foreign_keys_columns) { foreign_keys.map(&:column) }
|
|
|
|
let(:ignored_columns) { ignored_fk_columns(table) }
|
|
|
|
|
|
|
|
it 'do have the foreign keys' do
|
|
|
|
expect(column_names_with_id - ignored_columns).to contain_exactly(*foreign_keys_columns)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-26 22:10:19 +05:30
|
|
|
# These pre-existing enums have limits > 2 bytes
|
|
|
|
IGNORED_LIMIT_ENUMS = {
|
|
|
|
'Analytics::CycleAnalytics::GroupStage' => %w[start_event_identifier end_event_identifier],
|
|
|
|
'Analytics::CycleAnalytics::ProjectStage' => %w[start_event_identifier end_event_identifier],
|
|
|
|
'Ci::Bridge' => %w[failure_reason],
|
|
|
|
'Ci::Build' => %w[failure_reason],
|
|
|
|
'Ci::BuildMetadata' => %w[timeout_source],
|
|
|
|
'Ci::BuildTraceChunk' => %w[data_store],
|
2020-04-22 19:07:51 +05:30
|
|
|
'Ci::DailyReportResult' => %w[param_type],
|
2019-12-26 22:10:19 +05:30
|
|
|
'Ci::JobArtifact' => %w[file_type],
|
|
|
|
'Ci::Pipeline' => %w[source config_source failure_reason],
|
2020-03-13 15:44:24 +05:30
|
|
|
'Ci::Processable' => %w[failure_reason],
|
2019-12-26 22:10:19 +05:30
|
|
|
'Ci::Runner' => %w[access_level],
|
|
|
|
'Ci::Stage' => %w[status],
|
|
|
|
'Clusters::Applications::Ingress' => %w[ingress_type],
|
|
|
|
'Clusters::Cluster' => %w[platform_type provider_type],
|
|
|
|
'CommitStatus' => %w[failure_reason],
|
|
|
|
'GenericCommitStatus' => %w[failure_reason],
|
|
|
|
'Gitlab::DatabaseImporters::CommonMetrics::PrometheusMetric' => %w[group],
|
|
|
|
'InternalId' => %w[usage],
|
|
|
|
'List' => %w[list_type],
|
|
|
|
'NotificationSetting' => %w[level],
|
|
|
|
'Project' => %w[auto_cancel_pending_pipelines],
|
|
|
|
'ProjectAutoDevops' => %w[deploy_strategy],
|
|
|
|
'PrometheusMetric' => %w[group],
|
|
|
|
'ResourceLabelEvent' => %w[action],
|
|
|
|
'User' => %w[layout dashboard project_view],
|
|
|
|
'UserCallout' => %w[feature_name],
|
|
|
|
'PrometheusAlert' => %w[operator]
|
|
|
|
}.freeze
|
|
|
|
|
|
|
|
context 'for enums' do
|
|
|
|
ApplicationRecord.descendants.each do |model|
|
|
|
|
describe model do
|
|
|
|
let(:ignored_enums) { ignored_limit_enums(model.name) }
|
|
|
|
let(:enums) { model.defined_enums.keys - ignored_enums }
|
|
|
|
|
|
|
|
it 'uses smallint for enums' do
|
|
|
|
expect(model).to use_smallint_for_enums(enums)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
private
|
|
|
|
|
|
|
|
def ignored_fk_columns(column)
|
|
|
|
IGNORED_FK_COLUMNS.fetch(column, [])
|
|
|
|
end
|
2019-12-26 22:10:19 +05:30
|
|
|
|
|
|
|
def ignored_limit_enums(model)
|
|
|
|
IGNORED_LIMIT_ENUMS.fetch(model, [])
|
|
|
|
end
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|