From c38c4e62268ed538a52f654647e42b51e00f6200 Mon Sep 17 00:00:00 2001 From: Rolf Schmidt Date: Wed, 25 May 2022 15:48:32 +0200 Subject: [PATCH] Fixes #4097 - Latest upgrade to 5.1 is not possible. --- ...524074118_issue4089_fix_draft_attribute.rb | 48 ++++++++++++++++++- .../issue_4089_fix_draft_attribute_spec.rb | 32 +++++++++++++ 2 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 spec/db/migrate/issue_4089_fix_draft_attribute_spec.rb diff --git a/db/migrate/20220524074118_issue4089_fix_draft_attribute.rb b/db/migrate/20220524074118_issue4089_fix_draft_attribute.rb index bedc5fcc4..752f77095 100644 --- a/db/migrate/20220524074118_issue4089_fix_draft_attribute.rb +++ b/db/migrate/20220524074118_issue4089_fix_draft_attribute.rb @@ -5,6 +5,52 @@ class Issue4089FixDraftAttribute < ActiveRecord::Migration[5.0] # return if it's a new setup return if !Setting.exists?(name: 'system_init_done') - ObjectManager::Attribute.find_by(name: 'shared_drafts', object_lookup_id: ObjectLookup.by_name('Group')).update(editable: false) + field = ObjectManager::Attribute.find_by(name: 'shared_drafts', object_lookup_id: ObjectLookup.by_name('Group')) + if !field + add_field + return + end + + field.update(editable: false) + end + + def add_field + UserInfo.current_user_id = 1 + ObjectManager::Attribute.add( + force: true, + object: 'Group', + name: 'shared_drafts', + display: 'Shared Drafts', + data_type: 'active', + data_option: { + null: false, + default: true, + permission: ['admin.group'], + }, + editable: false, + active: true, + screens: { + create: { + '-all-' => { + null: true, + }, + }, + edit: { + '-all-': { + null: false, + }, + }, + view: { + '-all-' => { + shown: false, + }, + }, + }, + to_create: false, + to_migrate: false, + to_delete: false, + position: 1400, + ) + ObjectManager::Attribute.migration_execute end end diff --git a/spec/db/migrate/issue_4089_fix_draft_attribute_spec.rb b/spec/db/migrate/issue_4089_fix_draft_attribute_spec.rb new file mode 100644 index 000000000..3e23ae48c --- /dev/null +++ b/spec/db/migrate/issue_4089_fix_draft_attribute_spec.rb @@ -0,0 +1,32 @@ +# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/ + +require 'rails_helper' + +RSpec.describe Issue4089FixDraftAttribute, type: :db_migration do + def field + ObjectManager::Attribute.find_by(name: 'shared_drafts', object_lookup_id: ObjectLookup.by_name('Group')) + end + + context 'when field does not exist', db_strategy: :reset do + before do + field.destroy + ObjectManager::Attribute.migration_execute + migrate + end + + it 'does create the field and set it not editable' do + expect(field.reload.editable).to be(false) + end + end + + context 'when field does exist' do + before do + field.update(editable: true) + migrate + end + + it 'does set the field to not editable' do + expect(field.reload.editable).to be(false) + end + end +end