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

Is adminCommand really connected to the admin DB? #111

Closed
DarrinProtag opened this issue Mar 9, 2021 · 12 comments · Fixed by #112
Closed

Is adminCommand really connected to the admin DB? #111

DarrinProtag opened this issue Mar 9, 2021 · 12 comments · Fixed by #112

Comments

@DarrinProtag
Copy link

DarrinProtag commented Mar 9, 2021

I'm trying to use runCommand or adminCommand to invoke shardCollection. I'm on version 4.3.1.

I get this error from Mongo: 'shardCollection may only be run against the admin database', from this:
adminCommand(command: '{shardCollection: 'darrin.player_info_static', key: '{location: 1, _id: 1}', unique: true} ')
Same as if I'd used runCommand.

Your AdminCommandStatement.run() calls: database.getMongoDatabase().runCommand(command);
I'd expect it to lookup the admin DB.

And I can't figure out how to invoke db.adminCommand() on Mongo using runCommand (as a workaround).

┆Issue is synchronized with this Jira Bug by Unito

@DarrinProtag
Copy link
Author

DarrinProtag commented Mar 11, 2021

I think you want something like this in AdminCommandStatement.java (I need to figure out how to test this change):
(deleted incorrect code)

@DarrinProtag
Copy link
Author

Sorry, I was browsing the older code base. I think this might work:

    public Document run(final MongoLiquibaseDatabase database) {
        MongoDatabase adminDB = ((MongoConnection)database.getConnection()).getMongoClient().getDatabase(database.getSystemSchema());
        return adminDB.runCommand(command);
    }

@DarrinProtag
Copy link
Author

To add some detail, here is the command I was attempting:

adminCommand(command: '{shardCollection: "darrin.player_info_static", key: {location: 1, _id: 1}, unique: true} ')

Also, I forked the project and made the patch I suggested above, and it appears to be working!

@alexandru-slobodcicov
Copy link

alexandru-slobodcicov commented Mar 14, 2021

Thank you @DarrinProtag, will go on with your solution if turns out it is required for adminCommand.

mongo documentation says that it basically does the same you described:
https://docs.mongodb.com/manual/reference/method/db.adminCommand/

db.adminCommand runs commands against the admin database regardless of the database context in which it runs. The following commands are equivalent:
db.getSiblingDB("admin").runCommand(<command>) db.adminCommand(<command>)

And yes because of that java driver do not contain api for adminCommand we have to run it via runCommand on admin db.
Will add integration tests examples and probably will use properties to provide db name so that liquibase changes are portable and db name not hardcoded

@alexandru-slobodcicov
Copy link

Merged will be available in 4.3.1.1 when released

@alexandru-slobodcicov
Copy link

@DarrinProtag
Copy link
Author

Hi @DarrinProtag please note https://github.com/liquibase/liquibase-mongodb/releases/tag/liquibase-mongodb-4.3.1.1 is released. Could you please try it out?

No problem. I switched my build to this released version, and it works fine. Thank you. I am now able to set up sharding, shard my collections, and set the shard keys!

For the next person...I'm using groovy formatted changelogs, and doing things like this... And need no manual setup steps, now.

        adminCommand(command: '{enableSharding: "${database.name}" } ')
        adminCommand(command: '{shardCollection: "${database.name}.player_info_static", key: {location: 1, _id: 1}, unique: true} ')

@Yuriy-Lukashevich
Copy link

Yuriy-Lukashevich commented Jul 15, 2021

Hi @DarrinProtag. I'm trying to add luquibase to my project. We are using Cosmosdb Mongodb API.
I would appreciate it very much if you could share how to add to xml or json script operation like this:
db.runCommand({customAction: "CreateCollection", collection: "name", offerThroughput: 400, shardKey: "partition_key", indexes: [{key: {_id: 1}}, {key: {partition_key: 1, some_key: 1},unique: true}]});

@DarrinProtag
Copy link
Author

https://docs.liquibase.com/workflows/database-setup-tutorials/mongodb.html - if you twist open the Examples section, there are examples of XML commands to create collections. I haven't found great examples of json (or gradle; which we use) formatted commands. I just tried permutations of syntax until it started making sense to me.

These are the libraries we included:
plugins{
id 'org.liquibase.gradle' version '2.0.4'
}
dependencies{
liquibaseRuntime "org.liquibase.ext:liquibase-mongodb:${project.liquibaseMongodbVersion}" //4.3.1
liquibaseRuntime "org.liquibase:liquibase-core:${project.liquibaseCoreVersion}" //4.3.1
liquibaseRuntime "org.liquibase:liquibase-groovy-dsl:${project.liquibaseGroovyVersion}" // 3.0.0
}

I assume there is a json-dsl library you'd need to include if you wanted to parse json command files.

I assume cosmo will accept a regular mongo connection string, as opposed to needing a custom cosmo sdk compiled against liquibase. No doubt, Azure (or whoever) wants fancy auth set up locally before it will respond, though.

To run stuff from gradle build files...
// Run liquibase.update to get schema/indexes up to date.
// In the future, to manage multiple clusters in one mongo,
// use -PrunList stuff from the docs: https://github.com/liquibase/liquibase-gradle-plugin
liquibase {
activities {
local {
// parameters in each activity are from -- for liquibase...
// https://docs.liquibase.com/commands/home.html
changeLogFile 'datastore/src/main/resources/liquibase/changelogs.groovy'
dataOutputDirectory 'datastore/src/main/resources/liquibase/changelog_out'
url 'mongodb://localhost:27017/darrin'
// username ''
// password ''

        // Needs to run with -Ddatabase.name=darrin to fix up changelog contents (e.g. stuff like "${database.name}.unique_item"}.
        changeLogParameters([ "database.name" : "darrin"])
        classPath "$rootDir"
    }
}

}

Sorry I can't be more help about the json format.

@Yuriy-Lukashevich
Copy link

Yuriy-Lukashevich commented Jul 16, 2021

Darrin thank you for your reply. I red tutorial, but it has no useful examples in my case. I have no issues with connection or running simple commands on CosmosDB MongoDB, but runCommand with customAction won't work.

@DarrinProtag
Copy link
Author

Its a bit of a leap into deep water, but the liquibase-mongo source is online. If you spend awhile staring at it, you might figure out whether customAction is supported or if there is a generic command that you could send it through. And if not, maybe you could see how to add a new command that supports it. Alexandru was quite kind about taking suggested changes, so you may not have to use a private fork for very long.

https://github.com/liquibase/liquibase-mongodb/tree/main/src/main/java/liquibase/ext/mongodb

@nwinkler
Copy link

@Yuriy-Lukashevich I'm trying to run Liquibase-MongoDB on CosmosDB, but I'm running into some errors when Liquibase is starting up, complaining about the collMod command not being supported on CosmosDB.

Did you have to use any special configuration for Liquibase to make it work on CosmosDB?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants