Add DB indices to histories and tickets tables (fixes #2368)
This commit is contained in:
parent
8c1ae9fe9d
commit
12fe223664
6 changed files with 97 additions and 7 deletions
|
@ -408,6 +408,9 @@ class CreateBase < ActiveRecord::Migration[4.2]
|
|||
add_index :histories, [:id_from]
|
||||
add_index :histories, [:value_from], length: 255
|
||||
add_index :histories, [:value_to], length: 255
|
||||
add_index :histories, [:related_o_id]
|
||||
add_index :histories, [:related_history_object_id]
|
||||
add_index :histories, %i[o_id history_object_id related_o_id]
|
||||
add_foreign_key :histories, :history_types
|
||||
add_foreign_key :histories, :history_objects
|
||||
add_foreign_key :histories, :history_attributes
|
||||
|
|
|
@ -111,6 +111,8 @@ class CreateTicket < ActiveRecord::Migration[4.2]
|
|||
add_index :tickets, [:pending_time]
|
||||
add_index :tickets, [:type]
|
||||
add_index :tickets, [:time_unit]
|
||||
add_index :tickets, %i[group_id state_id]
|
||||
add_index :tickets, %i[group_id state_id owner_id]
|
||||
add_foreign_key :tickets, :groups
|
||||
add_foreign_key :tickets, :users, column: :owner_id
|
||||
add_foreign_key :tickets, :users, column: :customer_id
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
class Issue2368AddIndicesToHistoriesAndTickets < ActiveRecord::Migration[5.1]
|
||||
def up
|
||||
return if !Setting.find_by(name: 'system_init_done')
|
||||
|
||||
add_index :histories, :related_o_id if !index_exists?(:histories, :related_o_id)
|
||||
add_index :histories, :related_history_object_id if !index_exists?(:histories, :related_history_object_id)
|
||||
add_index :histories, %i[o_id history_object_id related_o_id] if !index_exists?(:histories, %i[o_id history_object_id related_o_id])
|
||||
add_index :tickets, %i[group_id state_id] if !index_exists?(:tickets, %i[group_id state_id])
|
||||
add_index :tickets, %i[group_id state_id owner_id] if !index_exists?(:tickets, %i[group_id state_id owner_id])
|
||||
end
|
||||
end
|
|
@ -9,7 +9,7 @@ RSpec.describe Issue1977RemoveInvalidUserForeignKeys, type: :db_migration do
|
|||
context 'invalid User foreign key columns' do
|
||||
|
||||
it 'cleans up OnlineNotification#user_id', db_strategy: :reset do
|
||||
witout_foreign_key(:online_notifications, column: :user_id)
|
||||
without_foreign_key(:online_notifications, column: :user_id)
|
||||
|
||||
create(:online_notification, user_id: 1337)
|
||||
valid = create(:online_notification, user_id: existing_user_id)
|
||||
|
@ -22,8 +22,8 @@ RSpec.describe Issue1977RemoveInvalidUserForeignKeys, type: :db_migration do
|
|||
end
|
||||
|
||||
it 'cleans up RecentView#created_by_id', db_strategy: :reset do
|
||||
witout_foreign_key(:online_notifications, column: :user_id)
|
||||
witout_foreign_key(:recent_views, column: :created_by_id)
|
||||
without_foreign_key(:online_notifications, column: :user_id)
|
||||
without_foreign_key(:recent_views, column: :created_by_id)
|
||||
|
||||
create(:recent_view, created_by_id: 1337)
|
||||
valid = create(:recent_view, created_by_id: existing_user_id)
|
||||
|
@ -36,7 +36,7 @@ RSpec.describe Issue1977RemoveInvalidUserForeignKeys, type: :db_migration do
|
|||
end
|
||||
|
||||
it 'cleans up Avatar#o_id', db_strategy: :reset do
|
||||
witout_foreign_key(:online_notifications, column: :user_id)
|
||||
without_foreign_key(:online_notifications, column: :user_id)
|
||||
|
||||
create(:avatar, object_lookup_id: ObjectLookup.by_name('User'), o_id: 1337)
|
||||
valid_ticket = create(:avatar, object_lookup_id: ObjectLookup.by_name('Ticket'), o_id: 1337)
|
||||
|
|
|
@ -0,0 +1,55 @@
|
|||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Issue2368AddIndicesToHistoriesAndTickets, type: :db_migration do
|
||||
self.use_transactional_tests = false # see comments on #without_index method
|
||||
|
||||
before { without_index(table, column: columns) }
|
||||
|
||||
context 'for histories table' do
|
||||
let(:table) { :histories }
|
||||
|
||||
context 'and related_o_id column' do
|
||||
let(:columns) { %i[related_o_id] }
|
||||
|
||||
it 'adds an index' do
|
||||
expect { migrate }.to change { index_exists?(table, columns) }.to(true)
|
||||
end
|
||||
end
|
||||
|
||||
context 'and related_history_object_id column' do
|
||||
let(:columns) { %i[related_history_object_id] }
|
||||
|
||||
it 'adds an index' do
|
||||
expect { migrate }.to change { index_exists?(table, columns) }.to(true)
|
||||
end
|
||||
end
|
||||
|
||||
context 'and o_id, history_object_id, & related_o_id columns' do
|
||||
let(:columns) { %i[o_id history_object_id related_o_id] }
|
||||
|
||||
it 'adds a composite index' do
|
||||
expect { migrate }.to change { index_exists?(table, columns) }.to(true)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'for tickets table' do
|
||||
let(:table) { :tickets }
|
||||
|
||||
context 'and group_id & state_id columns' do
|
||||
let(:columns) { %i[group_id state_id] }
|
||||
|
||||
it 'adds a composite index' do
|
||||
expect { migrate }.to change { index_exists?(table, columns) }.to(true)
|
||||
end
|
||||
end
|
||||
|
||||
context 'and group_id, state_id, & owner_id columns' do
|
||||
let(:columns) { %i[group_id state_id owner_id] }
|
||||
|
||||
it 'adds a composite index' do
|
||||
expect { migrate }.to change { index_exists?(table, columns) }.to(true)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -42,10 +42,10 @@ module DbMigrationHelper
|
|||
# @param [Symbol] column the name of the foreign_key column
|
||||
#
|
||||
# @example
|
||||
# witout_foreign_key(:online_notifications, column: :user_id)
|
||||
# without_foreign_key(:online_notifications, column: :user_id)
|
||||
#
|
||||
# @return [nil]
|
||||
def witout_foreign_key(from_table, column:)
|
||||
def without_foreign_key(from_table, column:)
|
||||
suppress_messages do
|
||||
break if !foreign_key_exists?(from_table, column: column)
|
||||
|
||||
|
@ -53,6 +53,25 @@ module DbMigrationHelper
|
|||
end
|
||||
end
|
||||
|
||||
# Helper method for setting up specs on DB migrations that add indices.
|
||||
# Make sure to define type: :db_migration in your RSpec.describe call
|
||||
# and add `self.use_transactional_tests = false` to your context.
|
||||
#
|
||||
# @param [Symbol] from_table the name of the table with the indexed column
|
||||
# @param [Symbol] name(s) of indexed column(s)
|
||||
#
|
||||
# @example
|
||||
# without_index(:online_notifications, column: :user_id)
|
||||
#
|
||||
# @return [nil]
|
||||
def without_index(from_table, column:)
|
||||
suppress_messages do
|
||||
break if !index_exists?(from_table, column)
|
||||
|
||||
remove_index(from_table, column: column)
|
||||
end
|
||||
end
|
||||
|
||||
# Enables the usage of `ActiveRecord::Migration` methods.
|
||||
#
|
||||
# @see ActiveRecord::Migration
|
||||
|
@ -91,7 +110,7 @@ module DbMigrationHelper
|
|||
#
|
||||
# @return [nil]
|
||||
def adds_foreign_key(from_table, column:)
|
||||
witout_foreign_key(from_table, column: column)
|
||||
without_foreign_key(from_table, column: column)
|
||||
|
||||
suppress_messages do
|
||||
expect do
|
||||
|
|
Loading…
Reference in a new issue