2018年3月15日 | Leave a comment 问题: flyway 可以管理数据库升级记录 changelog 可以使用java编写,本文章采用 sql **另外有个 liquibase 的也是干这个事情的 而且支持 json / yaml 等编写 changelog 解决: pom.xml add plugin flyway-maven-plugin & config <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>demo</groupId> <artifactId>flyway1</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <build> <plugins> <plugin> <groupId>org.flywaydb</groupId> <artifactId>flyway-maven-plugin</artifactId> <version>5.0.7</version> <configuration> <url>jdbc:mysql://localhost:3306/flyway2</url> <user>root</user> <password>123456</password> </configuration> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>6.0.6</version> </dependency> </dependencies> </plugin> </plugins> </build> </project> 12345678910111213141516171819202122232425262728293031323334 <?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>demo</groupId> <artifactId>flyway1</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <build> <plugins> <plugin> <groupId>org.flywaydb</groupId> <artifactId>flyway-maven-plugin</artifactId> <version>5.0.7</version> <configuration> <url>jdbc:mysql://localhost:3306/flyway2</url> <user>root</user> <password>123456</password> </configuration> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>6.0.6</version> </dependency> </dependencies> </plugin> </plugins> </build></project> src/resources/db/migration/V1__init.sql CREATE TABLE `flyway2`.`tt` ( `id` INT NOT NULL AUTO_INCREMENT, `ttcol` VARCHAR(45) NOT NULL, PRIMARY KEY (`id`), UNIQUE INDEX `id_UNIQUE` (`id` ASC)); INSERT INTO `flyway2`.`tt` (`ttcol`) VALUES ('2222'); INSERT INTO `flyway2`.`tt` (`ttcol`) VALUES ('3333'); 12345678 CREATE TABLE `flyway2`.`tt` ( `id` INT NOT NULL AUTO_INCREMENT, `ttcol` VARCHAR(45) NOT NULL, PRIMARY KEY (`id`), UNIQUE INDEX `id_UNIQUE` (`id` ASC)); INSERT INTO `flyway2`.`tt` (`ttcol`) VALUES ('2222');INSERT INTO `flyway2`.`tt` (`ttcol`) VALUES ('3333'); 然后执行 mvn flyway:migrate 1 mvn flyway:migrate 相当于执行了 V1__init.sql 在增加数据库升级脚本 src/resources/db/migration/V1.0.1__addt2.sql create table t2 ( id int not null primary key , ddddd varchar(50) ); alter table t1 add column dddd varchar(50); 1234567 create table t2 (id int not null primary key ,ddddd varchar(50)); alter table t1add column dddd varchar(50); 再执行 mvn flyway:migrate 1 mvn flyway:migrate 升级数据库到 V1.0.1 *** 已经 migrate 的脚本不能再修改 参考: https://flywaydb.org/getstarted/