Skip to content

Commit 96db071

Browse files
committed
Add an index for user_id on all roles tables
Currently there is only a combined index (space/org_id + user_id) which cannot be used when accessing the table via user_id. Multiple role queries could benefit from a dedicated index on user_id.
1 parent 8d3cedf commit 96db071

2 files changed

Lines changed: 127 additions & 0 deletions

File tree

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
Sequel.migration do
2+
roles_tables = %w[
3+
organizations_auditors
4+
organizations_billing_managers
5+
organizations_managers
6+
organizations_users
7+
spaces_auditors
8+
spaces_developers
9+
spaces_managers
10+
spaces_supporters
11+
]
12+
13+
# adding an index concurrently cannot be done within a transaction
14+
no_transaction
15+
16+
up do
17+
roles_tables.each do |table|
18+
table_sym = table.to_sym
19+
index_sym = :"#{table}_user_id_index"
20+
21+
if database_type == :postgres
22+
VCAP::Migration.with_concurrent_timeout(self) do
23+
add_index table_sym, :user_id, name: index_sym, if_not_exists: true, concurrently: true
24+
end
25+
26+
elsif database_type == :mysql
27+
alter_table table_sym do
28+
# rubocop:disable Sequel/ConcurrentIndex
29+
add_index :user_id, name: index_sym unless @db.indexes(table_sym).include?(index_sym)
30+
# rubocop:enable Sequel/ConcurrentIndex
31+
end
32+
end
33+
end
34+
end
35+
36+
down do
37+
roles_tables.each do |table|
38+
table_sym = table.to_sym
39+
index_sym = :"#{table}_user_id_index"
40+
41+
if database_type == :postgres
42+
VCAP::Migration.with_concurrent_timeout(self) do
43+
drop_index table_sym, :user_id, name: index_sym, if_exists: true, concurrently: true
44+
end
45+
46+
elsif database_type == :mysql
47+
alter_table table_sym do
48+
# rubocop:disable Sequel/ConcurrentIndex
49+
drop_index :user_id, name: index_sym if @db.indexes(table_sym).include?(index_sym)
50+
# rubocop:enable Sequel/ConcurrentIndex
51+
end
52+
end
53+
end
54+
end
55+
end
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
require 'spec_helper'
2+
require 'migrations/helpers/migration_shared_context'
3+
4+
RSpec.shared_examples 'adding an index for table' do |table|
5+
describe "#{table} table" do
6+
let(:table_sym) { table.to_sym }
7+
let(:index_sym) { :"#{table}_user_id_index" }
8+
9+
describe 'up migration' do
10+
context 'index does not exist' do
11+
it 'adds the index' do
12+
expect(db.indexes(table_sym)).not_to include(index_sym)
13+
expect { Sequel::Migrator.run(db, migrations_path, target: current_migration_index, allow_missing_migration_files: true) }.not_to raise_error
14+
expect(db.indexes(table_sym)).to include(index_sym)
15+
end
16+
end
17+
18+
context 'index already exists' do
19+
before do
20+
db.add_index table_sym, :user_id, name: index_sym
21+
end
22+
23+
it 'does not fail' do
24+
expect(db.indexes(table_sym)).to include(index_sym)
25+
expect { Sequel::Migrator.run(db, migrations_path, target: current_migration_index, allow_missing_migration_files: true) }.not_to raise_error
26+
expect(db.indexes(table_sym)).to include(index_sym)
27+
end
28+
end
29+
end
30+
31+
describe 'down migration' do
32+
before do
33+
Sequel::Migrator.run(db, migrations_path, target: current_migration_index, allow_missing_migration_files: true)
34+
end
35+
36+
context 'index exists' do
37+
it 'removes the index' do
38+
expect(db.indexes(table_sym)).to include(index_sym)
39+
expect { Sequel::Migrator.run(db, migrations_path, target: current_migration_index - 1, allow_missing_migration_files: true) }.not_to raise_error
40+
expect(db.indexes(table_sym)).not_to include(index_sym)
41+
end
42+
end
43+
44+
context 'index does not exist' do
45+
before do
46+
db.drop_index table_sym, :user_id, name: index_sym
47+
end
48+
49+
it 'does not fail' do
50+
expect(db.indexes(table_sym)).not_to include(index_sym)
51+
expect { Sequel::Migrator.run(db, migrations_path, target: current_migration_index - 1, allow_missing_migration_files: true) }.not_to raise_error
52+
expect(db.indexes(table_sym)).not_to include(index_sym)
53+
end
54+
end
55+
end
56+
end
57+
end
58+
59+
RSpec.describe 'migration to add an index for user_id on all roles tables', isolation: :truncation, type: :migration do
60+
include_context 'migration' do
61+
let(:migration_filename) { '20250318112800_add_user_id_index_to_roles_tables.rb' }
62+
end
63+
64+
include_examples 'adding an index for table', 'organizations_auditors'
65+
include_examples 'adding an index for table', 'organizations_billing_managers'
66+
include_examples 'adding an index for table', 'organizations_managers'
67+
include_examples 'adding an index for table', 'organizations_users'
68+
include_examples 'adding an index for table', 'spaces_auditors'
69+
include_examples 'adding an index for table', 'spaces_developers'
70+
include_examples 'adding an index for table', 'spaces_managers'
71+
include_examples 'adding an index for table', 'spaces_supporters'
72+
end

0 commit comments

Comments
 (0)