项目集成flyway以后,每次启动项目的时候会检查指定的migration目录,
查看数据库是否有更新,发现有新的数据库版本号就会执行数据库更新
1.引入maven依赖
依赖
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
<version>5.0.3</version>
</dependency>
plugin
<plugin>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>5.0.3</version>
</plugin>
2.配置文件添加如下配置
spring.flyway.enabled=true
spring.flyway.baseline-on-migrate=true
spring.flyway.locations=classpath:db/migration #就是步骤3里显示的db.migration,是个两级目录
3.创建sql文件
在resources
目录下创建目录db/migration
,虽然显示的db.migration,实际是两级目录
创建一个文件V1.1__init_database.sql
我们自己的sql版本号从1.1开始,版本1被用来初始化flyway服务
注意V1.1之后是两个下划线
sql文件里我们让他创建了一个空表
CREATE TABLE `some_table` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '名字',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
4.启动项目
查看数据库是否添加了一个flyway_schema_history
表,
并且根据我们的sql文件创建了一个some_table
表
5.持续集成sql版本
V1.2__insert_some_table.sql
insert into some_table (name) value ('张三');
insert into some_table (name) value ('李四');
V2.0__create_another_table.sql
CREATE TABLE `another_table` (
`id` int(10) NOT NULL,
`name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '名字',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
V2.1__insert_another_table.sql
insert into another_table (id,name) value (1212,'王五');
insert into another_table (id,name) value (8989,'赵六');