Example usage for com.mongodb.client.model ValidationOptions ValidationOptions

List of usage examples for com.mongodb.client.model ValidationOptions ValidationOptions

Introduction

In this page you can find the example usage for com.mongodb.client.model ValidationOptions ValidationOptions.

Prototype

ValidationOptions

Source Link

Usage

From source file:org.flywaydb.core.internal.metadatatable.MongoMetaDataTable.java

License:Apache License

/**
 * Creates the metadata collection./*from w  w  w  . j a v a  2 s . c  om*/
 *
 * @param dbName  The database where this metadata collection will be created.
 */
private void createMetadataCollection(String dbName) {
    LOG.info("In MongoDB " + dbName + ", creating Metadata collection: " + collectionName);
    // Database types used in the metadata collection schema.
    BasicDBObject dbInt = new BasicDBObject("$type", "int");
    BasicDBObject dbString = new BasicDBObject("$type", "string");
    BasicDBObject dbBool = new BasicDBObject("$type", "bool");
    BasicDBObject dbDate = new BasicDBObject("$type", "date");
    BasicDBObject dbNull = new BasicDBObject("$type", "null");
    BasicDBObject checksumTypeInt = new BasicDBObject(MetaDataDocument.CHECKSUM, dbInt);
    BasicDBObject checksumTypeNull = new BasicDBObject(MetaDataDocument.CHECKSUM, dbNull);
    BasicDBObject[] checksumType = { checksumTypeInt, checksumTypeNull };
    BasicDBObject versionTypeString = new BasicDBObject(MetaDataDocument.VERSION, dbString);
    BasicDBObject versionTypeNull = new BasicDBObject(MetaDataDocument.VERSION, dbNull);
    BasicDBObject[] versionType = { versionTypeString, versionTypeNull };
    // Database object for setting a valid mongo schema.
    BasicDBObject validSchema = new BasicDBObject().append(MetaDataDocument.INSTALLED_RANK, dbInt)
            .append("$or", versionType).append(MetaDataDocument.DESCRIPTION, dbString)
            .append(MetaDataDocument.TYPE, dbString).append(MetaDataDocument.SCRIPT, dbString)
            .append("$or", checksumType).append(MetaDataDocument.INSTALLED_BY, dbString)
            .append(MetaDataDocument.INSTALLED_ON, dbDate).append(MetaDataDocument.EXECUTION_TIME, dbInt)
            .append(MetaDataDocument.SUCCESS, dbBool);
    ValidationOptions validator = new ValidationOptions().validator(validSchema)
            .validationLevel(ValidationLevel.STRICT);
    CreateCollectionOptions collectionOptions = new CreateCollectionOptions().validationOptions(validator);

    Boolean dbExists = MongoDatabaseUtil.exists(client, dbName);
    if (dbExists) {
        LOG.debug("Database " + dbName + " already exists. Skipping database creation.");
    } else {
        LOG.info("Creating Mongo database " + dbName + " ...");
    }
    mongoDatabase.createCollection(collectionName, collectionOptions);
    LOG.debug("Metadata collection " + collectionName + " created.");
    this.metadataCollection = mongoDatabase.getCollection(collectionName, BasicDBObject.class);
    if (!dbExists)
        addSchemaMarker();
}