Skip to content

Modify collection schema #388

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,33 @@

package com.arangodb.internal.velocypack;

import com.arangodb.entity.*;
import com.arangodb.entity.arangosearch.*;
import com.arangodb.entity.BaseDocument;
import com.arangodb.entity.BaseEdgeDocument;
import com.arangodb.entity.CollectionType;
import com.arangodb.entity.DocumentField;
import com.arangodb.entity.LogLevel;
import com.arangodb.entity.MinReplicationFactor;
import com.arangodb.entity.Permissions;
import com.arangodb.entity.ReplicationFactor;
import com.arangodb.entity.ViewType;
import com.arangodb.entity.arangosearch.ArangoSearchCompression;
import com.arangodb.entity.arangosearch.ArangoSearchProperties;
import com.arangodb.entity.arangosearch.CollectionLink;
import com.arangodb.entity.arangosearch.ConsolidationType;
import com.arangodb.entity.arangosearch.FieldLink;
import com.arangodb.entity.arangosearch.PrimarySort;
import com.arangodb.entity.arangosearch.StoreValuesType;
import com.arangodb.entity.arangosearch.StoredValue;
import com.arangodb.internal.velocystream.internal.AuthenticationRequest;
import com.arangodb.model.CollectionSchema;
import com.arangodb.model.TraversalOptions;
import com.arangodb.model.TraversalOptions.Order;
import com.arangodb.model.arangosearch.ArangoSearchPropertiesOptions;
import com.arangodb.velocypack.*;
import com.arangodb.velocypack.VPackBuilder;
import com.arangodb.velocypack.VPackParser;
import com.arangodb.velocypack.VPackSerializer;
import com.arangodb.velocypack.VPackSlice;
import com.arangodb.velocypack.ValueType;
import com.arangodb.velocystream.Request;

