Skip to content

Commit 66ce805

Browse files
authored
Simplify migration-shared-context (#3749)
When errors are encountered during the setup of migration tests, especially when DB migrations are run up to the one which is currently being tested, it was not possible to set breakpoints in the migration file. This was because the migration-shared-context copied the currently tested migration into its own folder and also the currently tested migration + all newer migrations into another folder. So breakpoints do not work. Copying the migration files for only applying the correct migrations is not necessary. For the path we can use the normal path to the migrations folder. Additionally, we can use `target` to specify the migration to which we want to migrate. The correct target is calculated by using the current migrations timestamp and the latest migration's timestamp. This works for both, the up and the down path.
1 parent 58b2c0e commit 66ce805

11 files changed

+31
-49
lines changed

spec/migrations/20230822153000_streamline_annotation_key_prefix_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
value: 'some_value'
1919
)
2020
a1 = db[:isolation_segment_annotations].first(resource_guid: '123')
21-
expect { Sequel::Migrator.run(db, migration_to_test, allow_missing_migration_files: true) }.not_to raise_error
21+
expect { Sequel::Migrator.run(db, migrations_path, target: current_migration_index, allow_missing_migration_files: true) }.not_to raise_error
2222
b1 = db[:isolation_segment_annotations].first(resource_guid: '123')
2323
expect(b1[:guid]).to eq a1[:guid]
2424
expect(b1[:created_at]).to eq a1[:created_at]
@@ -36,7 +36,7 @@
3636
db[:isolation_segment_annotations].insert(guid: 'bommel2', resource_guid: '123', key: 'mykey2', value: 'some_value2')
3737
b1 = db[:isolation_segment_annotations].first(key: 'mykey')
3838
b2 = db[:isolation_segment_annotations].first(key: 'mykey2')
39-
expect { Sequel::Migrator.run(db, migration_to_test, allow_missing_migration_files: true) }.not_to raise_error
39+
expect { Sequel::Migrator.run(db, migrations_path, target: current_migration_index, allow_missing_migration_files: true) }.not_to raise_error
4040
c1 = db[:isolation_segment_annotations].first(key: 'mykey')
4141
c2 = db[:isolation_segment_annotations].first(key: 'mykey2')
4242
expect(b1.values).to eq(c1.values)

spec/migrations/20231016094900_microsecond_timestamp_msql_asg_update_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
end
2727

2828
# Change TIMESTAMP to TIMESTAMP(6)
29-
expect { Sequel::Migrator.run(db, migration_to_test, allow_missing_migration_files: true) }.not_to raise_error
29+
expect { Sequel::Migrator.run(db, migrations_path, target: current_migration_index, allow_missing_migration_files: true) }.not_to raise_error
3030

3131
# the migration shouldn't add accuracy to previously inserted values
3232
t1_post_migration = ds.first(id: 1)

spec/migrations/20231113105256_add_service_plan_id_index_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
end
88

99
it 'succeeds' do
10-
Sequel::Migrator.run(db, migration_to_test, allow_missing_migration_files: true)
10+
Sequel::Migrator.run(db, migrations_path, target: current_migration_index, allow_missing_migration_files: true)
1111
end
1212
end

spec/migrations/20231205143526_remove_deployments_with_degenerate_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
expect { db[:deployments].where(guid: 'degenerate_guid').delete }.to raise_error(Sequel::ForeignKeyConstraintViolation)
2222

23-
expect { Sequel::Migrator.run(db, migration_to_test, allow_missing_migration_files: true) }.not_to raise_error
23+
expect { Sequel::Migrator.run(db, migrations_path, target: current_migration_index, allow_missing_migration_files: true) }.not_to raise_error
2424

2525
expect(db[:deployments].where(status_reason: 'DEGENERATE').count).to eq(0)
2626

spec/migrations/20231221123000_rename_annotations_key_column_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
expect(db[table.to_sym].columns).to include(:key)
4444
expect(db[table.to_sym].columns).not_to include(:key_name)
4545
end
46-
expect { Sequel::Migrator.run(db, migration_to_test, allow_missing_migration_files: true) }.not_to raise_error
46+
expect { Sequel::Migrator.run(db, migrations_path, target: current_migration_index, allow_missing_migration_files: true) }.not_to raise_error
4747
annotation_tables.each do |table|
4848
expect(db[table.to_sym].columns).not_to include(:key)
4949
expect(db[table.to_sym].columns).to include(:key_name)

spec/migrations/20240102150000_add_annotation_label_uniqueness_spec.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
a1 = annotation.create(resource_guid: i1.guid, key_name: key_name, value: 'some_value')
2020

21-
expect { Sequel::Migrator.run(db, migration_to_test, allow_missing_migration_files: true) }.not_to raise_error
21+
expect { Sequel::Migrator.run(db, migrations_path, target: current_migration_index, allow_missing_migration_files: true) }.not_to raise_error
2222
expect(a1.reload.key_name).to eq(truncated_key_name)
2323
end
2424

