Commit 05c5fc38 authored by Andreas Brandl's avatar Andreas Brandl

Merge branch '338002-add-work-item-type-db-fixtures' into 'master'

Adds DB fixtures to create base work item types

See merge request gitlab-org/gitlab!69021
parents 2dca2163 e005e57c
...@@ -9,14 +9,18 @@ class WorkItem::Type < ApplicationRecord ...@@ -9,14 +9,18 @@ class WorkItem::Type < ApplicationRecord
include CacheMarkdownField include CacheMarkdownField
# Base types need to exist on the DB on app startup
# This constant is used by the DB seeder
BASE_TYPES = {
issue: { name: 'Issue', icon_name: 'issue-type-issue', enum_value: 0 },
incident: { name: 'Incident', icon_name: 'issue-type-incident', enum_value: 1 },
test_case: { name: 'Test Case', icon_name: 'issue-type-test-case', enum_value: 2 }, ## EE-only
requirement: { name: 'Requirement', icon_name: 'issue-type-requirements', enum_value: 3 } ## EE-only
}.freeze
cache_markdown_field :description, pipeline: :single_line cache_markdown_field :description, pipeline: :single_line
enum base_type: { enum base_type: BASE_TYPES.transform_values { |value| value[:enum_value] }
issue: 0,
incident: 1,
test_case: 2, ## EE-only
requirement: 3 ## EE-only
}
belongs_to :namespace, optional: true belongs_to :namespace, optional: true
has_many :work_items, class_name: 'Issue', foreign_key: :work_item_type_id, inverse_of: :work_item_type has_many :work_items, class_name: 'Issue', foreign_key: :work_item_type_id, inverse_of: :work_item_type
......
# frozen_string_literal: true
Gitlab::Seeder.quiet do
Gitlab::DatabaseImporters::WorkItems::BaseTypeImporter.import
end
# frozen_string_literal: true
Gitlab::Seeder.quiet do
Gitlab::DatabaseImporters::WorkItems::BaseTypeImporter.import
end
# frozen_string_literal: true
class UpsertBaseWorkItemTypes < ActiveRecord::Migration[6.1]
module WorkItem
class Type < ActiveRecord::Base
self.table_name = 'work_item_types'
enum base_type: {
issue: 0,
incident: 1,
test_case: 2,
requirement: 3
}
end
end
def up
# upsert default types
WorkItem::Type.find_or_create_by(name: 'Issue', namespace_id: nil, base_type: :issue, icon_name: 'issue-type-issue')
WorkItem::Type.find_or_create_by(name: 'Incident', namespace_id: nil, base_type: :incident, icon_name: 'issue-type-incident')
WorkItem::Type.find_or_create_by(name: 'Test Case', namespace_id: nil, base_type: :test_case, icon_name: 'issue-type-test-case')
WorkItem::Type.find_or_create_by(name: 'Requirement', namespace_id: nil, base_type: :requirement, icon_name: 'issue-type-requirements')
end
def down
# We expect this table to be empty at the point of the up migration,
# however there is a remote possibility that issues could already be
# using one of these types, with a tight foreign constraint.
# Therefore we will not attempt to remove any data.
end
end
50a06a2a57ed26c25af53d3d7f6f5ef73efde8a23a36c5f825af56b4d0c4c3f6
\ No newline at end of file
# frozen_string_literal: true
module Gitlab
module DatabaseImporters
module WorkItems
module BaseTypeImporter
def self.import
WorkItem::Type::BASE_TYPES.each do |type, attributes|
WorkItem::Type.create!(base_type: type, **attributes.slice(:name, :icon_name))
end
end
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'Create base work item types in development' do
subject { load Rails.root.join('db', 'fixtures', 'development', '001_create_base_work_item_types.rb') }
it_behaves_like 'work item base types importer'
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'Create base work item types in production' do
subject { load Rails.root.join('db', 'fixtures', 'production', '003_create_base_work_item_types.rb') }
it_behaves_like 'work item base types importer'
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::DatabaseImporters::WorkItems::BaseTypeImporter do
subject { described_class.import }
it_behaves_like 'work item base types importer'
end
# frozen_string_literal: true
require 'spec_helper'
require_migration!('upsert_base_work_item_types')
RSpec.describe UpsertBaseWorkItemTypes, :migration do
let!(:work_item_types) { table(:work_item_types) }
context 'when no default types exist' do
it 'creates default data' do
expect(work_item_types.count).to eq(0)
reversible_migration do |migration|
migration.before -> {
# Depending on whether the migration has been run before,
# the size could be 4, or 0, so we don't set any expectations
# as we don't delete base types on migration reverse
}
migration.after -> {
expect(work_item_types.count).to eq(4)
expect(work_item_types.all.pluck(:base_type)).to match_array(WorkItem::Type.base_types.values)
}
end
end
end
context 'when default types already exist' do
before do
Gitlab::DatabaseImporters::WorkItems::BaseTypeImporter.import
end
it 'does not create default types again' do
expect(work_item_types.all.pluck(:base_type)).to match_array(WorkItem::Type.base_types.values)
reversible_migration do |migration|
migration.before -> {
expect(work_item_types.all.pluck(:base_type)).to match_array(WorkItem::Type.base_types.values)
}
migration.after -> {
expect(work_item_types.count).to eq(4)
expect(work_item_types.all.pluck(:base_type)).to match_array(WorkItem::Type.base_types.values)
}
end
end
end
end
# frozen_string_literal: true
RSpec.shared_examples 'work item base types importer' do
it 'creates all base work item types' do
expect { subject }.to change(WorkItem::Type, :count).from(0).to(WorkItem::Type::BASE_TYPES.count)
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