你能用Liquibase初始化Spring Batch元数据表吗?

wmtdaxz3  于 2023-04-30  发布在  Spring
关注(0)|答案(3)|浏览(102)

目前我有一个像下面这样的设置。在本地运行批处理作业时,作业将使用data-source属性值自动创建必要的元数据表,因为initialize-schema设置为始终。Liquibase还将运行并创建其更新日志中列出的任何表。
这里是我的application.yml文件

spring:
  batch:
    initialize-schema: always
    job:
      enabled: true
  liquibase:
    url: db_url
    user: deploy_user
    password: deploy_pass
    change-log: classpath:db/changelog/db.changelog-master.yaml
    enabled: true
data-source:
  mysql:
    user: r_user
    password: r_pass
    jdbc-url: db_url

这是我的db.changelog-master.yaml文件。

databaseChangeLog:

  - changeSet:
    dbms: mysql
    id: create-sample-table
    author: me
    sql: CREATE TABLE sample_table (
      sample_id VARCHAR(255) NOT NULL,
      sample_text TEXT,
      PRIMARY KEY (samoke_id)
      ) ENGINE=InnoDB DEFAULT
      CHARSET=utf8 COLLATE=utf8_bin;

MySQL数据源配置:

@Configuration
public class DataSourceConfiguration {

    @Primary
    @Bean(name = "mySQLDataSource")
    @ConfigurationProperties("data-source.mysql")
    public DataSource mySQLDataSource() {
        return DataSourceBuilder.create().type(HikariDataSource.class).build();
    }
}

Liquibase配置(可能发布的内容比需要的内容更多):

@Configuration
@EnableConfigurationProperties(LiquibaseProperties.class)
public class LiquibaseConfiguration {
    private static final Logger LOG = LoggerFactory.getLogger(LiquibaseConfiguration.class);

    @Autowired
    private LiquibaseProperties liquibaseProperties;

    public DataSource liquibaseDataSource() {
        DataSourceBuilder factory = DataSourceBuilder
                .create()
                .url(liquibaseProperties.getUrl())
                .username(liquibaseProperties.getUser())
                .password(liquibaseProperties.getPassword());

        return factory.build();
    }

    public void testLiquibaseConnection() throws SQLException {

        LOG.info("Testing connection to Liquibase (in case PCF restarts and we have stale dynamic secrets)...");
        liquibaseDataSource().getConnection();
        LOG.info("Testing connection to Liquibase (in case PCF restarts and we have stale dynamic secrets)... Succeeded");
    }

    @Bean
    public SpringLiquibase liquibase() {
        try {
            testLiquibaseConnection();
        } catch (Exception ex) {
            LOG.warn("WARNING: Could not connect to the database using " + liquibaseProperties.getUser() + ", so we will be skipping the Liquibase Migration for now. ", ex);
            return null;
        }
        SpringLiquibase liquibase = new SpringLiquibase();
        liquibase.setChangeLog(this.liquibaseProperties.getChangeLog());
        liquibase.setContexts(this.liquibaseProperties.getContexts());
        liquibase.setDataSource(liquibaseDataSource());
        liquibase.setDefaultSchema(this.liquibaseProperties.getDefaultSchema());
        liquibase.setDropFirst(this.liquibaseProperties.isDropFirst());
        liquibase.setShouldRun(this.liquibaseProperties.isEnabled());
        liquibase.setLabels(this.liquibaseProperties.getLabels());
        liquibase.setChangeLogParameters(this.liquibaseProperties.getParameters());
        return liquibase;
    }

}

问题是我们在部署的环境中创建/部署表和阅读/写表的凭据不同。因此,下面的设置将通过Liquibase创建表,但由于部署时的凭据不正确,创建元数据表失败。我们目前的解决方法是使用具有部署凭证的data-source属性进行部署,运行作业以初始化表,然后使用读/写凭证进行重新部署。(我们不能只为读取保留部署凭据,因为它们的TTL非常短)。
是否可以通过Liquibase自动创建Spring Batch的元数据表?具体地说,不需要手动将创建SQL添加到更改日志文件中?

更新:

使用veljkost下面的答案,有一个看起来像这样的更改日志文件可以工作:

databaseChangeLog:
  - changeSet:
      dbms: mysql
      id: create-spring-batch-metadata
      author: dev.me
      changes:
        - sqlFile:
            encoding: UTF-8
            path: classpath:/org/springframework/batch/core/schema-mysql.sql
            relativeToChangelogFile: false
            splitStatements: true
            stripComments: true
lokaqttq

lokaqttq1#

是的,您可以引用Spring Batch项目中已经存在的模式文件。在org.springframework.batch.core包中,你可以找到schema-*。sql文件,其中 * 是目标数据库的名称。因为你运行在mysql上,你的更改集看起来像这样:

- changeSet:
  id: 1234
  author: adam.sandler
  changes:
      - sqlFile:
            encoding: utf8
            path: classpath:/org/springframework/batch/core/schema-mysql.sql
            relativeToChangelogFile: false
            splitStatements: true
            stripComments: true
zdwk9cvp

zdwk9cvp2#

自动迁移到数据库而不使用liquabase添加

spring.batch.initialize-schema=always

到你的 www.example.com 文件,它将自动迁移到嵌入的数据源

yqlxgs2m

yqlxgs2m3#

对于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"
    logicalFilePath="/common/run-once/feature-base.xml"
    xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog 
                             http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.0.xsd">
    <changeSet author="tim.doege" id="feature/spring-batch">
        <sqlFile dbms="mysql" encoding="UTF-8"
            path="classpath:/org/springframework/batch/core/schema-mysql.sql"
            relativeToChangelogFile="false" splitStatements="true"
            endDelimiter=";" stripComments="true" />
    </changeSet>
</databaseChangeLog>

相关问题