1

我正在尝试运行 mongock 以使用版本控制来处理我的数据库迁移,但是从 mongock 基础架构中得到了一个异常,另外请注意我没有使用 spring-boot 只使用 spring bean 系统,我没有选择使用 spring boot。 ..

Pom 和代码片段:

            <dependency>
                <groupId>com.github.cloudyrock.mongock</groupId>
                <artifactId>mongock-standalone</artifactId>
                <version>4.1.19</version>
            </dependency>
            <dependency>
                <groupId>com.github.cloudyrock.mongock</groupId>
                <artifactId>mongodb-springdata-v2-driver</artifactId>
                <version>4.1.19</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.data</groupId>
                <artifactId>spring-data-mongodb</artifactId>
                <version>2.1.5.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.mongodb</groupId>
                <artifactId>mongo-java-driver</artifactId>
                <version>3.8.2</version>
            </dependency>

代码:

@Configuration
@Import({MongoSelectorContext.class})
public class MongockBeans {

    private final MongoSelectorContext mongoSelectorContext;

    public MongockBeans(MongoSelectorContext mongoSelectorContext) {
        this.mongoSelectorContext = mongoSelectorContext;
    }

    @Bean
    public void updateMongoTemplateSchema() {
        MongoTemplate mongoTemplate = mongoSelectorContext.mongoTemplateProvider().getMongoTemplateByDatabaseAlias("test");
        MongockStandalone.builder()
                .setDriver(SpringDataMongo2Driver.withDefaultLock(mongoTemplate))
                .addChangeLogClass(TestChangeLog.class)
                .buildRunner().execute();
    }
}

Mongo 模板按应有的方式提供 但我收到以下错误,错误是从 mongock 而不是我的变更集引发的,它甚至不执行我的变更集。任何帮助都会有帮助

Exception in thread "main" java.lang.RuntimeException: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongockBeans': Invocation of init method failed; nested exception is io.changock.migration.api.exception.ChangockException: Error in method[MongockV3LegacyMigrationChangeRunAlwaysLog.mongockSpringLegacyMigration] : Wrong parameter[MongockLegacyMigration] with name: legacy-migration
    at com.citi.qi.core.platform.CorePlatformLauncher.start(CorePlatformLauncher.java:39)
    at com.citi.quickinsight.evaluation.engine.EngineMain.main(EngineMain.java:35)
...
Caused by: io.changock.migration.api.exception.ChangockException: Error in method[MongockV3LegacyMigrationChangeRunAlwaysLog.mongockSpringLegacyMigration] : Wrong parameter[MongockLegacyMigration] with name: legacy-migration
    at io.changock.runner.core.MigrationExecutor.processExceptionOnChangeSetExecution(MigrationExecutor.java:195)
    at io.changock.runner.core.MigrationExecutor.processSingleChangeSet(MigrationExecutor.java:102)
    at io.changock.runner.core.MigrationExecutor.lambda$processSingleChangeLog$2(MigrationExecutor.java:94)
    at io.changock.runner.core.MigrationExecutor.executeInTransactionIfStrategyOrUsualIfNot(MigrationExecutor.java:80)
    at io.changock.runner.core.MigrationExecutor.processSingleChangeLog(MigrationExecutor.java:94)
    at io.changock.runner.core.MigrationExecutor.lambda$processAllChangeLogs$1(MigrationExecutor.java:88)
    at io.changock.runner.core.MigrationExecutor.executeInTransactionIfStrategyOrUsualIfNot(MigrationExecutor.java:80)
    at io.changock.runner.core.MigrationExecutor.processAllChangeLogs(MigrationExecutor.java:88)
    at io.changock.runner.core.MigrationExecutor.lambda$executeMigration$0(MigrationExecutor.java:69)
    at io.changock.runner.core.MigrationExecutor.executeInTransactionIfStrategyOrUsualIfNot(MigrationExecutor.java:80)
    at io.changock.runner.core.MigrationExecutor.executeMigration(MigrationExecutor.java:69)
    at io.changock.runner.core.ChangockBase.execute(ChangockBase.java:49)
    at com.citi.quickinsight.MongockBeans.updateMongoTemplateSchema(MongockBeans.java:29)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:389)
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:333)
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:157)
    ... 14 more
Caused by: io.changock.driver.api.common.DependencyInjectionException: Wrong parameter[MongockLegacyMigration] with name: legacy-migration
    at io.changock.runner.core.MigrationExecutor.lambda$getParameter$4(MigrationExecutor.java:182)
    at java.util.Optional.orElseThrow(Optional.java:290)
    at io.changock.runner.core.MigrationExecutor.getParameter(MigrationExecutor.java:182)
    at io.changock.runner.core.MigrationExecutor.executeChangeSetMethod(MigrationExecutor.java:171)
    at io.changock.runner.core.MigrationExecutor.executeAndLogChangeSet(MigrationExecutor.java:127)
    at io.changock.runner.core.MigrationExecutor.processSingleChangeSet(MigrationExecutor.java:100)
    ... 32 more
4

2 回答 2

2

这需要深入研究,但我猜你在开发环境中,你可以尝试这个解决方法,这只是暂时让你继续工作。

private MongockLegacyMigration getLegacyMigration() {
    MongockLegacyMigration mongockLegacyMigration = new MongockLegacyMigration();
    mongockLegacyMigration.setCollectionName("no-existing-collection");
    return mongockLegacyMigration;
}

@PostConstruct
public MongockStandalone.Runner updateMongoTemplateSchema(MongoTemplate mongoTemplate) {
    return MongockStandalone.builder()
            .setDriver(SpringDataMongo2Driver.withDefaultLock(mongoTemplate))
            .addChangeLogClass(ClientInitializerChangeLog.class)
            .setLegacyMigration(getLegacyMigration())
            .buildRunner();
}

请注意,您提供的是不存在的 legacyMigration。这不是正确/最终的解决方案,但不会受到伤害,并且由于您处于开发阶段(我的假设),这可以让您继续工作。

于 2020-11-23T11:27:14.323 回答
2

虽然之前的解决方法有效,但这是由于 Mongock 错误#268导致的,该错误已在版本 4.2.3.BETA 中修复。

实际的问题是,当构建器中没有设置包时,反射库会得到一个空的包列表,而不是什么都不返回,它会返回类路径中的所有内容。

由于 99% 没有注释,它们被忽略,但它会找到遗留迁移并运行它。

解决方案:如果包列表为空,请避免调用反射库

于 2020-11-24T08:53:28.413 回答