Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/rveck/liquibase-mongodb int…
Browse files Browse the repository at this point in the history
…o rveck-main
  • Loading branch information
nvoxland committed May 24, 2022
2 parents 1bb1b92 + 8f860cd commit a52073e
Showing 1 changed file with 9 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,16 @@ public int update(final MongoLiquibaseDatabase database) {
, MongoChangeLogLock.formLockedBy(), locked);
final Document inputDocument = new MongoChangeLogLockToDocumentConverter().toDocument(entry);
inputDocument.put(MongoChangeLogLock.Fields.locked, locked);

long qtDocuments = database.getMongoDatabase().getCollection(collectionName).countDocuments();
boolean upsert = qtDocuments == 0;

final Optional<Document> changeLogLock = Optional.ofNullable(
database.getMongoDatabase().getCollection(collectionName)
.findOneAndReplace(Filters.eq(MongoChangeLogLock.Fields.id, entry.getId()), inputDocument,
new FindOneAndReplaceOptions().upsert(true).returnDocument(ReturnDocument.AFTER))
database.getMongoDatabase()
.getCollection(collectionName)
.findOneAndReplace(Filters.eq(MongoChangeLogLock.Fields.id,
entry.getId()), inputDocument,
new FindOneAndReplaceOptions().upsert(upsert).returnDocument(ReturnDocument.AFTER))
);
return changeLogLock.map(e -> 1).orElse(0);
}
Expand Down

0 comments on commit a52073e

Please sign in to comment.