mongock迁移无法运行mongockspringlegacymigration

kmb7vmvb  于 2021-07-11  发布在  Java
关注(0)|答案(2)|浏览(354)

我试图运行mongock来处理我的db迁移和版本控制,但是mongock基础设施出现了一个异常,也请注意我没有使用springboot,只有springbean系统,我没有选择使用springboot。。。
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
jtjikinw

jtjikinw1#

这需要更深入地研究,但我猜您是在开发环境中,您可以尝试这个变通方法,这只是暂时的,您可以继续工作。

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。这不是正确的/最终的解决方案,但不会造成伤害,因为您正在开发(我的假设),这允许您继续工作。

vdgimpew

vdgimpew2#

虽然以前的解决方案可以工作,但这是由于mongock bug#268,它已在4.2.3.beta版中修复。
实际问题是,当生成器中没有设置包时,反射库会得到一个空包列表,而不是返回nothing时,它会返回类路径中的所有内容。
由于99%没有注解,它们被忽略,但它会找到遗留迁移并运行它。
解决方案:如果包列表为空,请避免调用反射库

相关问题