|
| 1 | +require 'migrations/helpers/migration_shared_context' |
| 2 | +require 'database/bigint_migration' |
| 3 | + |
| 4 | +RSpec.shared_context 'bigint migration step3a' do |
| 5 | + subject(:run_migration_step1) { Sequel::Migrator.run(db, migrations_path, target: current_migration_index, allow_missing_migration_files: true) } |
| 6 | + |
| 7 | + subject(:run_migration_step3a) { Sequel::Migrator.run(db, migrations_path, target: current_migration_index_step3a, allow_missing_migration_files: true) } |
| 8 | + |
| 9 | + let(:migration_filename) { migration_filename_step1 } |
| 10 | + let(:current_migration_index_step3a) { migration_filename_step3a.match(/\A\d+/)[0].to_i } |
| 11 | + |
| 12 | + include_context 'migration' |
| 13 | + |
| 14 | + let(:skip_bigint_id_migration) { false } |
| 15 | + let(:logger) { double(:logger, info: nil) } |
| 16 | + |
| 17 | + before do |
| 18 | + skip unless db.database_type == :postgres |
| 19 | + |
| 20 | + allow_any_instance_of(VCAP::CloudController::Config).to receive(:get).with(:skip_bigint_id_migration).and_return(skip_bigint_id_migration) |
| 21 | + end |
| 22 | + |
| 23 | + describe 'up' do |
| 24 | + context 'when migration step 1 was executed' do |
| 25 | + context 'when the id_bigint column was added' do |
| 26 | + before do |
| 27 | + insert.call(db) |
| 28 | + run_migration_step1 |
| 29 | + end |
| 30 | + |
| 31 | + context 'when backfilling was completed' do |
| 32 | + before do |
| 33 | + VCAP::BigintMigration.backfill(logger, db, table) |
| 34 | + end |
| 35 | + |
| 36 | + it 'adds a check constraint' do |
| 37 | + expect(db).not_to have_table_with_check_constraint(table) |
| 38 | + |
| 39 | + run_migration_step3a |
| 40 | + |
| 41 | + expect(db).to have_table_with_check_constraint(table) |
| 42 | + end |
| 43 | + end |
| 44 | + |
| 45 | + context 'when backfilling was not completed' do |
| 46 | + after do |
| 47 | + db[table].delete # Necessary as the migration will be executed again in the after block of the migration shared context - and should not fail... |
| 48 | + end |
| 49 | + |
| 50 | + it 'fails ...' do |
| 51 | + expect do |
| 52 | + run_migration_step3a |
| 53 | + end.to raise_error(/Failed to add check constraint on 'events' table!/) |
| 54 | + end |
| 55 | + end |
| 56 | + end |
| 57 | + |
| 58 | + context "when the migration was concluded (id column's type switched)" do |
| 59 | + before do |
| 60 | + db[table].delete |
| 61 | + run_migration_step1 |
| 62 | + end |
| 63 | + |
| 64 | + it 'does not add a check constraint' do |
| 65 | + expect(db).not_to have_table_with_check_constraint(table) |
| 66 | + |
| 67 | + run_migration_step3a |
| 68 | + |
| 69 | + expect(db).not_to have_table_with_check_constraint(table) |
| 70 | + end |
| 71 | + end |
| 72 | + end |
| 73 | + |
| 74 | + context 'when migration step 1 was skipped' do |
| 75 | + let(:skip_bigint_id_migration) { true } |
| 76 | + |
| 77 | + before do |
| 78 | + run_migration_step1 |
| 79 | + end |
| 80 | + |
| 81 | + it 'does not add a check constraint' do |
| 82 | + expect(db).not_to have_table_with_check_constraint(table) |
| 83 | + |
| 84 | + run_migration_step3a |
| 85 | + |
| 86 | + expect(db).not_to have_table_with_check_constraint(table) |
| 87 | + end |
| 88 | + end |
| 89 | + end |
| 90 | + |
| 91 | + describe 'down' do |
| 92 | + subject(:run_rollback_step3a) { Sequel::Migrator.run(db, migrations_path, target: current_migration_index_step3a - 1, allow_missing_migration_files: true) } |
| 93 | + |
| 94 | + context 'when migration step 3a was executed' do |
| 95 | + before do |
| 96 | + insert.call(db) |
| 97 | + run_migration_step1 |
| 98 | + VCAP::BigintMigration.backfill(logger, db, table) |
| 99 | + run_migration_step3a |
| 100 | + end |
| 101 | + |
| 102 | + it 'drops the check constraint' do |
| 103 | + expect(db).to have_table_with_check_constraint(table) |
| 104 | + |
| 105 | + run_rollback_step3a |
| 106 | + |
| 107 | + expect(db).not_to have_table_with_check_constraint(table) |
| 108 | + end |
| 109 | + end |
| 110 | + end |
| 111 | +end |
| 112 | + |
| 113 | +RSpec.shared_context 'bigint migration step3b' do |
| 114 | + subject(:run_migration_step1) { Sequel::Migrator.run(db, migrations_path, target: current_migration_index, allow_missing_migration_files: true) } |
| 115 | + |
| 116 | + subject(:run_migration_step3a) { Sequel::Migrator.run(db, migrations_path, target: current_migration_index_step3a, allow_missing_migration_files: true) } |
| 117 | + |
| 118 | + subject(:run_migration_step3b) { Sequel::Migrator.run(db, migrations_path, target: current_migration_index_step3b, allow_missing_migration_files: true) } |
| 119 | + |
| 120 | + let(:migration_filename) { migration_filename_step1 } |
| 121 | + let(:current_migration_index_step3a) { migration_filename_step3a.match(/\A\d+/)[0].to_i } |
| 122 | + let(:current_migration_index_step3b) { migration_filename_step3b.match(/\A\d+/)[0].to_i } |
| 123 | + |
| 124 | + include_context 'migration' |
| 125 | + |
| 126 | + let(:skip_bigint_id_migration) { false } |
| 127 | + let(:logger) { double(:logger, info: nil) } |
| 128 | + |
| 129 | + before do |
| 130 | + skip unless db.database_type == :postgres |
| 131 | + |
| 132 | + allow_any_instance_of(VCAP::CloudController::Config).to receive(:get).with(:skip_bigint_id_migration).and_return(skip_bigint_id_migration) |
| 133 | + end |
| 134 | + |
| 135 | + describe 'up' do |
| 136 | + context 'when migration step 3a was executed' do |
| 137 | + before do |
| 138 | + insert.call(db) |
| 139 | + run_migration_step1 |
| 140 | + VCAP::BigintMigration.backfill(logger, db, table) |
| 141 | + run_migration_step3a |
| 142 | + end |
| 143 | + |
| 144 | + it 'drops the check constraint' do |
| 145 | + expect(db).to have_table_with_check_constraint(table) |
| 146 | + |
| 147 | + run_migration_step3b |
| 148 | + |
| 149 | + expect(db).not_to have_table_with_check_constraint(table) |
| 150 | + end |
| 151 | + |
| 152 | + it 'drops the trigger function' do |
| 153 | + expect(db).to have_trigger_function_for_table(table) |
| 154 | + |
| 155 | + run_migration_step3b |
| 156 | + |
| 157 | + expect(db).not_to have_trigger_function_for_table(table) |
| 158 | + end |
| 159 | + |
| 160 | + it 'drops the id primary key column' do |
| 161 | + expect(db).to have_table_with_column(table, :id) |
| 162 | + |
| 163 | + run_migration_step3b |
| 164 | + |
| 165 | + expect(db).not_to have_table_with_column(table, :id) |
| 166 | + end |
| 167 | + end |
| 168 | + end |
| 169 | + |
| 170 | + describe 'down' do |
| 171 | + subject(:run_rollback_step3b) { Sequel::Migrator.run(db, migrations_path, target: current_migration_index_step3b - 1, allow_missing_migration_files: true) } |
| 172 | + |
| 173 | + context 'when migration step 3b was executed' do |
| 174 | + before do |
| 175 | + insert.call(db) |
| 176 | + run_migration_step1 |
| 177 | + VCAP::BigintMigration.backfill(logger, db, table) |
| 178 | + run_migration_step3a |
| 179 | + run_migration_step3b |
| 180 | + end |
| 181 | + |
| 182 | + it 're-adds the id primary key column' do |
| 183 | + expect(db).not_to have_table_with_column(table, :id) |
| 184 | + |
| 185 | + run_rollback_step3b |
| 186 | + |
| 187 | + expect(db).to have_table_with_column(table, :id) |
| 188 | + end |
| 189 | + |
| 190 | + it 're-creates the trigger function' do |
| 191 | + expect(db).not_to have_trigger_function_for_table(table) |
| 192 | + |
| 193 | + run_rollback_step3b |
| 194 | + |
| 195 | + expect(db).to have_trigger_function_for_table(table) |
| 196 | + end |
| 197 | + |
| 198 | + it 're-adds the check constraint' do |
| 199 | + expect(db).not_to have_table_with_check_constraint(table) |
| 200 | + |
| 201 | + run_rollback_step3b |
| 202 | + |
| 203 | + expect(db).to have_table_with_check_constraint(table) |
| 204 | + end |
| 205 | + end |
| 206 | + end |
| 207 | +end |
0 commit comments