From 8274bb8a11c6c3edeb049f0e0dcb8c091f9ad6a6 Mon Sep 17 00:00:00 2001 From: Rolf Schmidt Date: Wed, 6 Apr 2022 15:56:43 +0200 Subject: [PATCH] Fixes #4049 - Can't delete smime certificate - ERROR: duplicate key value violates unique constraint. --- ...20220405133444_issue4049_fix_object_lookup.rb | 10 ++++++++++ .../migrate/issue_4049_fix_object_lookup_spec.rb | 16 ++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 db/migrate/20220405133444_issue4049_fix_object_lookup.rb create mode 100644 spec/db/migrate/issue_4049_fix_object_lookup_spec.rb diff --git a/db/migrate/20220405133444_issue4049_fix_object_lookup.rb b/db/migrate/20220405133444_issue4049_fix_object_lookup.rb new file mode 100644 index 000000000..4e4df7496 --- /dev/null +++ b/db/migrate/20220405133444_issue4049_fix_object_lookup.rb @@ -0,0 +1,10 @@ +# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/ + +class Issue4049FixObjectLookup < ActiveRecord::Migration[6.0] + def change + # return if it's a new setup + return if !Setting.exists?(name: 'system_init_done') + + ObjectLookup.find_by(name: 'SmimeCertificate')&.update(name: 'SMIMECertificate') + end +end diff --git a/spec/db/migrate/issue_4049_fix_object_lookup_spec.rb b/spec/db/migrate/issue_4049_fix_object_lookup_spec.rb new file mode 100644 index 000000000..0297656b6 --- /dev/null +++ b/spec/db/migrate/issue_4049_fix_object_lookup_spec.rb @@ -0,0 +1,16 @@ +# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/ + +require 'rails_helper' + +RSpec.describe Issue4049FixObjectLookup, type: :db_migration do + before do + # create and update to wrong state in pre release + ObjectLookup.by_name('SMIMECertificate') + ObjectLookup.find_by(name: 'SMIMECertificate').update(name: 'SmimeCertificate') + end + + it 'does fix the broken object lookup' do + migrate + expect(ObjectLookup.by_name('SMIMECertificate')).not_to be_nil + end +end