Commit 7d263f4e authored by Mayra Cabrera's avatar Mayra Cabrera

Merge branch '2256-add-organizations-and-contacts' into 'master'

Add organizations table and model

See merge request gitlab-org/gitlab!67551
parents 47268e78 6a69edff
# frozen_string_literal: true
class CustomerRelations::Organization < ApplicationRecord
self.table_name = "customer_relations_organizations"
belongs_to :group, -> { where(type: 'Group') }, foreign_key: 'group_id'
before_validation :strip_whitespace!
enum state: {
inactive: 0,
active: 1
}
validates :group, presence: true
validates :name, presence: true
validates :name, uniqueness: { case_sensitive: false, scope: [:group_id] }
validates :name, length: { maximum: 255 }
validates :description, length: { maximum: 1024 }
def self.find_by_name(group_id, name)
where(group: group_id)
.where('LOWER(name) = LOWER(?)', name)
end
private
def strip_whitespace!
name&.strip!
end
end
# frozen_string_literal: true
class CreateCustomerRelationsOrganizations < ActiveRecord::Migration[6.1]
include Gitlab::Database::MigrationHelpers
def up
create_table_with_constraints :customer_relations_organizations do |t|
t.references :group, index: false, null: false, foreign_key: { to_table: :namespaces, on_delete: :cascade }
t.timestamps_with_timezone null: false
t.integer :state, limit: 1, default: 1, null: false
t.decimal :default_rate, precision: 18, scale: 2
t.text :name, null: false
t.text :description
t.text_limit :name, 255
t.text_limit :description, 1024
t.index 'group_id, LOWER(name)', unique: true, name: :index_customer_relations_organizations_on_unique_name_per_group
end
end
def down
with_lock_retries do
drop_table :customer_relations_organizations
end
end
end
db62fb6413db4be5e1013bccf16b0c3a66c9aaf9f3d646f42442be16c511af5f
\ No newline at end of file
......@@ -12058,6 +12058,28 @@ CREATE SEQUENCE custom_emoji_id_seq
ALTER SEQUENCE custom_emoji_id_seq OWNED BY custom_emoji.id;
CREATE TABLE customer_relations_organizations (
id bigint NOT NULL,
group_id bigint NOT NULL,
created_at timestamp with time zone NOT NULL,
updated_at timestamp with time zone NOT NULL,
state smallint DEFAULT 1 NOT NULL,
default_rate numeric(18,2),
name text NOT NULL,
description text,
CONSTRAINT check_2ba9ef1c4c CHECK ((char_length(name) <= 255)),
CONSTRAINT check_e476b6058e CHECK ((char_length(description) <= 1024))
);
CREATE SEQUENCE customer_relations_organizations_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER SEQUENCE customer_relations_organizations_id_seq OWNED BY customer_relations_organizations.id;
CREATE TABLE dast_profile_schedules (
id bigint NOT NULL,
project_id bigint NOT NULL,
......@@ -20179,6 +20201,8 @@ ALTER TABLE ONLY csv_issue_imports ALTER COLUMN id SET DEFAULT nextval('csv_issu
ALTER TABLE ONLY custom_emoji ALTER COLUMN id SET DEFAULT nextval('custom_emoji_id_seq'::regclass);
ALTER TABLE ONLY customer_relations_organizations ALTER COLUMN id SET DEFAULT nextval('customer_relations_organizations_id_seq'::regclass);
ALTER TABLE ONLY dast_profile_schedules ALTER COLUMN id SET DEFAULT nextval('dast_profile_schedules_id_seq'::regclass);
ALTER TABLE ONLY dast_profiles ALTER COLUMN id SET DEFAULT nextval('dast_profiles_id_seq'::regclass);
......@@ -21458,6 +21482,9 @@ ALTER TABLE ONLY csv_issue_imports
ALTER TABLE ONLY custom_emoji
ADD CONSTRAINT custom_emoji_pkey PRIMARY KEY (id);
ALTER TABLE ONLY customer_relations_organizations
ADD CONSTRAINT customer_relations_organizations_pkey PRIMARY KEY (id);
ALTER TABLE ONLY dast_profile_schedules
ADD CONSTRAINT dast_profile_schedules_pkey PRIMARY KEY (id);
......@@ -23557,6 +23584,8 @@ CREATE INDEX index_custom_emoji_on_creator_id ON custom_emoji USING btree (creat
CREATE UNIQUE INDEX index_custom_emoji_on_namespace_id_and_name ON custom_emoji USING btree (namespace_id, name);
CREATE UNIQUE INDEX index_customer_relations_organizations_on_unique_name_per_group ON customer_relations_organizations USING btree (group_id, lower(name));
CREATE UNIQUE INDEX index_cycle_analytics_stage_event_hashes_on_hash_sha_256 ON analytics_cycle_analytics_stage_event_hashes USING btree (hash_sha256);
CREATE UNIQUE INDEX index_daily_build_group_report_results_unique_columns ON ci_daily_build_group_report_results USING btree (project_id, ref_path, date, group_name);
......@@ -27801,6 +27830,9 @@ ALTER TABLE ONLY jira_connect_subscriptions
ALTER TABLE ONLY fork_network_members
ADD CONSTRAINT fk_rails_a40860a1ca FOREIGN KEY (fork_network_id) REFERENCES fork_networks(id) ON DELETE CASCADE;
ALTER TABLE ONLY customer_relations_organizations
ADD CONSTRAINT fk_rails_a48597902f FOREIGN KEY (group_id) REFERENCES namespaces(id) ON DELETE CASCADE;
ALTER TABLE ONLY operations_feature_flag_scopes
ADD CONSTRAINT fk_rails_a50a04d0a4 FOREIGN KEY (feature_flag_id) REFERENCES operations_feature_flags(id) ON DELETE CASCADE;
# frozen_string_literal: true
FactoryBot.define do
factory :organization, class: 'CustomerRelations::Organization' do
group
name { generate(:name) }
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe CustomerRelations::Organization, type: :model do
describe 'associations' do
it { is_expected.to belong_to(:group).with_foreign_key('group_id') }
end
describe 'validations' do
subject { create(:organization) }
it { is_expected.to validate_presence_of(:group) }
it { is_expected.to validate_presence_of(:name) }
it { is_expected.to validate_uniqueness_of(:name).case_insensitive.scoped_to([:group_id]) }
it { is_expected.to validate_length_of(:name).is_at_most(255) }
it { is_expected.to validate_length_of(:description).is_at_most(1024) }
end
describe '#name' do
it 'strips name' do
organization = described_class.new(name: ' GitLab ')
organization.valid?
expect(organization.name).to eq('GitLab')
end
end
describe '#find_by_name' do
let!(:group) { create(:group) }
let!(:organiztion1) { create(:organization, group: group, name: 'Test') }
let!(:organiztion2) { create(:organization, group: create(:group), name: 'Test') }
it 'strips name' do
expect(described_class.find_by_name(group.id, 'TEST')).to eq([organiztion1])
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