Skip to content

Commit

Permalink
CreateCollection now supports all available options. Cosmetic adjustm…
Browse files Browse the repository at this point in the history
…ents, changed to AbstractRunCommandStatement.
  • Loading branch information
alexandru-slobodcicov committed Jan 24, 2021
1 parent bc31cfd commit 5146509
Show file tree
Hide file tree
Showing 13 changed files with 128 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@

@DatabaseChange(name = "createCollection",
description = "Create collection. Supports all options available: " +
"https://docs.mongodb.com/manual/reference/method/db.createCollection/#db.createCollection",
"https://docs.mongodb.com/manual/reference/method/db.createCollection/#db.createCollection\n" +
"https://docs.mongodb.com/manual/reference/method/db.runCommand/#db.runCommand",
priority = ChangeMetaData.PRIORITY_DEFAULT, appliesTo = "collection")
@NoArgsConstructor
@Getter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.List;

import static java.lang.Boolean.TRUE;
import static java.util.Optional.ofNullable;

public class AdjustChangeLogCollectionStatement extends RunCommandStatement {

Expand Down Expand Up @@ -87,4 +88,18 @@ private void adjustIndexes(final MongoConnection connection) {
collection.createIndex(keys, options);
}
}

@Override
public String toJs() {
return SHELL_DB_PREFIX
+ getCommandName()
+ "("
+ ofNullable(command).map(Document::toJson).orElse(null)
+ ");";
}

