SpringBoot整合flyway(Gradle/Postgresql)

1. gradle.build.kts文件新增flyway的插件和依赖

plugins {
    id("org.springframework.boot") version "3.3.2"
    id("org.flywaydb.flyway") version "9.10.2"
    distribution
    kotlin("jvm") version "2.1.0"
}

dependencies {
    implementation ("org.flywaydb:flyway-core:11.0.0")
    runtimeOnly ("org.flywaydb:flyway-database-postgresql:10.10.0")
}

2. 自定义配置类

@Setter
@Getter
@Configuration
public class FlywayConfig {
    @Value("${spring.profiles.active}")
    private String isDev;

    @Value("${spring.datasource.druid.url}")
    private String url;

    @Value("${spring.datasource.druid.username}")
    private String user;

    @Value("${spring.datasource.druid.password}")
    private String pwd;

    @Value("${spring.flyway.locations}")
    private String locations;

    @Bean
    public Flyway flyway() {
            return Flyway.configure()
                    .dataSource("jdbc:postgresql://xx.xx.xx.xx:xxx/postgres?currentSchema=public", "username", "pwd")
                    .locations(locations)
                    .baselineOnMigrate(true)
                    .encoding("UTF-8")
                    .load();
    }
}

3. application配置文件

spring:
  flyway:
    enabled: false
    locations: classpath:db/migration
    table: flyway_schema_history

更多推荐