简体   繁体   English

如何使Spring Boot使用MultiTenantSpringLiquibase?

[英]How to make Spring Boot use MultiTenantSpringLiquibase?

In my Spring Boot application, I want Liquibase to create tables on a PostgreSQL database, so in my configuration classes, I have set up a Bean that returns a MultiTenantSpringLiquibase containing schema names that it should use. 在我的Spring Boot应用程序中,我希望Liquibase在PostgreSQL数据库上创建表,因此在我的配置类中,我设置了一个Bean,该Bean返回一个MultiTenantSpringLiquibase,其中包含应该使用的架构名称。

The problem is that this Bean is created but ignored, Spring Boot finds a valid SpringLiquibase Bean in LiquibaseAutoConfiguration, and uses it on my DB, accessing the same changelog that I intended for MultiTenantSpringLiquibase. 问题是创建了该Bean,但忽略了它,Spring Boot在LiquibaseAutoConfiguration中找到了一个有效的SpringLiquibase Bean,并在我的DB上使用了它,访问了与MultiTenantSpringLiquibase相同的变更日志。 This fails because this configuration tries to use the schema "public", which does not exist. 之所以失败,是因为此配置尝试使用不存在的架构“ public”。 If it does exist, it creates the tables I've defined in my changelog, but I don't want them to appear in the "public" schema. 如果确实存在,它将创建我在变更日志中定义的表,但是我不希望它们出现在“公共”模式中。

How do I prevent LiquibaseAutoConfiguration or SpringLiquibase from executing, so that my MultiTenantSpringLiquibase Bean is used instead ? 如何防止执行LiquibaseAutoConfiguration或SpringLiquibase,以代替使用我的MultiTenantSpringLiquibase Bean?

In your application.properties, add this. 在您的application.properties中,添加它。

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration 

or in your Main class use this. 或在您的Main class中使用它。

@SpringBootApplication(exclude={LiquibaseAutoConfiguration.class})

You should have at least the SpringLiquibase bean for the default schema. 对于默认模式,至少应具有SpringLiquibase bean。 In this case, you can skip the execution for this bean, but need to declare it. 在这种情况下,您可以跳过该bean的执行,但需要声明它。

@Bean
public SpringLiquibase liquibase(@Qualifier("taskExecutor") TaskExecutor taskExecutor,
                                     DataSource dataSource, LiquibaseProperties liquibaseProperties) {
     SpringLiquibase liquibase = new AsyncSpringLiquibase(taskExecutor, env);
     liquibase.setDataSource(dataSource);
     liquibase.setChangeLog("path-to-xml");
     liquibase.setContexts(liquibaseProperties.getContexts());
     liquibase.setDefaultSchema(liquibaseProperties.getDefaultSchema());
    return liquibase;
}

After this configuration, you should be able to create the MultiTenantSpringLiquibase Bean to run in all schemas: 完成此配置后,您应该能够创建MultiTenantSpringLiquibase Bean以在所有模式中运行:

@Bean
@DependsOn("liquibase") // ensure execution after SpringLiquibase Bean
public MultiTenantSpringLiquibase liquibaseMt(DataSource dataSource, LiquibaseProperties liquibaseProperties) {

    MultiTenantSpringLiquibase liquibase = new MultiTenantSpringLiquibase();

    liquibase.setDataSource(dataSource);
    liquibase.setChangeLog("path-to-xml");
    liquibase.setDefaultSchema(YOUR_DEFAULT_SCHEMA);
    liquibase.setSchemas(YOUR_ARRAY_OF_SCHEMAS);
    return liquibase;
}

After the execution of the SpringLiquibase , MultiTenantSpringLiquibase will be called and executed in your schemas by the line liquibase.setSchemas(...) 在执行后SpringLiquibaseMultiTenantSpringLiquibase将被调用,通过该行的模式执行liquibase.setSchemas(...)

This sample is a working sample that I've used recently. 该示例是我最近使用的工作示例。

Full example class here . 完整的示例课在这里

Did you try adding this to your application.properties file: 您是否尝试将其添加到application.properties文件中:

#Disable Liquibase
spring.liquibase.enabled=false

If you don't disable it I believe this gets executed: 如果您不禁用它,我相信这将被执行:

https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration.java#L74 https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration。 Java的#L74

It's just an idea, I'm not sure but you can try since you have the environment and I can only speculate. 这只是一个主意,我不确定,但是您可以尝试一下,因为您拥有环境并且我只能推测。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM