package com.ruoyi.config; import javax.sql.DataSource; import com.alibaba.druid.pool.DruidDataSource; import org.flywaydb.core.Flyway; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * Runs versioned migrations against the same master datasource used by MyBatis. * The application uses a custom Druid datasource, so Spring Boot's standard * Flyway auto-configuration is not available here. */ @Configuration public class FlywayMigrationConfig { @Bean(name = "flywayMigrationInitializer", initMethod = "migrate") public FlywayMigrationInitializer flywayMigrationInitializer(@Qualifier("masterDataSource") DataSource dataSource) { if (!(dataSource instanceof DruidDataSource druidDataSource)) { throw new IllegalStateException("Flyway requires the configured Druid master datasource"); } Flyway flyway = Flyway.configure() // Flyway must not use a pooled Druid connection here. Druid disables // the connection when MySQL denies Flyway's optional performance_schema probe. .dataSource(druidDataSource.getUrl(), druidDataSource.getUsername(), druidDataSource.getPassword()) .locations("classpath:db/migration") .baselineOnMigrate(false) .validateOnMigrate(true) .outOfOrder(false) .cleanDisabled(true) .load(); return new FlywayMigrationInitializer(flyway); } /** A small lifecycle bean keeps migration execution explicit and testable. */ public static final class FlywayMigrationInitializer { private final Flyway flyway; public FlywayMigrationInitializer(Flyway flyway) { this.flyway = flyway; } public void migrate() { flyway.migrate(); } } }