From acd98bd21458df15ae984d8dde6da88bd7c18a06 Mon Sep 17 00:00:00 2001 From: Thorsten Eckel Date: Wed, 11 Nov 2020 14:53:54 +0100 Subject: [PATCH] Maintenance: Removed incomplete/obsolete TextModules and Templates references implementation to single User. --- app/models/template.rb | 2 -- app/models/text_module.rb | 2 -- app/models/user.rb | 2 -- db/migrate/20120101000010_create_ticket.rb | 6 ------ .../20201111133905_maintenance_associations_cleanup.rb | 10 ++++++++++ spec/models/user_spec.rb | 8 ++------ 6 files changed, 12 insertions(+), 18 deletions(-) create mode 100644 db/migrate/20201111133905_maintenance_associations_cleanup.rb diff --git a/app/models/template.rb b/app/models/template.rb index 632f9f6f2..53b00f1a0 100644 --- a/app/models/template.rb +++ b/app/models/template.rb @@ -3,8 +3,6 @@ class Template < ApplicationModel include ChecksClientNotification - belongs_to :user, optional: true - store :options validates :name, presence: true diff --git a/app/models/text_module.rb b/app/models/text_module.rb index 4a436ae41..024b82850 100644 --- a/app/models/text_module.rb +++ b/app/models/text_module.rb @@ -5,8 +5,6 @@ class TextModule < ApplicationModel include ChecksHtmlSanitized include CanCsvImport - belongs_to :user, optional: true - validates :name, presence: true validates :content, presence: true diff --git a/app/models/user.rb b/app/models/user.rb index 2ee0e3720..86125887f 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -25,7 +25,6 @@ class User < ApplicationModel has_many :tokens, after_add: :cache_update, after_remove: :cache_update, dependent: :destroy has_many :authorizations, after_add: :cache_update, after_remove: :cache_update, dependent: :destroy has_many :online_notifications, dependent: :destroy - has_many :templates, dependent: :destroy has_many :taskbars, dependent: :destroy has_many :user_devices, dependent: :destroy has_one :chat_agent_created_by, class_name: 'Chat::Agent', foreign_key: :created_by_id, dependent: :destroy, inverse_of: :created_by @@ -34,7 +33,6 @@ class User < ApplicationModel has_many :karma_user, class_name: 'Karma::User', dependent: :destroy has_many :karma_activity_logs, class_name: 'Karma::ActivityLog', dependent: :destroy has_many :cti_caller_ids, class_name: 'Cti::CallerId', dependent: :destroy - has_many :text_modules, dependent: :destroy has_many :customer_tickets, class_name: 'Ticket', foreign_key: :customer_id, dependent: :destroy, inverse_of: :customer has_many :owner_tickets, class_name: 'Ticket', foreign_key: :owner_id, inverse_of: :owner has_many :created_recent_views, class_name: 'RecentView', foreign_key: :created_by_id, dependent: :destroy, inverse_of: :created_by diff --git a/db/migrate/20120101000010_create_ticket.rb b/db/migrate/20120101000010_create_ticket.rb index 6c27ae1fc..a661222d9 100644 --- a/db/migrate/20120101000010_create_ticket.rb +++ b/db/migrate/20120101000010_create_ticket.rb @@ -378,7 +378,6 @@ class CreateTicket < ActiveRecord::Migration[4.2] add_foreign_key :postmaster_filters, :users, column: :updated_by_id create_table :text_modules do |t| - t.references :user, null: true t.column :name, :string, limit: 250, null: false t.column :keywords, :string, limit: 500, null: true t.column :content, :text, limit: 10.megabytes + 1, null: false @@ -389,9 +388,7 @@ class CreateTicket < ActiveRecord::Migration[4.2] t.column :created_by_id, :integer, null: false t.timestamps limit: 3, null: false end - add_index :text_modules, [:user_id] add_index :text_modules, [:name] - add_foreign_key :text_modules, :users add_foreign_key :text_modules, :users, column: :created_by_id add_foreign_key :text_modules, :users, column: :updated_by_id @@ -405,16 +402,13 @@ class CreateTicket < ActiveRecord::Migration[4.2] add_foreign_key :text_modules_groups, :groups create_table :templates do |t| - t.references :user, null: true t.column :name, :string, limit: 250, null: false t.column :options, :text, limit: 10.megabytes + 1, null: false t.column :updated_by_id, :integer, null: false t.column :created_by_id, :integer, null: false t.timestamps limit: 3, null: false end - add_index :templates, [:user_id] add_index :templates, [:name] - add_foreign_key :templates, :users add_foreign_key :templates, :users, column: :created_by_id add_foreign_key :templates, :users, column: :updated_by_id diff --git a/db/migrate/20201111133905_maintenance_associations_cleanup.rb b/db/migrate/20201111133905_maintenance_associations_cleanup.rb new file mode 100644 index 000000000..f3428ade2 --- /dev/null +++ b/db/migrate/20201111133905_maintenance_associations_cleanup.rb @@ -0,0 +1,10 @@ +class MaintenanceAssociationsCleanup < ActiveRecord::Migration[5.2] + def change + + # return if it's a new setup + return if !Setting.exists?(name: 'system_init_done') + + remove_reference :templates, :user, index: true, foreign_key: true + remove_reference :text_modules, :user, index: true, foreign_key: true + end +end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 7627d3496..bde1a3711 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -835,7 +835,7 @@ RSpec.describe User, type: :model do 'OnlineNotification' => { 'user_id' => 1, 'created_by_id' => 0, 'updated_by_id' => 0 }, 'Ticket' => { 'created_by_id' => 0, 'updated_by_id' => 0, 'owner_id' => 1, 'customer_id' => 3 }, - 'Template' => { 'user_id' => 1, 'created_by_id' => 0, 'updated_by_id' => 0 }, + 'Template' => { 'created_by_id' => 0, 'updated_by_id' => 0 }, 'Avatar' => { 'created_by_id' => 0, 'updated_by_id' => 0 }, 'Scheduler' => { 'created_by_id' => 0, 'updated_by_id' => 0 }, 'Chat' => { 'created_by_id' => 0, 'updated_by_id' => 0 }, @@ -873,7 +873,7 @@ RSpec.describe User, type: :model do 'Overview' => { 'created_by_id' => 1, 'updated_by_id' => 0 }, 'ActivityStream' => { 'created_by_id' => 0 }, 'StatsStore' => { 'created_by_id' => 0 }, - 'TextModule' => { 'user_id' => 1, 'created_by_id' => 0, 'updated_by_id' => 0 }, + 'TextModule' => { 'created_by_id' => 0, 'updated_by_id' => 0 }, 'Calendar' => { 'created_by_id' => 0, 'updated_by_id' => 0 }, 'UserGroup' => { 'user_id' => 1 }, 'Signature' => { 'created_by_id' => 0, 'updated_by_id' => 0 }, @@ -882,12 +882,10 @@ RSpec.describe User, type: :model do # delete objects token = create(:token, user: user) online_notification = create(:online_notification, user: user) - template = create(:template, :dummy_data, user: user) taskbar = create(:taskbar, user: user) user_device = create(:user_device, user: user) karma_activity_log = create(:karma_activity_log, user: user) cti_caller_id = create(:cti_caller_id, user: user) - text_module = create(:text_module, user: user) authorization = create(:twitter_authorization, user: user) recent_view = create(:recent_view, created_by: user) avatar = create(:avatar, o_id: user.id) @@ -918,12 +916,10 @@ RSpec.describe User, type: :model do expect { token.reload }.to raise_exception(ActiveRecord::RecordNotFound) expect { online_notification.reload }.to raise_exception(ActiveRecord::RecordNotFound) - expect { template.reload }.to raise_exception(ActiveRecord::RecordNotFound) expect { taskbar.reload }.to raise_exception(ActiveRecord::RecordNotFound) expect { user_device.reload }.to raise_exception(ActiveRecord::RecordNotFound) expect { karma_activity_log.reload }.to raise_exception(ActiveRecord::RecordNotFound) expect { cti_caller_id.reload }.to raise_exception(ActiveRecord::RecordNotFound) - expect { text_module.reload }.to raise_exception(ActiveRecord::RecordNotFound) expect { authorization.reload }.to raise_exception(ActiveRecord::RecordNotFound) expect { recent_view.reload }.to raise_exception(ActiveRecord::RecordNotFound) expect { avatar.reload }.to raise_exception(ActiveRecord::RecordNotFound)