liquibase-mongodb icon indicating copy to clipboard operation
liquibase-mongodb copied to clipboard

renameCollection feature

Open isalciuc opened this issue 1 year ago • 2 comments

I didn't find any way to rename a collection in MongoDB using Liquibase

isalciuc avatar May 18 '23 06:05 isalciuc

@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"
                }
              }
            }
          }
        ]
      }
    }
  ]
}

Ygilany avatar May 23 '23 01:05 Ygilany

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}")

isalciuc avatar May 23 '23 07:05 isalciuc