Skip to content

Commit

Permalink
Issue 90: Convert DropCollection to execute as a runCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandru-slobodcicov committed Feb 16, 2021
1 parent e70fab4 commit fd09ffc
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ public class CreateCollectionStatement extends AbstractRunCommandStatement {

public static final String RUN_COMMAND_NAME = "create";

public CreateCollectionStatement(final String collectionName) {
this(collectionName, (String) null);
}

public CreateCollectionStatement(final String collectionName, final String options) {
this(collectionName, BsonUtils.orEmptyDocument(options));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
/**
* Creates a index via the database runCommand method
* For a list of supported options see the reference page:
* @see <a href="https://docs.mongodb.com/manual/reference/command/createIndexes//">createIndexes</a>
*
* @see <a href="https://docs.mongodb.com/manual/reference/command/createIndexes/">createIndexes</a>
*/
@Getter
@EqualsAndHashCode(callSuper = true)
Expand All @@ -45,11 +46,6 @@ public class CreateIndexStatement extends AbstractRunCommandStatement
private static final String KEY = "key";
private static final String INDEXES = "indexes";

@Override
public String getRunCommandName() {
return RUN_COMMAND_NAME;
}

public CreateIndexStatement(final String collectionName, final Document keys, final Document options) {
super(toCommand(RUN_COMMAND_NAME, collectionName, combine(keys, options)));
}
Expand All @@ -58,6 +54,11 @@ public CreateIndexStatement(final String collectionName, final String keys, fina
this(collectionName, orEmptyDocument(keys), orEmptyDocument(options));
}

@Override
public String getRunCommandName() {
return RUN_COMMAND_NAME;
}

private static Document combine(final Document key, final Document options) {

final Document indexDocument = new Document(KEY, key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,11 @@
* #L%
*/

import com.mongodb.client.MongoCollection;
import liquibase.ext.mongodb.database.MongoLiquibaseDatabase;
import liquibase.nosql.statement.NoSqlExecuteStatement;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import org.bson.Document;

import java.util.function.Consumer;

Expand All @@ -45,8 +43,8 @@ public String getCommandName() {
@Override
public void execute(final MongoLiquibaseDatabase database) {
getMongoDatabase(database).listCollectionNames()
.map(getMongoDatabase(database)::getCollection)
.forEach((Consumer<? super MongoCollection<Document>>) MongoCollection::drop);
.map(DropCollectionStatement::new)
.forEach((Consumer<? super DropCollectionStatement>) s -> s.execute(database));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,46 +20,32 @@
* #L%
*/

import com.mongodb.client.MongoCollection;
import liquibase.ext.mongodb.database.MongoLiquibaseDatabase;
import liquibase.nosql.statement.NoSqlExecuteStatement;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import org.bson.Document;

import static liquibase.ext.mongodb.statement.AbstractRunCommandStatement.SHELL_DB_PREFIX;
import static liquibase.ext.mongodb.statement.BsonUtils.toCommand;

/**
* Drops a collection via the database runCommand method
* For a list of supported options see the reference page:
*
* @see <a href="https://docs.mongodb.com/manual/reference/command/drop/">drop</a>
*/
@Getter
@EqualsAndHashCode(callSuper = true)
public class DropCollectionStatement extends AbstractCollectionStatement
public class DropCollectionStatement extends AbstractRunCommandStatement
implements NoSqlExecuteStatement<MongoLiquibaseDatabase> {

public static final String COMMAND_NAME = "drop";
public static final String RUN_COMMAND_NAME = "drop";

public DropCollectionStatement(final String collectionName) {
super(collectionName);
}

@Override
public String getCommandName() {
return COMMAND_NAME;
super(toCommand(RUN_COMMAND_NAME, collectionName, null));
}

@Override
public String toJs() {
return
SHELL_DB_PREFIX +
getCollectionName() +
"." +
getCommandName() +
"(" +
");";
public String getRunCommandName() {
return RUN_COMMAND_NAME;
}

@Override
public void execute(final MongoLiquibaseDatabase database) {
final MongoCollection<Document> collection = getMongoDatabase(database).getCollection(getCollectionName());
collection.drop();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ void getConfirmationMessage() {
.hasSize(1)
.first()
.isInstanceOf(DropCollectionStatement.class)
.returns("collection1", s -> ((DropCollectionStatement) s).getCollectionName());
.returns("drop", s -> ((DropCollectionStatement) s).getRunCommandName())
.returns("collection1", s -> ((DropCollectionStatement) s).getCommand().get("drop"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,43 @@
* #L%
*/

import com.mongodb.MongoCommandException;
import liquibase.ext.AbstractMongoIntegrationTest;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;

import static liquibase.ext.mongodb.TestUtils.COLLECTION_NAME_1;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;

class DropCollectionStatementIT extends AbstractMongoIntegrationTest {

@Test
void execute() {
mongoDatabase.createCollection(COLLECTION_NAME_1);
assertThat(mongoDatabase.listCollectionNames()).hasSize(1);

new DropCollectionStatement(COLLECTION_NAME_1).execute(database);
final List<String> collectionNames = new ArrayList<>();

new CreateCollectionStatement(COLLECTION_NAME_1).execute(database);
mongoDatabase.listCollectionNames().iterator().forEachRemaining(collectionNames::add);
assertThat(collectionNames).hasSize(1).containsExactly(COLLECTION_NAME_1);

final DropCollectionStatement dropCollectionStatement = new DropCollectionStatement(COLLECTION_NAME_1);
dropCollectionStatement.execute(database);
assertThat(mongoDatabase.listCollectionNames()).isEmpty();

// try to delete a non existing collection
assertThatExceptionOfType(MongoCommandException.class).isThrownBy(() -> dropCollectionStatement.execute(database))
.withMessageStartingWith("Command failed with error")
.withMessageContaining("NamespaceNotFound");
}

@Test
void toString1() {
final DropCollectionStatement statement = new DropCollectionStatement(COLLECTION_NAME_1);
assertThat(statement.toString())
.isEqualTo(statement.toJs())
.isEqualTo("db.collectionName.drop();");
.isEqualTo("db.runCommand({\"drop\": \"collectionName\"});");
}
}

0 comments on commit fd09ffc

Please sign in to comment.