0

I am using liquibase and mongo to execute a rename-migration like that:

<ext:runCommand>
  <ext:command><![CDATA[
    {
      renameCollection: "XXX.foo",
      to: "XXX.bar"
    }
  ]]></ext:command>
</ext:runCommand>

The renaming happens within the bounds of the existing DB, so cross-db migrations are not relevant to my usecase. My problem is that I do now know XXX in advance. My liquibase migration is intended to run in multiple environmets and each one uses its unique version of XXX.

Also, liquibase limits me to runCommand/adminCommand semantics, and the spec for them clearly says that I should provide full namespaces for that, which I cannot have.

Of course I could create multiple liquibase change sets, one for each environment, and hardcode the proper namespace for each one. But I would like to avoid that option since it will does not scale very well.

Is there any way to rename a mongo collection (using runCommand/adminCommand semantics), in a namespace agnostic way?

Alkis Mavridis
  • 1,090
  • 12
  • 28
  • Before running the `renameCollection` run the `dbStats` command - the output document's `db` property has the database name. – prasad_ Apr 21 '21 at 13:01
  • How can I capture the values from dbStats within an XML migration in order to use them in renameCollection? – Alkis Mavridis Apr 21 '21 at 13:17
  • I am not familiar working with liquibase library. – prasad_ Apr 21 '21 at 13:22
  • 1
    Can you enter the database name as a parameter at the time of executing liquibase update and then use property substitution here with the specified parameter? I'm not sure if it will work but just a thought. – Rakhi Agrawal Apr 21 '21 at 13:25
  • 1
    @RakhiAgrawal it worked! Thanks a lot. Please put this in a comment so that I can mark it as an accepted answer. Just for the record, I use standalone liquibase, and I had to call liquibaseInstance.getChangeLogParameters().set("databaseName", myRuntimeDetectedName); – Alkis Mavridis Apr 21 '21 at 13:36
  • great. Happy that you got it working!! :) – Rakhi Agrawal Apr 21 '21 at 13:40

1 Answers1

1

Enter the database name as a parameter at the time of executing liquibase update and then use liquibase changelog property substitution in the changeset with the specified parameter. Should solve the problem.

Adding the way Alkis has achieved it as mentioned in comments :

Just for the record, I use standalone liquibase, and I had to call liquibaseInstance.getChangeLogParameters().set("databaseName", myRuntimeDetectedName);

Rakhi Agrawal
  • 827
  • 7
  • 14