diff --git a/app/models/project.rb b/app/models/project.rb index e55a57e46ef..f6218519533 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -1068,7 +1068,7 @@ class Project < ActiveRecord::Base # rubocop: disable CodeReuse/ServiceClass def create_labels Label.templates.each do |label| - params = label.attributes.except('id', 'template', 'created_at', 'updated_at') + params = label.attributes.except('id', 'template', 'created_at', 'updated_at', 'type') Labels::FindOrCreateService.new(nil, self, params).execute(skip_authorization: true) end end diff --git a/changelogs/unreleased/issue_55744.yml b/changelogs/unreleased/issue_55744.yml new file mode 100644 index 00000000000..6a643732b18 --- /dev/null +++ b/changelogs/unreleased/issue_55744.yml @@ -0,0 +1,5 @@ +--- +title: Fix template labels not being created on new projects +merge_request: 24803 +author: +type: fixed diff --git a/db/post_migrate/20190131122559_fix_null_type_labels.rb b/db/post_migrate/20190131122559_fix_null_type_labels.rb new file mode 100644 index 00000000000..83bb613990c --- /dev/null +++ b/db/post_migrate/20190131122559_fix_null_type_labels.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +class FixNullTypeLabels < ActiveRecord::Migration[5.0] + include Gitlab::Database::MigrationHelpers + + DOWNTIME = false + + disable_ddl_transaction! + + def up + update_column_in_batches(:labels, :type, 'ProjectLabel') do |table, query| + query.where( + table[:project_id].not_eq(nil) + .and(table[:template].eq(false)) + .and(table[:type].eq(nil)) + ) + end + end + + def down + # no action + end +end diff --git a/db/schema.rb b/db/schema.rb index 7c1733becb9..4cc870ba6c8 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20190124200344) do +ActiveRecord::Schema.define(version: 20190131122559) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" diff --git a/spec/migrations/fix_null_type_labels_spec.rb b/spec/migrations/fix_null_type_labels_spec.rb new file mode 100644 index 00000000000..462ae9b913f --- /dev/null +++ b/spec/migrations/fix_null_type_labels_spec.rb @@ -0,0 +1,36 @@ +# frozen_string_literal: true + +require 'spec_helper' +require Rails.root.join('db', 'post_migrate', '20190131122559_fix_null_type_labels') + +describe FixNullTypeLabels, :migration do + let(:migration) { described_class.new } + let(:projects) { table(:projects) } + let(:namespaces) { table(:namespaces) } + let(:labels) { table(:labels) } + + before do + group = namespaces.create(name: 'labels-test-project', path: 'labels-test-project', type: 'Group') + project = projects.create!(namespace_id: group.id, name: 'labels-test-group', path: 'labels-test-group') + + @template_label = labels.create(title: 'template', template: true) + @project_label = labels.create(title: 'project label', project_id: project.id, type: 'ProjectLabel') + @group_label = labels.create(title: 'group_label', group_id: group.id, type: 'GroupLabel') + @broken_label_1 = labels.create(title: 'broken 1', project_id: project.id) + @broken_label_2 = labels.create(title: 'broken 2', project_id: project.id) + end + + describe '#up' do + it 'fix labels with type missing' do + migration.up + + # Labels that requires type change + expect(@broken_label_1.reload.type).to eq('ProjectLabel') + expect(@broken_label_2.reload.type).to eq('ProjectLabel') + # Labels out of scope + expect(@template_label.reload.type).to be_nil + expect(@project_label.reload.type).to eq('ProjectLabel') + expect(@group_label.reload.type).to eq('GroupLabel') + end + end +end diff --git a/spec/services/projects/create_service_spec.rb b/spec/services/projects/create_service_spec.rb index f71e2b4bc24..54ce33dd103 100644 --- a/spec/services/projects/create_service_spec.rb +++ b/spec/services/projects/create_service_spec.rb @@ -16,7 +16,11 @@ describe Projects::CreateService, '#execute' do Label.create(title: "bug", template: true) project = create_project(user, opts) - expect(project.labels).not_to be_empty + created_label = project.reload.labels.last + + expect(created_label.type).to eq('ProjectLabel') + expect(created_label.project_id).to eq(project.id) + expect(created_label.title).to eq('bug') end context 'user namespace' do