@@ -28,7 +28,7 @@
2828

2929
a1 = annotation.create(resource_guid: i1.guid, key_name: key_name, value: 'some_value')
3030

31-
expect { Sequel::Migrator.run(db, migration_to_test, allow_missing_migration_files: true) }.not_to raise_error
31+
expect { Sequel::Migrator.run(db, migrations_path, target: current_migration_index, allow_missing_migration_files: true) }.not_to raise_error
3232
expect(a1.reload.key_name).to eq(key_name)
3333
end
3434

@@ -50,7 +50,7 @@
5050
expect(b1.id).to be < b2.id
5151
expect(b1.id).to be < b3.id
5252

53-
expect { Sequel::Migrator.run(db, migration_to_test, allow_missing_migration_files: true) }.not_to raise_error
53+
expect { Sequel::Migrator.run(db, migrations_path, target: current_migration_index, allow_missing_migration_files: true) }.not_to raise_error
5454
expect(annotation.where(key_name:).count).to eq(2)
5555
expect(a1.reload).to be_a(annotation)
5656
expect { a2.reload }.to raise_error(Sequel::NoExistingObject)
@@ -76,7 +76,7 @@
7676
b3 = annotation.create(resource_guid: i1.guid, key_prefix: 'bommel', key_name: key_b, value: 'v3')
7777
b4 = annotation.create(resource_guid: i1.guid, key_prefix: 'sword', key_name: key_a, value: 'v4')
7878

79-
expect { Sequel::Migrator.run(db, migration_to_test, allow_missing_migration_files: true) }.not_to raise_error
79+
expect { Sequel::Migrator.run(db, migrations_path, target: current_migration_index, allow_missing_migration_files: true) }.not_to raise_error
8080

8181
expect(annotation.all.count).to eq(7)
8282
expect(a1.reload).to be_a(annotation)
@@ -98,7 +98,7 @@
9898
# In case key_prefix is set
9999
annotation.create(resource_guid: i2.guid, key_prefix: 'bommel', key_name: key, value: 'v1')
100100

101-
expect { Sequel::Migrator.run(db, migration_to_test, allow_missing_migration_files: true) }.not_to raise_error
101+
expect { Sequel::Migrator.run(db, migrations_path, target: current_migration_index, allow_missing_migration_files: true) }.not_to raise_error
102102

103103
expect { annotation.create(resource_guid: i1.guid, key_name: key, value: 'v2') }.to raise_error(Sequel::UniqueConstraintViolation)
104104
expect { annotation.create(resource_guid: i2.guid, key_prefix: 'bommel', key_name: key, value: 'v2') }.to raise_error(Sequel::UniqueConstraintViolation)
@@ -110,7 +110,7 @@
110110
key_a = 'a' * 63
111111
key_b = 'b' * 63
112112

113-
expect { Sequel::Migrator.run(db, migration_to_test, allow_missing_migration_files: true) }.not_to raise_error
113+
expect { Sequel::Migrator.run(db, migrations_path, target: current_migration_index, allow_missing_migration_files: true) }.not_to raise_error
114114

115115
# In case key_prefix is not set
116116
a1 = annotation.create(resource_guid: i1.guid, key_name: key_a, value: 'v1')
@@ -152,7 +152,7 @@
152152
expect(b1.id).to be < b2.id
153153
expect(b1.id).to be < b3.id
154154

155-
expect { Sequel::Migrator.run(db, migration_to_test, allow_missing_migration_files: true) }.not_to raise_error
155+
expect { Sequel::Migrator.run(db, migrations_path, target: current_migration_index, allow_missing_migration_files: true) }.not_to raise_error
156156
expect(label.where(key_name: key).count).to eq(2)
157157
expect(a1.reload).to be_a(label)
158158
expect { a2.reload }.to raise_error(Sequel::NoExistingObject)
@@ -178,7 +178,7 @@
178178
b3 = label.create(resource_guid: i1.guid, key_prefix: 'bommel', key_name: key_b, value: 'v3')
179179
b4 = label.create(resource_guid: i1.guid, key_prefix: 'sword', key_name: key_a, value: 'v4')
180180

181-
expect { Sequel::Migrator.run(db, migration_to_test, allow_missing_migration_files: true) }.not_to raise_error
181+
expect { Sequel::Migrator.run(db, migrations_path, target: current_migration_index, allow_missing_migration_files: true) }.not_to raise_error
182182

183183
expect(label.all.count).to eq(7)
184184
expect(a1.reload).to be_a(label)
@@ -200,7 +200,7 @@
200200
# In case key_prefix is set
201201
label.create(resource_guid: i2.guid, key_prefix: 'bommel', key_name: key, value: 'v1')
202202

203-
expect { Sequel::Migrator.run(db, migration_to_test, allow_missing_migration_files: true) }.not_to raise_error
203+
expect { Sequel::Migrator.run(db, migrations_path, target: current_migration_index, allow_missing_migration_files: true) }.not_to raise_error
204204

