|
| 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 | + before do |
| 10 | + skip unless db.database_type == :postgres |
| 11 | + end |
| 12 | + |
| 13 | + describe 'up migration' do |
| 14 | + context 'index does not exist' do |
| 15 | + it 'adds the index' do |
| 16 | + expect(db.indexes(table_sym)).not_to include(index_sym) |
| 17 | + expect { Sequel::Migrator.run(db, migrations_path, target: current_migration_index, allow_missing_migration_files: true) }.not_to raise_error |
| 18 | + expect(db.indexes(table_sym)).to include(index_sym) |
| 19 | + end |
| 20 | + end |
| 21 | + |
| 22 | + context 'index already exists' do |
| 23 | + before do |
| 24 | + db.add_index table_sym, :user_id, name: index_sym |
| 25 | + end |
| 26 | + |
| 27 | + it 'does not fail' do |
| 28 | + expect(db.indexes(table_sym)).to include(index_sym) |
| 29 | + expect { Sequel::Migrator.run(db, migrations_path, target: current_migration_index, allow_missing_migration_files: true) }.not_to raise_error |
| 30 | + expect(db.indexes(table_sym)).to include(index_sym) |
| 31 | + end |
| 32 | + end |
| 33 | + end |
| 34 | + |
| 35 | + describe 'down migration' do |
| 36 | + before do |
| 37 | + Sequel::Migrator.run(db, migrations_path, target: current_migration_index, allow_missing_migration_files: true) |
| 38 | + end |
| 39 | + |
| 40 | + context 'index exists' do |
| 41 | + it 'removes the index' do |
| 42 | + expect(db.indexes(table_sym)).to include(index_sym) |
| 43 | + expect { Sequel::Migrator.run(db, migrations_path, target: current_migration_index - 1, allow_missing_migration_files: true) }.not_to raise_error |
| 44 | + expect(db.indexes(table_sym)).not_to include(index_sym) |
| 45 | + end |
| 46 | + end |
| 47 | + |
| 48 | + context 'index does not exist' do |
| 49 | + before do |
| 50 | + db.drop_index table_sym, :user_id, name: index_sym |
| 51 | + end |
| 52 | + |
| 53 | + it 'does not fail' do |
| 54 | + expect(db.indexes(table_sym)).not_to include(index_sym) |
| 55 | + expect { Sequel::Migrator.run(db, migrations_path, target: current_migration_index - 1, allow_missing_migration_files: true) }.not_to raise_error |
| 56 | + expect(db.indexes(table_sym)).not_to include(index_sym) |
| 57 | + end |
| 58 | + end |
| 59 | + end |
| 60 | + end |
| 61 | +end |
| 62 | + |
| 63 | +RSpec.describe 'migration to add an index for user_id on all roles tables', isolation: :truncation, type: :migration do |
| 64 | + include_context 'migration' do |
| 65 | + let(:migration_filename) { '20250318112800_add_user_id_index_to_roles_tables.rb' } |
| 66 | + end |
| 67 | + |
| 68 | + include_examples 'adding an index for table', 'organizations_auditors' |
| 69 | + include_examples 'adding an index for table', 'organizations_billing_managers' |
| 70 | + include_examples 'adding an index for table', 'organizations_managers' |
| 71 | + include_examples 'adding an index for table', 'organizations_users' |
| 72 | + include_examples 'adding an index for table', 'spaces_auditors' |
| 73 | + include_examples 'adding an index for table', 'spaces_developers' |
| 74 | + include_examples 'adding an index for table', 'spaces_managers' |
| 75 | + include_examples 'adding an index for table', 'spaces_supporters' |
| 76 | +end |
0 commit comments