diff --git a/db/migrations/20230822153000_streamline_annotation_key_prefix.rb b/db/migrations/20230822153000_streamline_annotation_key_prefix.rb index 698c81ef09b..50a32c1f623 100644 --- a/db/migrations/20230822153000_streamline_annotation_key_prefix.rb +++ b/db/migrations/20230822153000_streamline_annotation_key_prefix.rb @@ -35,8 +35,13 @@ 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, -value: annotation[:value]) + 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 end diff --git a/db/migrations/202308221730000_add_migration_views_for_annotations.rb b/db/migrations/20230822173000_add_migration_views_for_annotations.rb similarity index 100% rename from db/migrations/202308221730000_add_migration_views_for_annotations.rb rename to db/migrations/20230822173000_add_migration_views_for_annotations.rb diff --git a/spec/migrations/20230822153000_streamline_annotation_key_prefix_spec.rb b/spec/migrations/20230822153000_streamline_annotation_key_prefix_spec.rb index 6bfe9b173f4..d76ac798b66 100644 --- a/spec/migrations/20230822153000_streamline_annotation_key_prefix_spec.rb +++ b/spec/migrations/20230822153000_streamline_annotation_key_prefix_spec.rb @@ -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 } @@ -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. @@ -27,34 +29,44 @@ 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: '2023-08-23 15:52:36.000000000 +0000', + updated_at: '2023-08-23 15:52:36.000000000 +0000', + 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