0%

flyway

1. 配置

1.1. maven 依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
<!-- flyway 操作数据库需要用到的 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>

1.2. Spring 配置

1
2
3
4
spring:
flyway:
enabled: true
baseline-on-migrate: true

其他还有

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

1.3. 脚本

脚本默认目录: db/migration
脚本名称: V{version}__{name}.sql
固定的以 V 开头, 中间以双下划线分割.
flyway 只会执行比执行历史记录表(默认 flyway_schema_history) 中版本更大的脚本.
如: V1.0__init.sql

2. base-line

当项目存在历史数据, 需要告诉 flyway 在某个版本之前的脚本不需要执行了

1
2
3
baseline-description: #对执行迁移时基准版本的描述.
baseline-on-migrate: #当迁移时发现目标schema非空,而且带有没有元数据的表时,是否自动执行基准迁移,默认false.
baseline-version: #开始执行基准迁移时对现有的schema的版本打标签,默认值为1.