Flyway spring 引导应用程序在启动时不应用插入脚本

Flyway spring boot app doesn't apply insert script upon starting

我有一个 spring-boot JPA 应用程序,我正尝试将其与 flyway 集成。我的应用程序启动正常,它 运行 在我的本地数据库中创建模式 (V1_somescript.sql),但它不 运行 或应用插入 (V2_insert_script.sql) 脚本。这是我的配置:

pom.xml 飞路:

 <dependency>
    <groupId>org.flywaydb</groupId>
    <artifactId>flyway-core</artifactId>
    <version>6.1.4</version>
 </dependency>

application.properties 文件:

# ===============================
# JPA / HIBERNATE / FLYWAY
# ===============================

spring.jpa.hibernate.ddl-auto=update
spring.flyway.check-location=false
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL55Dialect
spring.jpa.properties.hibernate.generate_statistics=true

# ===============================
# DataSource
# ===============================
spring.flyway.locations=classpath*:resources/db/migrations
spring.datasource.url=jdbc:mysql://localhost:3306:3306/my_schema?useTimezone=true&serverTimezone=UTC
spring.datasource.username=my_username
spring.datasource.password=my_password

这是 V1_create_schema.sql 脚本:

CREATE TABLE `lang` (
  `lang` varchar(6) NOT NULL,
  `name` varchar(20) NOT NULL,
  PRIMARY KEY (`lang`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

这是 V2_insert_schema.sql 脚本:

INSERT IGNORE INTO `lang`(`lang`, `name`)
VALUES  ('ar-AE', 'Arabic'),
  ('cs-CZ', 'Czech'),
  ('da-DK', 'Danish'),
  ('de-DE', 'German'),
  ('el-GR', 'Greek'),
  ('en-GB', 'English (UK)'),
  ('en-US', 'English (US)');

此外,这是我的 AppConfig.java:

中的 bean 定义
  @Bean
  @Profile(value = {"default", "memory"})
  public Flyway flyway(DataSourceProperties dataSourceProperties) {
    Flyway flyway =
        Flyway.configure()
            .dataSource(
                dataSourceProperties.getUrl(),
                dataSourceProperties.getUsername(),
                dataSourceProperties.getPassword())
            .baselineOnMigrate(true)
            .locations("classpath:db/migration")
            .load();
    return flyway;
  }

我在 IDE 上启动应用程序时没有遇到任何错误。我在启动应用程序之前删除了模式并创建了模式,看到应用程序启动正常并应用了创建模式,但是 lang table 中没有任何记录.

此外,应用程序日志似乎表明它可能没有通过 flyway 应用 V1_create_schema.sql 迁移,而必须由休眠来完成。有人知道我在这里会遗漏什么吗?

看起来 Hibernate 为您创建了数据模型 (spring.jpa.hibernate.ddl-auto=update),您可以禁用它,因为您正在使用 Flyway。

无论如何,在您想要

的现有非空模式上使用 Flyway 时
spring.flyway.baseline-on-migrate = true

确保飞行路线历史记录 table 在首次运行时创建。