Skip to content

Commit

Permalink
Correct migration tests to not leave db in half migrated state
Browse files Browse the repository at this point in the history
This change fixes the migration tests for the streamlining of annotation
keys. The previous test would leave the db in a half migrated state and
then following tests would fail due to the rest of the migrations beeing
missing.

It may make sense to move the begin and after part of the migration test
into a helper to make this a proper workflow for every migration test
since we must make sure we start the test before the migration we`d like
to test. Then just apply one single migration(and not all migrations),
then expect certain behaviour and lastly complete the migration to not
destroy the DB. This should be in a helper as currently it also requires
working down migrations which is not always the case.
  • Loading branch information
FloThinksPi committed Aug 23, 2023
1 parent d3d2788 commit f6febf2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
prefix, key_name = VCAP::CloudController::MetadataHelpers.extract_prefix(annotation[:key].to_s)
if prefix.present?
self[table.to_sym].where(guid: annotation[:guid]).delete
self[table.to_sym].insert(guid: annotation[:guid], created_at: annotation[:created_at], resource_guid: annotation[:resource_guid], key_prefix: prefix, key: key_name,
self[table.to_sym].insert(guid: annotation[:guid], created_at: annotation[:created_at], updated_at: Sequel::CURRENT_TIMESTAMP, resource_guid: annotation[:resource_guid], key_prefix: prefix, key: key_name,
value: annotation[:value])
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

RSpec.describe 'migration to streamline changes to annotation_key_prefix', isolation: :truncation do
let(:filename) { '20230822153000_streamline_annotation_key_prefix.rb' }
let(:tmp_all_migrations_dir) { Dir.mktmpdir }
let(:tmp_down_migrations_dir) { Dir.mktmpdir }
let(:tmp_up_migrations_dir) { Dir.mktmpdir }
let(:db) { Sequel::Model.db }
Expand All @@ -18,6 +19,7 @@
# Make a file list of the migration file we like to test plus all migrations after the one we want to test
migration_files_after_test = migration_files[migration_index...]
# Copy them to a temp directory
FileUtils.cp(migration_files, tmp_all_migrations_dir)
FileUtils.cp(migration_files_after_test, tmp_down_migrations_dir)
FileUtils.cp(File.join(DBMigrator::SEQUEL_MIGRATIONS, filename), tmp_up_migrations_dir)
# Revert the given migration and everything newer so we are at the database version exactly before our migration we want to test.
Expand All @@ -27,34 +29,38 @@
after do
FileUtils.rm_rf(tmp_up_migrations_dir)
FileUtils.rm_rf(tmp_down_migrations_dir)

# Complete the migration to not leave the test database half migrated and following tests fail due to this
Sequel::Migrator.run(db, tmp_all_migrations_dir, allow_missing_migration_files: true)
FileUtils.rm_rf(tmp_all_migrations_dir)
end

describe 'annotation tables' do
it 'converts all legacy key_prefixes to annotations with prefixes in the key_prefix column' do
i1 = isolation_segment.create(name: 'bommel')
a1 = annotation.create(resource_guid: i1.guid, key_name: 'mylegacyprefix/mykey', value: 'some_value')
new_prefix, new_key_name = VCAP::CloudController::MetadataHelpers.extract_prefix(a1[:key].to_s)
db[:isolation_segments].insert(name: 'bommel', guid: '123')
db[:isolation_segment_annotations].insert(guid: 'bommel', created_at: Sequel::CURRENT_TIMESTAMP, updated_at: Sequel::CURRENT_TIMESTAMP, resource_guid: '123', key: 'mylegacyprefix/mykey', value: 'some_value')
a1 = db[:isolation_segment_annotations].first(resource_guid: '123')
expect { Sequel::Migrator.run(db, tmp_up_migrations_dir, allow_missing_migration_files: true) }.not_to raise_error
b1 = db[:isolation_segment_annotations].first(resource_guid: i1.guid)
b1 = db[:isolation_segment_annotations].first(resource_guid: '123')
expect(b1[:guid]).to eq a1[:guid]
expect(b1[:created_at]).to eq a1[:created_at]
expect(b1[:updated_at]).to_not eq a1[:updated_at]
expect(b1[:resource_guid]).to eq a1[:resource_guid]
expect(b1[:key_prefix]).to_not eq a1[:key_prefix]
expect(b1[:key]).to_not eq a1[:key]
expect(b1[:key_prefix]).to eq new_prefix
expect(b1[:key]).to eq new_key_name
expect(b1[:key_prefix]).to eq "mylegacyprefix"
expect(b1[:key]).to eq "mykey"
end

it 'doesnt touch any values that have no legacy key_prefix in its key field' do
i1 = isolation_segment.create(name: 'bommel')
a1 = annotation.create(resource_guid: i1.guid, key_prefix: 'myprefix', key: 'mykey', value: 'some_value')
a2 = annotation.create(resource_guid: i1.guid, key: 'mykey2', value: 'some_value2')
b1 = db[:isolation_segment_annotations].first(key: a1[:key])
b2 = db[:isolation_segment_annotations].first(key: a2[:key])
db[:isolation_segments].insert(name: 'bommel', guid: '123')
db[:isolation_segment_annotations].insert(guid: 'bommel', resource_guid: '123', key_prefix: 'myprefix', key: 'mykey', value: 'some_value')
db[:isolation_segment_annotations].insert(guid: 'bommel2', resource_guid: '123', key: 'mykey2', value: 'some_value2')
b1 = db[:isolation_segment_annotations].first(key: 'mykey')
b2 = db[:isolation_segment_annotations].first(key: 'mykey2')
expect { Sequel::Migrator.run(db, tmp_up_migrations_dir, allow_missing_migration_files: true) }.not_to raise_error
c1 = db[:isolation_segment_annotations].first(key: a1[:key])
c2 = db[:isolation_segment_annotations].first(key: a2[:key])
c1 = db[:isolation_segment_annotations].first(key: 'mykey')
c2 = db[:isolation_segment_annotations].first(key: 'mykey2')
expect(b1.values).to eq(c1.values)
expect(b2.values).to eq(c2.values)
end
Expand Down

0 comments on commit f6febf2

Please sign in to comment.