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

Issue-74: createIndex with TTL (expireAfterSeconds) is ignored and no… #77

Merged
merged 1 commit 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
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ Liquibase turned to be the most feasible tool to extend as it allows to define c
## Release Notes

#### 4.2.2.1
* Fixed [Does it support preconditions](https://github.com/liquibase/liquibase-mongodb/issues/69)
* Added documentExistsPrecondition
* Fixed [Issue-69: Does it support preconditions](https://github.com/liquibase/liquibase-mongodb/issues/69)
* Added DocumentExistsPrecondition, ExpectedDocumentCountPrecondition
* Fixed [Issue-74: createIndex with TTL (expireAfterSeconds) is ignored and normal index created](https://github.com/liquibase/liquibase-mongodb/issues/74)

#### 4.2.2
* Support for Liquibase 4.2.2
Expand Down
45 changes: 29 additions & 16 deletions src/main/java/liquibase/ext/mongodb/statement/BsonUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,26 @@

import com.mongodb.DBRefCodecProvider;
import com.mongodb.MongoClientSettings;
import com.mongodb.client.model.*;
import com.mongodb.client.model.CreateCollectionOptions;
import com.mongodb.client.model.IndexOptions;
import com.mongodb.client.model.ValidationAction;
import com.mongodb.client.model.ValidationLevel;
import com.mongodb.client.model.ValidationOptions;
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;
Expand All @@ -43,12 +53,12 @@
public final class BsonUtils {

public static final DocumentCodec DOCUMENT_CODEC =
new DocumentCodec(fromProviders(
new UuidCodecProvider(UuidRepresentation.STANDARD),
new ValueCodecProvider(),
new BsonValueCodecProvider(),
new DocumentCodecProvider(),
new DBRefCodecProvider()));
new DocumentCodec(fromProviders(
new UuidCodecProvider(UuidRepresentation.STANDARD),
new ValueCodecProvider(),
new BsonValueCodecProvider(),
new DocumentCodecProvider(),
new DBRefCodecProvider()));

public static CodecRegistry uuidCodecRegistry() {
return CodecRegistries.fromRegistries(
Expand All @@ -59,19 +69,19 @@ public static CodecRegistry uuidCodecRegistry() {

public static Document orEmptyDocument(final String json) {
return (
ofNullable(trimToNull(json))
.map(s -> Document.parse(s, DOCUMENT_CODEC))
.orElseGet(Document::new)
ofNullable(trimToNull(json))
.map(s -> Document.parse(s, DOCUMENT_CODEC))
.orElseGet(Document::new)
);
}

public static List<Document> orEmptyList(final String json) {
return (
ofNullable(trimToNull(json))
.map(jn -> "{ items: " + jn + "}")
.map(s -> Document.parse(s, DOCUMENT_CODEC))
.map(d -> d.getList("items", Document.class, new ArrayList<>()))
.orElseGet(ArrayList::new)
ofNullable(trimToNull(json))
.map(jn -> "{ items: " + jn + "}")
.map(s -> Document.parse(s, DOCUMENT_CODEC))
.map(d -> d.getList("items", Document.class, new ArrayList<>()))
.orElseGet(ArrayList::new)
);
}

Expand Down Expand Up @@ -110,6 +120,9 @@ public static IndexOptions orEmptyIndexOptions(final Document options) {
if (options.containsKey("name")) {
indexOptions.name(options.getString("name"));
}
if (options.containsKey("expireAfterSeconds")) {
indexOptions.expireAfter(options.getLong("expireAfterSeconds"), TimeUnit.SECONDS);
}
return indexOptions;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.junit.jupiter.api.Test;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

import static java.util.stream.Collectors.toList;
import static liquibase.ext.mongodb.statement.BsonUtils.DOCUMENT_CODEC;
Expand Down Expand Up @@ -74,4 +75,11 @@ void uuidParseTest() {
//.isEqualTo(UUID.fromString("2e4933f2-0fd5-a2cd-96db-1dad099a5091"))
.isEqualTo(uuid1);
}

@Test
void orEmptyIndexOptionsTest() {
assertThat(BsonUtils.orEmptyIndexOptions(
BsonUtils.orEmptyDocument("{expireAfterSeconds: NumberLong(\"60\")}")).getExpireAfter(TimeUnit.SECONDS))
.isEqualTo(60L);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void execute() {
database.createCollection(COLLECTION_NAME_1);
database.getCollection(COLLECTION_NAME_1).insertOne(initialDocument);
final CreateIndexStatement createIndexStatement = new CreateIndexStatement(COLLECTION_NAME_1, "{ locale: 1 }",
"{ name: \"" + indexName + "\", unique: true}");
"{ name: \"" + indexName + "\", unique: true, expireAfterSeconds: NumberLong(\"30\") }");
createIndexStatement.execute(connection);

final Document document = StreamSupport.stream(database.getCollection(COLLECTION_NAME_1).listIndexes().spliterator(), false)
Expand All @@ -60,5 +60,6 @@ void execute() {

assertThat(document.get("unique")).isEqualTo(true);
assertThat(document.get("key")).isEqualTo(Document.parse("{ locale: 1 }"));
assertThat(document.get("expireAfterSeconds")).isEqualTo(30L);
}
}