use of com.apple.foundationdb.record.metadata.MetaDataException in project fdb-record-layer by FoundationDB.
the class FDBMetaDataStoreTest method deprecateImportedRecordType.
@Test
public void deprecateImportedRecordType() {
int version;
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
RecordMetaData metaData = RecordMetaData.build(TestRecordsImportProto.getDescriptor());
version = metaData.getVersion();
metaDataStore.saveRecordMetaData(metaData);
context.commit();
assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MySimpleRecord"));
}
// Deprecate the record type
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
RecordMetaData metaData = metaDataStore.getRecordMetaData();
assertEquals(version, metaData.getVersion());
assertNotNull(metaData.getRecordType("MySimpleRecord"));
assertNotNull(metaData.getRecordType("MyLongRecord"));
MetaDataException e = assertThrows(MetaDataException.class, () -> deprecateRecordType("MySimpleRecord"));
assertEquals("Record type MySimpleRecord not found", e.getMessage());
}
}
use of com.apple.foundationdb.record.metadata.MetaDataException in project fdb-record-layer by FoundationDB.
the class FDBMetaDataStoreTest method nestedRecordDefinition.
/**
* Validate that a message type cannot be specified using "." syntax.
*/
@Test
public void nestedRecordDefinition() {
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
RecordMetaData metaData = RecordMetaData.build(TestRecordsDoubleNestedProto.getDescriptor());
metaDataStore.saveRecordMetaData(metaData);
context.commit();
}
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
MetaDataException e = assertThrows(MetaDataException.class, () -> renameRecordType("OuterRecord.MiddleRecord", "OuterRecord.MiddlingRecord"));
assertEquals("No record type found with name OuterRecord.MiddleRecord", e.getMessage());
}
}
use of com.apple.foundationdb.record.metadata.MetaDataException in project fdb-record-layer by FoundationDB.
the class FDBMetaDataStoreTest method nestedRecordTypes.
@Test
public void nestedRecordTypes() {
int version;
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
RecordMetaData metaData = RecordMetaData.build(TestRecords1Proto.getDescriptor());
version = metaData.getVersion();
metaDataStore.saveRecordMetaData(metaData);
context.commit();
assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MySimpleRecord"));
}
// Adding an existing record type should fail
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MySimpleRecord"));
assertEquals(version, metaDataStore.getRecordMetaData().getVersion());
DescriptorProtos.MessageOptions.Builder nestedMessageOptions = DescriptorProtos.MessageOptions.newBuilder().setExtension(RecordMetaDataOptionsProto.record, RecordMetaDataOptionsProto.RecordTypeOptions.newBuilder().setUsage(RecordMetaDataOptionsProto.RecordTypeOptions.Usage.NESTED).build());
DescriptorProtos.DescriptorProto newRecordType = DescriptorProtos.DescriptorProto.newBuilder().setName("MySimpleRecord").addField(DescriptorProtos.FieldDescriptorProto.newBuilder().setLabel(DescriptorProtos.FieldDescriptorProto.Label.LABEL_OPTIONAL).setType(DescriptorProtos.FieldDescriptorProto.Type.TYPE_INT32).setName("rec_no").setNumber(1)).setOptions(nestedMessageOptions).build();
MetaDataException e = assertThrows(MetaDataException.class, () -> addNestedRecordType(newRecordType));
assertEquals(e.getMessage(), "Record type MySimpleRecord already exists");
assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MySimpleRecord"));
// version should not change
assertEquals(version, metaDataStore.getRecordMetaData().getVersion());
context.commit();
}
// Add a nested record type.
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MySimpleRecord"));
assertEquals(version, metaDataStore.getRecordMetaData().getVersion());
DescriptorProtos.MessageOptions.Builder nestedMessageOptions = DescriptorProtos.MessageOptions.newBuilder().setExtension(RecordMetaDataOptionsProto.record, RecordMetaDataOptionsProto.RecordTypeOptions.newBuilder().setUsage(RecordMetaDataOptionsProto.RecordTypeOptions.Usage.NESTED).build());
DescriptorProtos.DescriptorProto newRecordType = DescriptorProtos.DescriptorProto.newBuilder().setName("MyNewNestedRecord").addField(DescriptorProtos.FieldDescriptorProto.newBuilder().setLabel(DescriptorProtos.FieldDescriptorProto.Label.LABEL_OPTIONAL).setType(DescriptorProtos.FieldDescriptorProto.Type.TYPE_INT32).setName("rec_no").setNumber(1)).setOptions(nestedMessageOptions).build();
// Use addRecordType should fail
MetaDataException e = assertThrows(MetaDataException.class, () -> addRecordType(newRecordType, Key.Expressions.field("rec_no")));
assertEquals(e.getMessage(), "Use addNestedRecordType for adding NESTED record types");
// Use addNestedRecordType should succeed
addNestedRecordType(newRecordType);
assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MySimpleRecord"));
e = assertThrows(MetaDataException.class, () -> metaDataStore.getRecordMetaData().getRecordType("MyNewNestedRecord"));
assertEquals(e.getMessage(), "Unknown record type MyNewNestedRecord");
assertNotNull(metaDataStore.getRecordMetaData().getRecordsDescriptor().findMessageTypeByName("MyNewNestedRecord"));
assertNull(metaDataStore.getRecordMetaData().getRecordsDescriptor().findMessageTypeByName(RecordMetaDataBuilder.DEFAULT_UNION_NAME).findFieldByName("_MyNewNestedRecord"));
assertEquals(version + 1, metaDataStore.getRecordMetaData().getVersion());
// addNestedRecordType is not idempotent!
e = assertThrows(MetaDataException.class, () -> addNestedRecordType(newRecordType));
assertEquals(e.getMessage(), "Record type MyNewNestedRecord already exists");
assertEquals(version + 1, metaDataStore.getRecordMetaData().getVersion());
context.commit();
}
// Add nested type as a field
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MySimpleRecord"));
assertNotNull(metaDataStore.getRecordMetaData().getRecordsDescriptor().findMessageTypeByName("MyNewNestedRecord"));
assertEquals(version + 1, metaDataStore.getRecordMetaData().getVersion());
DescriptorProtos.FieldDescriptorProto field = DescriptorProtos.FieldDescriptorProto.newBuilder().setName("newField").setType(DescriptorProtos.FieldDescriptorProto.Type.TYPE_MESSAGE).setTypeName("MyNewNestedRecord").setLabel(DescriptorProtos.FieldDescriptorProto.Label.LABEL_OPTIONAL).setNumber(10).build();
addField("MySimpleRecord", field);
assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MySimpleRecord"));
assertNotNull(metaDataStore.getRecordMetaData().getRecordsDescriptor().findMessageTypeByName("MySimpleRecord").findFieldByName("newField"));
assertEquals(metaDataStore.getRecordMetaData().getRecordsDescriptor().findMessageTypeByName("MyNewNestedRecord"), metaDataStore.getRecordMetaData().getRecordsDescriptor().findMessageTypeByName("MySimpleRecord").findFieldByName("newField").getMessageType());
assertEquals(version + 2, metaDataStore.getRecordMetaData().getVersion());
context.commit();
}
// Deprecate a nested record type.
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
assertEquals(version + 2, metaDataStore.getRecordMetaData().getVersion());
assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MySimpleRecord"));
assertNotNull(metaDataStore.getRecordMetaData().getRecordsDescriptor().findMessageTypeByName("MyNewNestedRecord"));
deprecateField("MySimpleRecord", "newField");
assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MySimpleRecord"));
assertTrue(metaDataStore.getRecordMetaData().getRecordsDescriptor().findMessageTypeByName("MySimpleRecord").findFieldByName("newField").getOptions().getDeprecated());
assertEquals(version + 3, metaDataStore.getRecordMetaData().getVersion());
context.commit();
}
}
use of com.apple.foundationdb.record.metadata.MetaDataException in project fdb-record-layer by FoundationDB.
the class FDBMetaDataStoreTest method dontRenameRecordTypeWhenClashingWithImported.
/**
* Validate that if a {@code NESTED} record with the same name as an imported record type has its
* name changed, then the record type in the record type list does not change.
*/
@Test
public void dontRenameRecordTypeWhenClashingWithImported() {
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
RecordMetaData metaData = RecordMetaData.build(TestRecordsImportedAndNewProto.getDescriptor());
metaDataStore.saveRecordMetaData(metaData);
// rename the nested record
renameRecordType("MySimpleRecord", "MyLocalSimpleRecord");
assertEquals(ImmutableSet.of("MySimpleRecord", "MyOtherRecord"), metaData.getRecordTypes().keySet());
context.commit();
}
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
MetaDataException e = assertThrows(MetaDataException.class, () -> renameRecordType("MyOtherRecord", "MySimpleRecord"));
assertEquals("Cannot rename record type to MySimpleRecord as an imported record type of that name already exists", e.getMessage());
}
}
use of com.apple.foundationdb.record.metadata.MetaDataException in project fdb-record-layer by FoundationDB.
the class FDBMetaDataStoreTest method noUnionImplicitUsage.
@ParameterizedTest(name = "noUnionImplicitUsage [repeatSaveOrDoUpdate = {0}]")
@BooleanSource
public void noUnionImplicitUsage(boolean repeatSaveOrDoUpdate) {
int version;
// MyOtherRecord has no explicit usage. The proto file has a union and does not include MyOtherRecord.
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
metaDataStore.saveRecordMetaData(TestRecordsImplicitUsageProto.getDescriptor());
context.commit();
version = metaDataStore.getRecordMetaData().getVersion();
assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MySimpleRecord"));
MetaDataException e = assertThrows(MetaDataException.class, () -> metaDataStore.getRecordMetaData().getRecordType("MyOtherRecord"));
assertEquals(e.getMessage(), "Unknown record type MyOtherRecord");
}
// The evolved proto no longer has a union. The record types with no explicit usage should now automatically show up in the union.
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
assertEquals(version, metaDataStore.getRecordMetaData().getVersion());
if (repeatSaveOrDoUpdate) {
metaDataStore.updateRecords(TestRecordsImplicitUsageNoUnionProto.getDescriptor());
} else {
metaDataStore.saveRecordMetaData(TestRecordsImplicitUsageNoUnionProto.getDescriptor());
}
assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MySimpleRecord"));
assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MyOtherRecord"));
assertEquals(version + 1, metaDataStore.getRecordMetaData().getVersion());
context.commit();
}
}
Aggregations