添加flyway依赖

在根目录下pom.xml添加flyway依赖RuoYi-Vue\pom.xml

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

在ruoyi-framework\pom.xml下引入

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

yaml配置

在RuoYi-Vue\ruoyi-admin\src\main\resources\application.yml中添加以下配置

# 配置flyway数据版本管理
  flyway:
    enabled: true
    baseline-on-migrate: true
    clean-on-validation-error: false
    sql-migration-prefix: V
    sql-migration-suffixes: .sql
    locations: classpath:db/migration
flyway.baseline-description对执行迁移时基准版本的描述.
flyway.baseline-on-migrate当迁移时发现目标schema非空,而且带有没有元数据的表时,是否自动执行基准迁移,默认false.
flyway.baseline-version开始执行基准迁移时对现有的schema的版本打标签,默认值为1.
flyway.check-location检查迁移脚本的位置是否存在,默认false.
flyway.clean-on-validation-error当发现校验错误时是否自动调用clean,默认false.
flyway.enabled是否开启flywary,默认true.
flyway.encoding设置迁移时的编码,默认UTF-8.
flyway.ignore-failed-future-migration当读取元数据表时是否忽略错误的迁移,默认false.
flyway.init-sqls当初始化好连接时要执行的SQL.
flyway.locations迁移脚本的位置,默认db/migration.
flyway.out-of-order是否允许无序的迁移,默认false.
flyway.password目标数据库的密码.
flyway.placeholder-prefix设置每个placeholder的前缀,默认${.
flyway.placeholder-replacementplaceholders是否要被替换,默认true.
flyway.placeholder-suffix设置每个placeholder的后缀,默认}.
flyway.placeholders.[placeholder name]设置placeholder的value
flyway.schemas设定需要flywary迁移的schema,大小写敏感,默认为连接默认的schema.
flyway.sql-migration-prefix迁移文件的前缀,默认为V.
flyway.sql-migration-separator迁移脚本的文件名分隔符,默认__
flyway.sql-migration-suffix迁移脚本的后缀,默认为.sql
flyway.tableflyway使用的元数据表名,默认为schema_version
flyway.target迁移时使用的目标版本,默认为latest version
flyway.url迁移时使用的JDBC URL,如果没有指定的话,将使用配置的主数据源
flyway.user迁移数据库的用户名
flyway.validate-on-migrate迁移时是否校验,默认为true.

创建目录

在RuoYi-Vue\ruoyi-system\src\main\resources目录下创建db/migration目录,把若依若依提供的表结构sql文件复制到该文件夹,并且重命名。

文件命名规则

仅需要被执行一次的SQL命名以大写的”V”开头,V+版本号(版本号的数字间以”.“或”_“分隔开)+双下划线(用来分隔版本号和描述)+文件描述+后缀名。例如:V20201100__create_user.sql、V2.1.5__create_user_ddl.sql、V4.1_2__add_user_dml.sql。
可重复运行的SQL,则以大写的“R”开头,后面再以两个下划线分割,其后跟文件名称,最后以.sql结尾。(不推荐使用)比如:R__truncate_user_dml.sql。

若依项目代码修改

全局搜索一下@PostConstruct,将代码中@PostConstruct注释掉,因为@PostConstruct比flyway初始化更快执行,所以有可能造成代码错误或者执行了旧版本的数据库结构
总共有三个文件需要修改SysJobServiceImpl,SysDictTypeServiceImpl,SysConfigServiceImpl

/**
 * 项目启动时,初始化定时器 主要是防止手动修改数据库导致未同步到定时任务处理(注:不能手动修改数据库ID和任务组名,否则会导致脏数据)
 */
//@PostConstruct
public void init() throws SchedulerException, TaskException
{
    scheduler.clear();
    List<SysJob> jobList = jobMapper.selectJobAll();
    for (SysJob job : jobList)
    {
        ScheduleUtils.createScheduleJob(scheduler, job);
    }
}

/**
 * 项目启动时,初始化字典到缓存
 */
//@PostConstruct
public void init()
{
    loadingDictCache();
}

/**
 * 项目启动时,初始化参数到缓存
 */
//@PostConstruct
public void init()
{
    loadingConfigCache();
}

注释掉以后,我们把启动时初始化的操作移动到RuoYiApplication.java中

package com.ruoyi;

import com.ruoyi.quartz.service.impl.SysJobServiceImpl;
import com.ruoyi.system.service.impl.SysConfigServiceImpl;
import com.ruoyi.system.service.impl.SysDictTypeServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;

/**
 * 启动程序
 *
 * @author ruoyi
 */
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
public class RuoYiApplication
        implements CommandLineRunner {
    @Autowired
    private SysConfigServiceImpl configService;
    @Autowired
    private SysJobServiceImpl jobService;
    @Autowired
    private SysDictTypeServiceImpl dictTypeService;

    public static void main(String[] args)
    {
        // System.setProperty("spring.devtools.restart.enabled", "false");
        SpringApplication.run(RuoYiApplication.class, args);
        System.out.println("(♥◠‿◠)ノ゙  若依启动成功   ლ(´ڡ`ლ)゙  \n" +
                " .-------.       ____     __        \n" +
                " |  _ _   \\      \\   \\   /  /    \n" +
                " | ( ' )  |       \\  _. /  '       \n" +
                " |(_ o _) /        _( )_ .'         \n" +
                " | (_,_).' __  ___(_ o _)'          \n" +
                " |  |\\ \\  |  ||   |(_,_)'         \n" +
                " |  | \\ `'   /|   `-'  /           \n" +
                " |  |  \\    /  \\      /           \n" +
                " ''-'   `'-'    `-..-'              ");
    }

    @Override
    public void run(String... args) throws Exception {
        configService.init();
        jobService.init();
        dictTypeService.init();
    }
}

启动项目

代码编写好以后,我们在数据库中创建一个ry-vue的数据库,在RuoYi-Vue\ruoyi-admin\src\main\resources\application.yml下修改数据库连接和数据库密码等信息。

启动成功以后,我们会看到ry-vue的数据库中表结构已经创建完成。

转载自:https://songhaozhi.com/2022/09/08/ruoyi-vue项目集成flyway实现自动创建表/