I am trying to create indexes on a mongodb collection using mongock
version 4.3.8
. The mongockLock
and mongockChangeLog
collections are eventually created, but the migration itself does not run. mongockChangeLog
table is empty. Query db.BOOKS.getIndexes();
returns a single string: with an index for id
(the value of name
in it is equal to _id_
), which is most likely generated automatically. There are no obvious errors in the logs either.
Everything else seems to be set up correctly, I don't understand why my migration is not running.
build.gradle.kts:
implementation("com.github.cloudyrock.mongock:mongodb-springdata-v3-driver:4.3.8")
implementation("com.github.cloudyrock.mongock:mongock-spring-v5:4.3.8")
implementation(platform("com.github.cloudyrock.mongock:mongock-bom:4.3.8"))
application.yml:
mongock:
change-logs-scan-package:
- com.dreamland.configuration.migration
config file:
@EnableMongock
@Configuration
@EnableMongoAuditing
class MongoConfig
Migration file:
@ChangeLog(order = "1668471203")
class IndexChangeLog {
companion object : KLogging()
@ChangeSet(order = "1668471204", id = "1668471204_create_indexes", author = "Irish")
fun createIndexes(mongockTemplate: MongockTemplate) {
val indexOps = mongockTemplate.indexOps(BookObjectDocument::class.java)
Index().on("createdAt", Sort.Direction.DESC).let { indexOps.ensureIndex(it) }.also { log(it) }
Index().on("createdBy", Sort.Direction.ASC).let { indexOps.ensureIndex(it) }.also { log(it) }
Index().on("type", Sort.Direction.DESC).let { indexOps.ensureIndex(it) }.also { log(it) }
}
private fun log(indexName: String) {
logger.info { "Index '$indexName' was created successfully" }
}
}
Document class:
@Document(collection = "BOOKS_V2")
data class BookObjectDocument(
val type: BookObjectType?,
val description: String?
) {
@Id
lateinit var id: String
@CreatedDate
lateinit var createdAt: Instant
@CreatedBy
var createdBy: String? = null
}