forked from alexandru-slobodcicov/liquibase-mongodb
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
48dc184
commit f85f571
Showing
5 changed files
with
142 additions
and
24 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
src/main/java/liquibase/ext/mongodb/precondition/CollectionExistsPrecondition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package liquibase.ext.mongodb.precondition; | ||
|
||
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.MongoLiquibaseDatabase; | ||
import liquibase.ext.mongodb.statement.CountCollectionByNameStatement; | ||
import liquibase.precondition.AbstractPrecondition; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
import static java.lang.String.format; | ||
|
||
public class CollectionExistsPrecondition extends AbstractPrecondition { | ||
|
||
@Getter | ||
@Setter | ||
private String collectionName; | ||
|
||
@Override | ||
public String getName() { | ||
return "collectionExists"; | ||
} | ||
|
||
@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 CountCollectionByNameStatement countCollectionByNameStatement | ||
= new CountCollectionByNameStatement(collectionName); | ||
|
||
if (countCollectionByNameStatement.queryForLong((MongoLiquibaseDatabase) database) == 0L) { | ||
throw new PreconditionFailedException(format("Collection %s does not exist", collectionName), 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; | ||
} | ||
} |
3 changes: 2 additions & 1 deletion
3
src/main/resources/META-INF/services/liquibase.precondition.Precondition
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
liquibase.ext.mongodb.precondition.DocumentExistsPrecondition | ||
liquibase.ext.mongodb.precondition.ExpectedDocumentCountPrecondition | ||
liquibase.ext.mongodb.precondition.ExpectedDocumentCountPrecondition | ||
liquibase.ext.mongodb.precondition.CollectionExistsPrecondition |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters