数据库迁移工具(一)
作者:互联网
数据库迁移工具(一)
Flyway定义数据库的迁移
Flyway版本:5.2.4
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
<version>5.2.4</version>
</dependency>
在使用这个版本之前首先测试了不配置版本和配置8版本的Flyway,但是并没有像网上和书上所写的那样会自动生成Flyway记录表和SQL定义的表,最终使用了5版本的Flyway才出现效果
Flyway使用SQL来定义迁移脚本,因此在这提前准备一些SQL文件,将sql文件保存到resources/db/migration(flyway默认读取位置)
CREATE TABLE IF NOT EXISTS`mybatis`.`flyway_table` (
`id` INT NOT NULL AUTO_INCREMENT,
`field_name` VARCHAR(45) NOT NULL,
`field_value` VARCHAR(45) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `id_UNIQUE` (`id` ASC));
INSERT INTO `mybatis`.`flyway_table` (`field_name`, `field_value`) VALUES ('flyway', 'init');
INSERT INTO `mybatis`.`flyway_table` (`field_name`, `field_value`) VALUES ('code tools', 'vscode');
INSERT INTO `mybatis`.`flyway_table` (`field_name`, `field_value`) VALUES ('connection', 'workbench');
INSERT INTO `mybatis`.`flyway_table` (`field_name`, `field_value`) VALUES ('time', 'night');
Flyway脚本名称以大写字母V开头,随后是脚本的版本号,后面跟着两个下划线和对脚本的庙是,如: V1.2__initFlyway.sql
在application.yml中添加Flyway的配置
spring:
flyway:
enabled: true
baseline-on-migrate: true
baseline-description: 学习测试
validate-on-migrate: true
check-location: true
locations: classpath:db/migration
如果不配置Flyway的话,在该版本的maven中会出现如下报错(因为之前看一些资料显示可以不配置Flyway):
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flywayInitializer' defined in class path resource [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]: Invocation of
init method failed; nested exception is org.flywaydb.core.api.FlywayException: Found non-empty schema(s) `mybatis` without schema history table! Use baseline() or set baselineOnMigrate to true to initialize the schema history table.
启动整个项目,启动成功后查看数据表,出现了 flyway_schema_history 和 flyway_table这两张数据表
标签:name,数据库,Flyway,field,flyway,mybatis,table,迁移,工具 来源: https://blog.csdn.net/zhangdy12307/article/details/121452551