import java.util.Collection;
Expand Down Expand Up @@ -247,10 +266,10 @@ private static void serializeFieldLinks(final VPackBuilder builder, final Collec

public static final VPackSerializer<CollectionSchema> COLLECTION_VALIDATION = (builder, attribute, value, context) -> {
VPackParser parser = new VPackParser.Builder().build();
VPackSlice rule = parser.fromJson(value.getRule(), true);
VPackSlice rule = value.getRule() != null ? parser.fromJson(value.getRule(), true) : null;
final Map<String, Object> doc = new HashMap<>();
doc.put("message", value.getMessage());
doc.put("level", value.getLevel().getValue());
doc.put("level", value.getLevel() != null ? value.getLevel().getValue() : null);
doc.put("rule", rule);
context.serialize(builder, attribute, doc);
};
Expand Down
21 changes: 18 additions & 3 deletions src/main/java/com/arangodb/model/CollectionCreateOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@

package com.arangodb.model;

import com.arangodb.entity.*;
import com.arangodb.entity.CollectionType;
import com.arangodb.entity.KeyOptions;
import com.arangodb.entity.KeyType;
import com.arangodb.entity.MinReplicationFactor;
import com.arangodb.entity.ReplicationFactor;

/**
* @author Mark Vollmary
Expand Down Expand Up @@ -330,10 +334,21 @@ public CollectionSchema getSchema() {
}

/**
* @param schema object that specifies the collection level schema for documents
* @since ArangoDB 3.7
* @param schema
* object that specifies the collection level schema for documents
* @return options
* @since ArangoDB 3.7
*/
public CollectionCreateOptions schema(final CollectionSchema schema) {
this.schema = schema;
return this;
}

/**
* @since ArangoDB 3.7
* @deprecated Use {@link #schema(CollectionSchema)} instead.
*/
@Deprecated
public CollectionCreateOptions setSchema(final CollectionSchema schema) {
this.schema = schema;
return this;
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/arangodb/model/CollectionPropertiesOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class CollectionPropertiesOptions {

private Boolean waitForSync;
private Long journalSize;
private CollectionSchema schema;

public CollectionPropertiesOptions() {
super();
Expand Down Expand Up @@ -62,4 +63,18 @@ public CollectionPropertiesOptions journalSize(final Long journalSize) {
return this;
}

public CollectionSchema getSchema() {
return schema;
}

/**
* @param schema object that specifies the collection level schema for documents
* @since ArangoDB 3.7
* @return options
*/
public CollectionPropertiesOptions schema(final CollectionSchema schema) {
this.schema = schema;
return this;
}

}
9 changes: 9 additions & 0 deletions src/main/java/com/arangodb/model/CollectionSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@
* @since ArangoDB 3.7
*/
public class CollectionSchema {

/**
* Value to unset the collection schema on properties update {@link com.arangodb.ArangoCollection#changeProperties(CollectionPropertiesOptions)}.
*/
public static final CollectionSchema NULL_SCHEMA = new CollectionSchema()
.setLevel(null)
.setMessage(null)
.setRule(null);

private String rule;
private Level level;
private String message;
Expand Down
40 changes: 37 additions & 3 deletions src/test/java/com/arangodb/ArangoCollectionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import com.arangodb.entity.ShardEntity;
import com.arangodb.model.CollectionCreateOptions;
import com.arangodb.model.CollectionPropertiesOptions;
import com.arangodb.model.CollectionSchema;
import com.arangodb.model.DocumentCreateOptions;
import com.arangodb.model.DocumentDeleteOptions;
import com.arangodb.model.DocumentExistsOptions;
Expand Down Expand Up @@ -2612,14 +2613,47 @@ public void getPropeties() {
public void changeProperties() {
final CollectionPropertiesEntity properties = collection.getProperties();
assertThat(properties.getWaitForSync(), is(notNullValue()));
if (isAtLeastVersion(3, 7)) {
assertThat(properties.getSchema(), is(nullValue()));
}

final CollectionPropertiesEntity changedProperties = collection.changeProperties(new CollectionPropertiesOptions()
.waitForSync(!properties.getWaitForSync()));
String schemaRule = ("{ " +
" \"properties\": {" +
" \"number\": {" +
" \"type\": \"number\"" +
" }" +
" }" +
" }")
.replaceAll("\\s", "");
String schemaMessage = "The document has problems!";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessarily in this pr, but shouldn't the message be configurable?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code here is from the tests. Also, the message is the same for any validation error, since:

The schema validation cannot pin-point which part of a rule made it fail

See https://www.arangodb.com/docs/stable/data-modeling-documents-schema-validation.html#error-message


CollectionPropertiesOptions updatedOptions = new CollectionPropertiesOptions()
.waitForSync(!properties.getWaitForSync())
.schema(new CollectionSchema()
.setLevel(CollectionSchema.Level.NEW)
.setMessage(schemaMessage)
.setRule(schemaRule)
);

final CollectionPropertiesEntity changedProperties = collection.changeProperties(updatedOptions);
assertThat(changedProperties.getWaitForSync(), is(notNullValue()));
assertThat(changedProperties.getWaitForSync(), is(!properties.getWaitForSync()));
if (isAtLeastVersion(3, 7)) {
assertThat(changedProperties.getSchema(), is(notNullValue()));
assertThat(changedProperties.getSchema().getLevel(), is(CollectionSchema.Level.NEW));
assertThat(changedProperties.getSchema().getMessage(), is(schemaMessage));
assertThat(changedProperties.getSchema().getRule(), is(schemaRule));
}

// revert changes
collection.changeProperties(new CollectionPropertiesOptions().waitForSync(properties.getWaitForSync()));
CollectionPropertiesEntity revertedProperties = collection.changeProperties(new CollectionPropertiesOptions()
.waitForSync(properties.getWaitForSync())
.schema(CollectionSchema.NULL_SCHEMA)
);
if (isAtLeastVersion(3, 7)) {
assertThat(revertedProperties.getSchema(), is(nullValue()));
}

}

@Test
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/arangodb/ArangoDatabaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ public void createCollectionWithJsonSchema() {

final CollectionEntity result = db
.createCollection(name, new CollectionCreateOptions()
.setSchema(
.schema(
new CollectionSchema()
.setLevel(CollectionSchema.Level.NEW)
.setMessage(message)
Expand Down