flyway
参考:https://blog.waterstrong.me/flyway-in-practice/
参考:https://blog.youkuaiyun.com/Cug_wangww/article/details/81150417?utm_source=blogkpcl0
参考:https://www.cnblogs.com/dreamingodd/p/6125435.html
Flyway
定义:Flyway是一款开源的数据库版本管理工具,Flyway可以独立于应用实现管理并跟踪数据库的变更,Flyway根据自己的约定,不需要复杂的配置就可以实现数据的Migrate。Migrations可以写成SQL脚本,也可以写在Java代码中,Flyway还支持Spring Boot。
1、在pom.xml文件中导入依赖,如下:
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
<version>5.0.7</version>
</dependency>
<build>
<plugins>
<plugin>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>5.0.7</version>
</plugin>
</plugins>
</build>
2、在src/resources/application.yml文件中添加flyway配置信息
flyway:
baselineOnMigrate: true
enabled: true
baseline-version: 2018.10.25
sql-migration-prefix: V
sql-migration-suffix: .sql
url: jdbc:mysql://127.0.0.1:3386/ship_test?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull
user: root
password: 970131
validate-on-migrate: true
locations: classpath:migrate
check-location: false
jpa:
hibernate:
ddl-auto: none
注:
第一列 | 第二列 |
---|---|
baselineOnMigrate: true | # 是否执行不含元数据表的非空模式迁移 |
enabled: true | # 启用flyway |
baseline-version: 2018.10.25 | # 版本号 |
sql-migration-prefix: V | # SQL迁移的文件名前缀 |
sql-migration-suffix: .sql | # SQL迁移的文件名后缀 |
user: root | # 数据库用户名 |
password: 970131 | # 数据库密码 |
validate-on-migrate: true | # 校验 |
locations: classpath:migrate | # SQL迁移的文件所在位置 |
check-location: false | # 检查迁移脚本位置是否存在。 |
url: value | # 要迁移的数据库的J.BDBURL。如果未设置,则使用主配置数据源。 |
vlaue=
jdbc:mysql://127.0.0.1:3386/
ship_test?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull )
3、 在src/resources/migrate目录下放置数据库脚本文件:V2018.10.25__ship_dev.sql文件;
内容为:创建数据库的语句;
注:
sql脚本命名规范:
prefix version,数字间用点【.】隔开 双下划线 文件名,字符间用下划线连接 suffix
V 2018.10.25 __ ship_dev .sql
4、在本地创建新的数据库ship_test:
create database ship_test;
5、重新编译程序、启动程序;
spring会自动创建flyway的bean用来处理脚本;
注:flyway_shema_history : 用于版本控制