@Override
public Document run(final MongoConnection connection) {
return connection.getDatabase().runCommand(command);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
import liquibase.ext.mongodb.database.MongoConnection;
import liquibase.ext.mongodb.statement.RunCommandStatement;
import lombok.Getter;
import org.bson.Document;

import static java.lang.Boolean.TRUE;
import static java.util.Optional.ofNullable;

public class AdjustChangeLogLockCollectionStatement extends RunCommandStatement {

Expand Down Expand Up @@ -59,4 +61,18 @@ public void execute(final MongoConnection connection) {
super.execute(connection);
}
}

@Override
public String toJs() {
return SHELL_DB_PREFIX
+ getCommandName()
+ "("
+ ofNullable(command).map(Document::toJson).orElse(null)
+ ");";
}

@Override
public Document run(final MongoConnection connection) {
return connection.getDatabase().runCommand(command);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,17 @@ public abstract class AbstractCollectionStatement extends AbstractNoSqlStatement
protected final String collectionName;

/**
* Provides a javascript representation of the command
* (for example that can be ran in the mongo shell).
* Provides a pseudo javascript representation of the collection related statement
* (for example that can be ran in the mongo shell.
* Exceptions examples are count which uses db.getCollectionNames however filters programmatically by name).
* @return javascript version of the full command
*/
@Override
public abstract String toJs();
public String toJs() {
return "db." +
getCommandName() +
"(" +
getCollectionName() +
");";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,44 @@
import liquibase.ext.mongodb.database.MongoConnection;
import liquibase.nosql.statement.AbstractNoSqlStatement;
import liquibase.nosql.statement.NoSqlExecuteStatement;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import org.bson.Document;

public abstract class AbstractMongoDocumentStatement<T extends Document> extends AbstractNoSqlStatement
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
public abstract class AbstractRunCommandStatement extends AbstractNoSqlStatement
implements NoSqlExecuteStatement<MongoConnection> {

public abstract T run(MongoConnection connection);
public static final String COMMAND_NAME = "runCommand";
public static final String SHELL_DB_PREFIX = "db.";

@Getter
protected final Document command;

@Override
public void execute(final MongoConnection connection) {
run(connection);
}

public Document run(final MongoConnection connection) {
return connection.getDatabase().runCommand(command);
}

@Override
public String getCommandName() {
return COMMAND_NAME;
}

@Override
public String toJs() {
return SHELL_DB_PREFIX
+ getCommandName()
+ "("
+ BsonUtils.toJson(command)
+ ");";
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import lombok.Getter;
import org.bson.Document;

import static java.util.Optional.ofNullable;

@Getter
@EqualsAndHashCode(callSuper = true)
public class AdminCommandStatement extends RunCommandStatement {
Expand Down
19 changes: 18 additions & 1 deletion src/main/java/liquibase/ext/mongodb/statement/BsonUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,20 @@
import lombok.NoArgsConstructor;
import org.bson.Document;
import org.bson.UuidRepresentation;
import org.bson.codecs.*;
import org.bson.codecs.BsonValueCodecProvider;
import org.bson.codecs.DocumentCodec;
import org.bson.codecs.DocumentCodecProvider;
import org.bson.codecs.UuidCodec;
import org.bson.codecs.UuidCodecProvider;
import org.bson.codecs.ValueCodecProvider;
import org.bson.codecs.configuration.CodecRegistries;
import org.bson.codecs.configuration.CodecRegistry;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import static java.util.Objects.nonNull;
import static java.util.Optional.ofNullable;
import static liquibase.util.StringUtil.trimToNull;
import static lombok.AccessLevel.PRIVATE;
Expand Down Expand Up @@ -90,4 +96,15 @@ public static IndexOptions orEmptyIndexOptions(final Document options) {
return indexOptions;
}

public static String toJson(final Document document) {
return ofNullable(document).map(Document::toJson).orElse(null);
}

public static Document toCommand(final String commandName, final Object commandValue, final Document options) {
final Document command = new Document(commandName, commandValue);
if (nonNull(options)) {
command.putAll(options);
}
return command;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

import liquibase.ext.mongodb.database.MongoConnection;
import liquibase.nosql.statement.NoSqlQueryForLongStatement;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;

Expand All @@ -33,7 +32,7 @@
public class CountCollectionByNameStatement extends AbstractCollectionStatement
implements NoSqlQueryForLongStatement<MongoConnection> {

public static final String COMMAND_NAME = "count";
public static final String COMMAND_NAME = "getCollectionNames";

public CountCollectionByNameStatement(final String collectionName) {
super(collectionName);
Expand All @@ -50,9 +49,4 @@ public long queryForLong(final MongoConnection connection) {
.filter(s -> s.equals(getCollectionName()))
.count();
}

@Override
public String toJs() {
return String.format("db.%s.count()", getCollectionName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,59 +20,32 @@
* #L%
*/

import liquibase.ext.mongodb.database.MongoConnection;
import liquibase.nosql.statement.NoSqlExecuteStatement;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import org.bson.Document;
import org.bson.conversions.Bson;

import static com.mongodb.MongoClientSettings.getDefaultCodecRegistry;

/**
* Creates a collection via the database runCommand method
* For a list of supported options see the reference page:
* https://docs.mongodb.com/manual/reference/command/create/index.html
* https://docs.mongodb.com/manual/reference/command/create/#create
*/
@Getter
@EqualsAndHashCode(callSuper = true)
public class CreateCollectionStatement extends AbstractCollectionStatement
implements NoSqlExecuteStatement<MongoConnection> {

private static final String COMMAND_NAME = "create";
public class CreateCollectionStatement extends AbstractRunCommandStatement {

private final Document options;
public static final String RUN_COMMAND_NAME = "create";

public CreateCollectionStatement(final String collectionName, final String options) {
this(collectionName, BsonUtils.orEmptyDocument(options));
}

public CreateCollectionStatement(final String collectionName, final Document options) {
super(collectionName);
this.options = options;
super(BsonUtils.toCommand(RUN_COMMAND_NAME, collectionName, options));
}

@Override
public String getCommandName() {
return COMMAND_NAME;
}

@Override
public String toJs() {
return String.format("db.runCommand(%s)", createCommand().toString());
}

@Override
public void execute(final MongoConnection connection) {
Bson bson = createCommand();
connection.getDatabase().runCommand(bson);
}

private Bson createCommand() {
Document commandOptions = new Document(COMMAND_NAME, getCollectionName());
if(options!=null) {
commandOptions.putAll(options);
}
return commandOptions.toBsonDocument(Document.class, getDefaultCodecRegistry());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,47 +20,18 @@
* #L%
*/

import liquibase.ext.mongodb.database.MongoConnection;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import org.bson.Document;

import static java.util.Optional.ofNullable;

@Getter
@EqualsAndHashCode(callSuper = true)
public class RunCommandStatement extends AbstractMongoDocumentStatement<Document> {

public static final String COMMAND_NAME = "runCommand";

protected Document command;
public class RunCommandStatement extends AbstractRunCommandStatement {

public RunCommandStatement(final String command) {
this(BsonUtils.orEmptyDocument(command));
}

public RunCommandStatement(final Document command) {
this.command = command;
}

@Override
public String getCommandName() {
return COMMAND_NAME;
}

@Override
public String toJs() {
return
"db."
+ getCommandName()
+ "("
+ ofNullable(command).map(Document::toJson).orElse(null)
+ ");";
}

@Override
public Document run(final MongoConnection connection) {
return connection.getDatabase().runCommand(command);
super(command);
}

}

0 comments on commit 5146509

Please sign in to comment.