Commit d3f94db3 authored by Max Woolf's avatar Max Woolf Committed by Sean McGivern

Create ComplianceManagement::Framework model

Creates a new model `ComplianceManagement::Framework`
to be used to represent a compliance management framework.

This is the first step to implementing
https://gitlab.com/gitlab-org/gitlab/-/issues/251113
parent d667c0b4
---
title: Create ComplianceManagement::Framework Model
merge_request: 43301
author:
type: changed
# frozen_string_literal: true
class AddComplianceFrameworkModel < ActiveRecord::Migration[6.0]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
disable_ddl_transaction!
def up
unless table_exists?(:compliance_management_frameworks)
with_lock_retries do
create_table :compliance_management_frameworks do |t|
t.references :group, foreign_key: { to_table: :namespaces, on_delete: :cascade }, null: false, index: false
t.text :name, null: false
t.text :description, null: false
t.text :color, null: false
t.index [:group_id, :name], unique: true
end
end
end
add_text_limit :compliance_management_frameworks, :name, 255
add_text_limit :compliance_management_frameworks, :description, 255
add_text_limit :compliance_management_frameworks, :color, 10
end
def down
with_lock_retries do
drop_table :compliance_management_frameworks
end
end
end
a8450c6c21b1182afd06c88af18c0d9be92b0e7fdc73505c07d4ab3fddd39abf
\ No newline at end of file
...@@ -11087,6 +11087,26 @@ CREATE SEQUENCE commit_user_mentions_id_seq ...@@ -11087,6 +11087,26 @@ CREATE SEQUENCE commit_user_mentions_id_seq
ALTER SEQUENCE commit_user_mentions_id_seq OWNED BY commit_user_mentions.id; ALTER SEQUENCE commit_user_mentions_id_seq OWNED BY commit_user_mentions.id;
CREATE TABLE compliance_management_frameworks (
id bigint NOT NULL,
group_id bigint NOT NULL,
name text NOT NULL,
description text NOT NULL,
color text NOT NULL,
CONSTRAINT check_08cd34b2c2 CHECK ((char_length(color) <= 10)),
CONSTRAINT check_1617e0b87e CHECK ((char_length(description) <= 255)),
CONSTRAINT check_ab00bc2193 CHECK ((char_length(name) <= 255))
);
CREATE SEQUENCE compliance_management_frameworks_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER SEQUENCE compliance_management_frameworks_id_seq OWNED BY compliance_management_frameworks.id;
CREATE TABLE container_expiration_policies ( CREATE TABLE container_expiration_policies (
project_id bigint NOT NULL, project_id bigint NOT NULL,
created_at timestamp with time zone NOT NULL, created_at timestamp with time zone NOT NULL,
...@@ -17344,6 +17364,8 @@ ALTER TABLE ONLY clusters_kubernetes_namespaces ALTER COLUMN id SET DEFAULT next ...@@ -17344,6 +17364,8 @@ ALTER TABLE ONLY clusters_kubernetes_namespaces ALTER COLUMN id SET DEFAULT next
ALTER TABLE ONLY commit_user_mentions ALTER COLUMN id SET DEFAULT nextval('commit_user_mentions_id_seq'::regclass); ALTER TABLE ONLY commit_user_mentions ALTER COLUMN id SET DEFAULT nextval('commit_user_mentions_id_seq'::regclass);
ALTER TABLE ONLY compliance_management_frameworks ALTER COLUMN id SET DEFAULT nextval('compliance_management_frameworks_id_seq'::regclass);
ALTER TABLE ONLY container_repositories ALTER COLUMN id SET DEFAULT nextval('container_repositories_id_seq'::regclass); ALTER TABLE ONLY container_repositories ALTER COLUMN id SET DEFAULT nextval('container_repositories_id_seq'::regclass);
ALTER TABLE ONLY conversational_development_index_metrics ALTER COLUMN id SET DEFAULT nextval('conversational_development_index_metrics_id_seq'::regclass); ALTER TABLE ONLY conversational_development_index_metrics ALTER COLUMN id SET DEFAULT nextval('conversational_development_index_metrics_id_seq'::regclass);
...@@ -18377,6 +18399,9 @@ ALTER TABLE ONLY clusters ...@@ -18377,6 +18399,9 @@ ALTER TABLE ONLY clusters
ALTER TABLE ONLY commit_user_mentions ALTER TABLE ONLY commit_user_mentions
ADD CONSTRAINT commit_user_mentions_pkey PRIMARY KEY (id); ADD CONSTRAINT commit_user_mentions_pkey PRIMARY KEY (id);
ALTER TABLE ONLY compliance_management_frameworks
ADD CONSTRAINT compliance_management_frameworks_pkey PRIMARY KEY (id);
ALTER TABLE ONLY container_expiration_policies ALTER TABLE ONLY container_expiration_policies
ADD CONSTRAINT container_expiration_policies_pkey PRIMARY KEY (project_id); ADD CONSTRAINT container_expiration_policies_pkey PRIMARY KEY (project_id);
...@@ -19987,6 +20012,8 @@ CREATE INDEX index_clusters_on_user_id ON clusters USING btree (user_id); ...@@ -19987,6 +20012,8 @@ CREATE INDEX index_clusters_on_user_id ON clusters USING btree (user_id);
CREATE UNIQUE INDEX index_commit_user_mentions_on_note_id ON commit_user_mentions USING btree (note_id); CREATE UNIQUE INDEX index_commit_user_mentions_on_note_id ON commit_user_mentions USING btree (note_id);
CREATE UNIQUE INDEX index_compliance_management_frameworks_on_group_id_and_name ON compliance_management_frameworks USING btree (group_id, name);
CREATE INDEX index_container_expiration_policies_on_next_run_at_and_enabled ON container_expiration_policies USING btree (next_run_at, enabled); CREATE INDEX index_container_expiration_policies_on_next_run_at_and_enabled ON container_expiration_policies USING btree (next_run_at, enabled);
CREATE INDEX index_container_repositories_on_project_id ON container_repositories USING btree (project_id); CREATE INDEX index_container_repositories_on_project_id ON container_repositories USING btree (project_id);
...@@ -23668,6 +23695,9 @@ ALTER TABLE ONLY requirements_management_test_reports ...@@ -23668,6 +23695,9 @@ ALTER TABLE ONLY requirements_management_test_reports
ALTER TABLE ONLY pool_repositories ALTER TABLE ONLY pool_repositories
ADD CONSTRAINT fk_rails_d2711daad4 FOREIGN KEY (source_project_id) REFERENCES projects(id) ON DELETE SET NULL; ADD CONSTRAINT fk_rails_d2711daad4 FOREIGN KEY (source_project_id) REFERENCES projects(id) ON DELETE SET NULL;
ALTER TABLE ONLY compliance_management_frameworks
ADD CONSTRAINT fk_rails_d3240d6339 FOREIGN KEY (group_id) REFERENCES namespaces(id) ON DELETE CASCADE;
ALTER TABLE ONLY group_group_links ALTER TABLE ONLY group_group_links
ADD CONSTRAINT fk_rails_d3a0488427 FOREIGN KEY (shared_group_id) REFERENCES namespaces(id) ON DELETE CASCADE; ADD CONSTRAINT fk_rails_d3a0488427 FOREIGN KEY (shared_group_id) REFERENCES namespaces(id) ON DELETE CASCADE;
......
# frozen_string_literal: true
module ComplianceManagement
class Framework < ApplicationRecord
include StripAttribute
self.table_name = 'compliance_management_frameworks'
strip_attributes :name, :color
belongs_to :group
validates :group, presence: true
validates :name, presence: true, uniqueness: true, length: { maximum: 255 }
validates :description, presence: true, length: { maximum: 255 }
validates :color, color: true, allow_blank: false, length: { maximum: 10 }
validates :group_id, uniqueness: { scope: [:name] }
end
end
# frozen_string_literal: true
FactoryBot.define do
factory :compliance_framework, class: 'ComplianceManagement::Framework' do
association :group, factory: :group
name { 'GDPR' }
description { 'The General Data Protection Regulation (GDPR) is a regulation in EU law on data protection and privacy in the European Union (EU) and the European Economic Area (EEA).' }
color { '#004494' }
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe ComplianceManagement::Framework do
context 'validation' do
let_it_be(:framework) { create(:compliance_framework) }
subject { framework }
it { is_expected.to validate_uniqueness_of(:group_id).scoped_to(:name) }
it { is_expected.to validate_presence_of(:name) }
it { is_expected.to validate_length_of(:name).is_at_most(255) }
it { is_expected.to validate_length_of(:description).is_at_most(255) }
it { is_expected.to validate_length_of(:color).is_at_most(10) }
end
describe 'color' do
context 'with whitespace' do
subject { create(:compliance_framework, color: ' #ABC123 ')}
it 'strips whitespace' do
expect(subject.color).to eq('#ABC123')
end
end
end
end
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