liquibase-mongodb
liquibase-mongodb copied to clipboard
renameCollection feature
I didn't find any way to rename a collection in MongoDB using Liquibase
@isalciuc renaming a collection is an adminCommand.
Here's an example for how you can do that
{
"databaseChangeLog": [
{
"changeSet": {
"id": "2023.05.22.1",
"author": "Yahya Gilany",
"comment": "Rename collections",
"changes": [
{
"adminCommand": {
"command": {
"$rawJson": {
"renameCollection": "database-name.old-collection-name",
"to": "database-name.new-collection-name"
}
}
}
}
]
}
}
]
}
It fails with a not very informative NPE exception:
Caused by: java.lang.NullPointerException: null at liquibase.ext.mongodb.statement.AdminCommandStatement.run(AdminCommandStatement.java:51) at liquibase.ext.mongodb.statement.AbstractRunCommandStatement.execute(AbstractRunCommandStatement.java:51) at liquibase.ext.mongodb.statement.AbstractRunCommandStatement.execute(AbstractRunCommandStatement.java:36) at liquibase.nosql.executor.NoSqlExecutor.execute(NoSqlExecutor.java:187)
It also requires to run it as admin and to know the database name.
On the other hand, this command doesn't require to run as admin and to know the database name:
db.{source-collection-name}.renameCollection("{new-collection-name}")