【问题标题】:Liquibase syntax for runCommand in MongoDBMongoDB 中 runCommand 的 Liquibase 语法
【发布时间】:2021-09-25 09:12:05
【问题描述】:

我正在尝试将 liquibase 添加到我的项目中。我需要在创建索引和分片键的同时创建一个集合。我在 Mongo shell 中使用下一个命令,它工作正常:

db.runCommand{customAction: "CreateCollection", collection: "name", offerThroughput: 400, shardKey: "partition_key", 索引: [{key: {_id: 1}, name: "_id_1"}, {key : {partition_key: 1, some_key: 1}, name: "partition_key_1_some_key_1", unique: true}]};

offerThroughput 密钥特定​​于我在项目中使用的 CosmosDB。

我尝试了下一个选项:

我的配置:

pom.xml:

    <properties>
            <liquibase.version>4.4.0</liquibase.version>
            <liquibase-maven-plugin.version>4.4.0</liquibase-maven-plugin.version>
            <liquibase-mongodb.version>4.4.0</liquibase-mongodb.version>
    </properties>
    
            <dependency>
                <groupId>org.liquibase.ext</groupId>
                <artifactId>liquibase-mongodb</artifactId>
                <version>${liquibase-mongodb.version}</version>
            </dependency>
            <dependency>
                <groupId>org.liquibase</groupId>
                <artifactId>liquibase-maven-plugin</artifactId>
                <version>${liquibase-maven-plugin.version}</version>
            </dependency>
    
     <build>
      <plugins>
            <plugin>
                    <groupId>org.liquibase</groupId>
                    <artifactId>liquibase-maven-plugin</artifactId>
                    <version>${liquibase-maven-plugin.version}</version>
                    <configuration>
                        <propertyFile>src/main/resources/liquibase.properties</propertyFile>
                    </configuration>
                    <dependencies>
                        <dependency>
                            <groupId>org.liquibase.ext</groupId>
                            <artifactId>liquibase-mongodb</artifactId>
                            <version>${liquibase-mongodb.version}</version>
                        </dependency>
                        <dependency>
                            <groupId>org.mongodb</groupId>
                            <artifactId>mongodb-driver-sync</artifactId>
                            <version>4.2.3</version>
                            <scope>compile</scope>
                        </dependency>
                    </dependencies>
                </plugin>
      </plugins>
     </build>

根据文档(https://github.com/alexandru-slobodcicov/liquibase-mongodb#implemented-changes),我们可以使用“runCommand”。 另外,我找到了一个示例(https://github.com/liquibase/liquibase-mongodb/tree/main/src/test/resources/liquibase/ext)并尝试使用它(changelog.run-command.test.xml):

 <changeSet id="1" author="alex">

        <ext:runCommand>

            <ext:command>
                { buildInfo: 1 }
            </ext:command>

        </ext:runCommand>

    </changeSet>

我尝试使用下一个更改日志文件配置运行 liquibase: db.changelog-master.xml:

<?xml version="1.0" encoding="UTF-8"?>

<databaseChangeLog
        xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
        xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
        http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.3.xsd
        http://www.liquibase.org/xml/ns/dbchangelog-ext
        http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">

    <changeSet id="1" author="author">

            <ext:runCommand>
                <ext:command>
                    <![CDATA[ {customAction: "CreateCollection", collection: "name", offerThroughput: 400, shardKey: "partition_key", indexes: [{key: {_id: 1}, name: "_id_1"}, {key: {partition_key: 1, some_key: 1}, name: "partition_key_1_some_key_1", unique: true}]}]]>
                </ext:command>
            </ext:runCommand>

    </changeSet>

</databaseChangeLog>

还有下一个:

<databaseChangeLog
        xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
        xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
        http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.3.xsd
        http://www.liquibase.org/xml/ns/dbchangelog-ext
        http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">

    <changeSet id="1" author="author">

            <ext:runCommand>
                <ext:command>
                    {customAction: "CreateCollection", collection: "name", offerThroughput: 400, shardKey: "partition_key", indexes: [{key: {_id: 1}, name: "_id_1"}, {key: {partition_key: 1, some_key: 1}, name: "partition_key_1_some_key_1", unique: true}]}
                </ext:command>
            </ext:runCommand>

    </changeSet>

</databaseChangeLog>

我所有的尝试都失败了。我没有找到任何使用此类命令的示例。如果有人能分享正确的语法来在 MongoDB 上为 liquibase 运行这样的命令,我将不胜感激。

【问题讨论】:

    标签: mongodb liquibase


    【解决方案1】:

    我看到您在 liquibase 论坛上发布了相同的问题。另外,您的帖子似乎很活跃。我建议关注 liquibase 论坛post

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多