Skip to content
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

Added ExpectedDocumentCountPrecondition #76

Merged
merged 2 commits into from
Jan 16, 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
@@ -0,0 +1,72 @@
package liquibase.ext.mongodb.precondition;

import org.bson.conversions.Bson;

import liquibase.changelog.ChangeSet;
import liquibase.changelog.DatabaseChangeLog;
import liquibase.changelog.visitor.ChangeExecListener;
import liquibase.database.Database;
import liquibase.exception.PreconditionErrorException;
import liquibase.exception.PreconditionFailedException;
import liquibase.exception.ValidationErrors;
import liquibase.exception.Warnings;
import liquibase.ext.mongodb.database.MongoConnection;
import liquibase.ext.mongodb.statement.BsonUtils;
import liquibase.ext.mongodb.statement.CountDocumentsInCollectionStatement;
import liquibase.precondition.AbstractPrecondition;
import lombok.Getter;
import lombok.Setter;

import static java.lang.String.format;

public class ExpectedDocumentCountPrecondition extends AbstractPrecondition{
@Getter
@Setter
private String collectionName;

@Getter
@Setter
private String filter;

@Getter
@Setter
private Long expectedCount;

@Override
public String getName() {
return "expectedDocumentCount";
}

@Override
public Warnings warn(final Database database) {
return new Warnings();
}

@Override
public ValidationErrors validate(final Database database) {
return new ValidationErrors();
}

@Override
public void check(final Database database, final DatabaseChangeLog changeLog, final ChangeSet changeSet,
final ChangeExecListener changeExecListener) throws PreconditionFailedException, PreconditionErrorException {
try {
final Bson bsonFilter = BsonUtils.orEmptyDocument(filter);
final CountDocumentsInCollectionStatement countDocumentsInCollectionStatement = new CountDocumentsInCollectionStatement(collectionName, bsonFilter);
Long actualDocumentCount = countDocumentsInCollectionStatement.queryForLong((MongoConnection) database.getConnection());
if (!actualDocumentCount.equals(expectedCount)) {
throw new PreconditionFailedException(format(
"ExpectedDocumentCount precondition fails for collection %s, expected: %s, actual: %s", collectionName, expectedCount, actualDocumentCount), changeLog, this);
}
} catch (final PreconditionFailedException e) {
throw e;
} catch (final Exception e) {
throw new PreconditionErrorException(e, changeLog, this);
}
}

@Override
public String getSerializedObjectNamespace() {
return GENERIC_CHANGELOG_EXTENSION_NAMESPACE;
}
}
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
liquibase.ext.mongodb.precondition.DocumentExistsPrecondition
liquibase.ext.mongodb.precondition.DocumentExistsPrecondition
liquibase.ext.mongodb.precondition.ExpectedDocumentCountPrecondition
11 changes: 11 additions & 0 deletions src/main/resources/liquibase.parser.core.xml/dbchangelog-ext.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,17 @@

</xsd:complexType>
</xsd:element>

<xsd:element name="expectedDocumentCount">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="filter" type="xsd:string" minOccurs="0" maxOccurs="1"/>
</xsd:sequence>

<xsd:attribute name="collectionName" type="xsd:string" use="required"/>
<xsd:attribute name="expectedCount" type="xsd:nonNegativeInteger" use="required"/>
</xsd:complexType>
</xsd:element>


</xsd:schema>
10 changes: 6 additions & 4 deletions src/test/java/liquibase/ext/MongoLiquibaseIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -248,15 +248,17 @@ void testPreconditions() {
liquibase.update("");

List<MongoRanChangeSet> changeSets = findAllRanChangeSets.queryForList(connection).stream().map(converter::fromDocument).collect(Collectors.toList());
assertThat(changeSets).hasSize(6)
assertThat(changeSets).hasSize(8)
.extracting(MongoRanChangeSet::getId, MongoRanChangeSet::getOrderExecuted, MongoRanChangeSet::getExecType)
.containsExactly(
tuple("1", 1, SKIPPED),
tuple("2", 2, EXECUTED),
tuple("3", 3, EXECUTED),
tuple("4", 4, SKIPPED),
tuple("5", 5, EXECUTED),
tuple("6", 6, EXECUTED)
tuple("6", 6, EXECUTED),
tuple("7", 7, SKIPPED),
tuple("8", 8, EXECUTED)
);

assertThat(getCollections(connection))
Expand All @@ -265,8 +267,8 @@ void testPreconditions() {

final FindAllStatement findAllResults = new FindAllStatement("results");
assertThat(findAllResults.queryForList(connection))
.hasSize(3).extracting(d -> d.get("info"))
.containsExactlyInAnyOrder("existsAnyDocumentInCollection1", "filterMatchedInCollection1", "changeSetExecutedMatch");
.hasSize(4).extracting(d -> d.get("info"))
.containsExactlyInAnyOrder("existsAnyDocumentInCollection1", "filterMatchedInCollection1", "changeSetExecutedMatch", "expectedDocumentCountfilterMatchedInCollection1");

}

Expand Down
10 changes: 6 additions & 4 deletions src/test/java/liquibase/ext/MongoLiquibaseJsonIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -184,15 +184,17 @@ void testPreconditions() {
liquibase.update("");

List<MongoRanChangeSet> changeSets = findAllRanChangeSets.queryForList(connection).stream().map(converter::fromDocument).collect(Collectors.toList());
assertThat(changeSets).hasSize(6)
assertThat(changeSets).hasSize(8)
.extracting(MongoRanChangeSet::getId, MongoRanChangeSet::getOrderExecuted, MongoRanChangeSet::getExecType)
.containsExactly(
tuple("1", 1, SKIPPED),
tuple("2", 2, EXECUTED),
tuple("3", 3, EXECUTED),
tuple("4", 4, SKIPPED),
tuple("5", 5, EXECUTED),
tuple("6", 6, EXECUTED)
tuple("6", 6, EXECUTED),
tuple("7", 7, SKIPPED),
tuple("8", 8, EXECUTED)
);

assertThat(getCollections(connection))
Expand All @@ -201,8 +203,8 @@ void testPreconditions() {

final FindAllStatement findAllResults = new FindAllStatement("results");
assertThat(findAllResults.queryForList(connection))
.hasSize(3).extracting(d -> d.get("info"))
.containsExactlyInAnyOrder("existsAnyDocumentInCollection1", "filterMatchedInCollection1", "changeSetExecutedMatch");
.hasSize(4).extracting(d -> d.get("info"))
.containsExactlyInAnyOrder("existsAnyDocumentInCollection1", "filterMatchedInCollection1", "changeSetExecutedMatch", "expectedDocumentCountFilterMatchedInCollection1");

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,5 +165,68 @@
</ext:insertOne>

</changeSet>

<changeSet id="7" author="alex">
<preConditions onFail="CONTINUE" onError="HALT">
<ext:expectedDocumentCount collectionName="collection1">
<ext:filter>
<!--@formatter:off-->
{
"$or" :
[
{"id": 100}, {"id": {"$lt": 10}}
]
}
<!--@formatter:on-->
</ext:filter>
<ext:expectedCount>0</ext:expectedCount>
</ext:expectedDocumentCount>
</preConditions>
<comment>Will not be inserted as filter not matches</comment>

<ext:insertOne collectionName="results">

<ext:document>
<!--@formatter:off-->
{
info: "expectedDocumentCountfilterNotMatchedInCollection1"
}
<!--@formatter:on-->
</ext:document>

</ext:insertOne>

</changeSet>

<changeSet id="8" author="alex">
<preConditions onFail="CONTINUE" onError="HALT">
<ext:expectedDocumentCount collectionName="collection1">
<ext:filter>
<!--@formatter:off-->
{
"$or" :
[
{"id": 100}, {"id": {"$lt": 10}}
]
}
<!--@formatter:on-->
</ext:filter>
<ext:expectedCount>1</ext:expectedCount>
</ext:expectedDocumentCount>
</preConditions>
<comment>Will be inserted as filter matches</comment>

<ext:insertOne collectionName="results">

<ext:document>
<!--@formatter:off-->
{
info: "expectedDocumentCountfilterMatchedInCollection1"
}
<!--@formatter:on-->
</ext:document>

</ext:insertOne>

</changeSet>
</databaseChangeLog>
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,94 @@
}
]
}
},
{
"changeSet": {
"id": "7",
"author": "alex",
"preConditions": [
{
"onFail": "CONTINUE"
},
{
"expectedDocumentCount": {
"collectionName": "collection1",
"filter": {
"$rawJson": {
"$or": [
{
"id": 100
},
{
"id": {
"$lt": 10
}
}
]
}
},
"expectedCount":0
}
}
],
"comment": "Will not be inserted as filter not matches",
"changes": [
{
"insertOne": {
"collectionName": "results",
"document": {
"$rawJson": {
"info": "expectedDocumentCountFilterNotMatchedInCollection1"
}
}
}
}
]
}
},
{
"changeSet": {
"id": "8",
"author": "alex",
"preConditions": [
{
"onFail": "CONTINUE"
},
{
"expectedDocumentCount": {
"collectionName": "collection1",
"filter": {
"$rawJson": {
"$or": [
{
"id": 100
},
{
"id": {
"$lt": 10
}
}
]
}
},
"expectedCount":1
}
}
],
"comment": "Will be inserted as filter matches",
"changes": [
{
"insertOne": {
"collectionName": "results",
"document": {
"$rawJson": {
"info": "expectedDocumentCountFilterMatchedInCollection1"
}
}
}
}
]
}
}
]
}