Springboot集成Flyway
目录
springboot集成Flyway
引入依赖
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
<version>10.15.2</version>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-mysql</artifactId>
<version>10.15.2</version>
</dependency>
在application.yml
文件中添加配置
spring:
flyway:
enabled: true
# 编码格式,默认UTF-8
encoding: UTF-8
# 迁移sql脚本文件存放路径,默认db/migration
locations: classpath:/db/migration
# 迁移sql脚本文件名称的前缀,默认V
sql-migration-prefix: V
# 迁移sql脚本文件名称的分隔符,默认2个下划线__
sql-migration-separator: __
# 迁移sql脚本文件名称的后缀
sql-migration-suffixes: .sql
# 迁移时是否进行校验,默认true
validate-on-migrate: true
# 当迁移发现数据库非空且存在没有元数据的表时,自动执行基准迁移,新建schema_version表
baseline-on-migrate: true
在上面的配置中,指定了脚本的目录在:classpath:/db/migration
所以在resources
目录下创建对应的目录
在目录下创建以Vxxx__xxx.sql
格式的文件:eg: V1.0.0__init.sql
create table sys_dict
(
id bigint auto_increment comment '字典ID'
primary key,
dict_code varchar(128) null comment '字典编码',
dict_name varchar(128) null comment '字典名称',
dict_type int default 1 null comment '字典值类型:1-字符串;0-数值',
dict_desc varchar(128) null comment '字典描述',
status int null comment '状态',
delete_yn int null comment '删除标志',
create_id varchar(32) null comment '创建人ID',
create_time datetime null comment '创建时间',
update_id varchar(32) null comment '更新人ID',
update_time datetime null comment '更新时间',
remark varchar(900) null comment '备注',
constraint uk_sd_dict_code
unique (dict_code)
)
comment '字典表' row_format = DYNAMIC;
前提是,数据库需要先创建好
启动springboot运用
getConnection 123 : HikariPool-1 - Start completed.
2024-07-22 20:52:40.331 INFO 29813 --- [ main] org.flywaydb.core.FlywayExecutor info 41 : Database: jdbc:mysql://192.168.0.101:3306/accumulate (MySQL 8.4)
2024-07-22 20:52:40.399 WARN 29813 --- [ main] o.f.c.internal.database.base.Database warn 45 : Flyway upgrade recommended: MySQL 8.4 is newer than this version of Flyway and support has not been tested. The latest supported version of MySQL is 8.1.
2024-07-22 20:52:40.446 INFO 29813 --- [ main] o.f.c.i.s.JdbcTableSchemaHistory info 41 : Schema history table `accumulate`.`flyway_schema_history` does not exist yet
2024-07-22 20:52:40.461 INFO 29813 --- [ main] o.f.core.internal.command.DbValidate info 41 : Successfully validated 1 migration (execution time 00:00.048s)
2024-07-22 20:52:40.551 INFO 29813 --- [ main] o.f.c.i.s.JdbcTableSchemaHistory info 41 : Creating Schema History table `accumulate`.`flyway_schema_history` ...
2024-07-22 20:52:40.784 INFO 29813 --- [ main] o.f.core.internal.command.DbMigrate info 41 : Current version of schema `accumulate`: << Empty Schema >>
2024-07-22 20:52:40.850 INFO 29813 --- [ main] o.f.core.internal.command.DbMigrate info 41 : Migrating schema `accumulate` to version "1.0.0 - init"
2024-07-22 20:52:42.197 WARN 29813 --- [ main] o.f.c.i.s.DefaultSqlScriptExecutor warn 45 : DB: Unknown table 'accumulate.sys_file' (SQL State: 42S02 - Error Code: 1051)
2024-07-22 20:52:42.261 WARN 29813 --- [ main] o.f.c.i.s.DefaultSqlScriptExecutor warn 45 : DB: Integer display width is deprecated and will be removed in a future release. (SQL State: HY000 - Error Code: 1681)
2024-07-22 20:52:42.261 WARN 29813 --- [ main] o.f.c.i.s.DefaultSqlScriptExecutor warn 45 : DB: Integer display width is deprecated and will be removed in a future release. (SQL State: HY000 - Error Code: 1681)
2024-07-22 20:52:42.261 WARN 29813 --- [ main] o.f.c.i.s.DefaultSqlScriptExecutor warn 45 : DB: Integer display width is deprecated and will be removed in a future release. (SQL State: HY000 - Error Code: 1681)
2024-07-22 20:52:42.261 WARN 29813 --- [ main] o.f.c.i.s.DefaultSqlScriptExecutor warn 45 : DB: 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous. (SQL State: HY000 - Error Code: 3719)
2024-07-22 20:52:42.340 WARN 29813 --- [ main] o.f.c.i.s.DefaultSqlScriptExecutor warn 45 : DB: Unknown table 'accumulate.sys_algorithm' (SQL State: 42S02 - Error Code: 1051)
2024-07-22 20:52:42.531 WARN 29813 --- [ main] o.f.c.i.s.DefaultSqlScriptExecutor warn 45 : DB: Unknown table 'accumulate.sys_alarm' (SQL State: 42S02 - Error Code: 1051)
可以看见日志