yeg
2026-08-29 0162f3730ca2e9644322a90450728b63243d40d0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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();
        }
    }
}