205205
expect { label.create(resource_guid: i1.guid, key_name: key, value: 'v2') }.to raise_error(Sequel::UniqueConstraintViolation)
206206
expect { label.create(resource_guid: i2.guid, key_prefix: 'bommel', key_name: key, value: 'v2') }.to raise_error(Sequel::UniqueConstraintViolation)
@@ -212,7 +212,7 @@
212212
key_a = 'a' * 63
213213
key_b = 'b' * 63
214214

215-
expect { Sequel::Migrator.run(db, migration_to_test, allow_missing_migration_files: true) }.not_to raise_error
215+
expect { Sequel::Migrator.run(db, migrations_path, target: current_migration_index, allow_missing_migration_files: true) }.not_to raise_error
216216

217217
# In case key_prefix is not set
218218
a1 = label.create(resource_guid: i1.guid, key_name: key_a, value: 'v1')

spec/migrations/20240115163000_add_delete_cascade_to_foreign_keys_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
context 'after adding the foreign key' do
2222
it 'prevents inserts with a build_guid that does not exist' do
23-
Sequel::Migrator.run(db, migration_to_test, allow_missing_migration_files: true)
23+
Sequel::Migrator.run(db, migrations_path, target: current_migration_index, allow_missing_migration_files: true)
2424

2525
expect { db[:buildpack_lifecycle_data].insert(guid: 'bld_guid', build_guid: 'not_exists') }.to raise_error(Sequel::ForeignKeyConstraintViolation)
2626
end
@@ -30,7 +30,7 @@
3030
db[:buildpack_lifecycle_data].insert(guid: 'bld_guid', build_guid: 'build_guid')
3131
db[:buildpack_lifecycle_data].insert(guid: 'another_bld_guid', build_guid: 'not_exists')
3232

33-
Sequel::Migrator.run(db, migration_to_test, allow_missing_migration_files: true)
33+
Sequel::Migrator.run(db, migrations_path, target: current_migration_index, allow_missing_migration_files: true)
3434

3535
expect(db[:buildpack_lifecycle_data].where(guid: 'bld_guid').count).to eq(1)
3636
expect(db[:buildpack_lifecycle_data].where(guid: 'another_bld_guid').count).to eq(0)

spec/migrations/20240219113000_add_routes_space_id_index_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
end
88

99
it 'succeeds' do
10-
Sequel::Migrator.run(db, migration_to_test, allow_missing_migration_files: true)
10+
Sequel::Migrator.run(db, migrations_path, target: current_migration_index, allow_missing_migration_files: true)
1111
end
1212
end

spec/migrations/20240222131500_change_delayed_jobs_reserve_index_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
end
88

99
it 'succeeds' do
10-
Sequel::Migrator.run(db, migration_to_test, allow_missing_migration_files: true)
10+
Sequel::Migrator.run(db, migrations_path, target: current_migration_index, allow_missing_migration_files: true)
1111
end
1212
end

spec/migrations/Readme.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ Sequel migration tests have a distinct operation compared to conventional RSpec
7979

8080
### Usage
8181

82-
Here’s a guide on how to write a migration spec. The `migration` shared context uses the `migration_file` variable, containing the filename of the migration under review. This shared context also provides a `migration_to_test` variable to perform the specific migration. All other processes, including migrating one version before the test migration and fully migrating the table after the test has finished, are handled automatically.
83-
82+
Here’s a guide on how to write a migration spec. The `migration` shared context uses the `migration_file` variable, containing the filename of the migration under review. This shared context also provides a path to all migrations in `migrations_path` as well as the index of the current migration in `current_migration_index` variable to perform the specific migration. All other processes, including migrating one version before the test migration and fully migrating the table after the test has finished, are handled automatically.
83+
For testing the down part of a migration one can use `Sequel::Migrator.run(db, migrations_path, target: current_migration_index - 1, allow_missing_migration_files: true)`
8484
```ruby
8585
require 'spec_helper'
8686
require 'migrations/helpers/migration_shared_context'
@@ -94,7 +94,7 @@ RSpec.describe 'migration to modify isolation_segments', isolation: :truncation
9494
it 'retains the initial name and guid' do
9595
db[:isolation_segments].insert(name: 'bommel', guid: '123')
9696
a1 = db[:isolation_segment_annotations].first(resource_guid: '123')
97-
expect { Sequel::Migrator.run(db, migration_to_test, allow_missing_migration_files: true) }.not_to raise_error
97+
expect { Sequel::Migrator.run(db, migrations_path, target: current_migration_index, allow_missing_migration_files: true) }.not_to raise_error
9898
b1 = db[:isolation_segment_annotations].first(resource_guid: '123')
9999
expect(b1[:guid]).to eq a1[:guid]
100100
expect(b1[:name]).to eq a1[:name]

0 commit comments

Comments
 (0)