YOU'VE MADE A BRAVE DECISION, WELCOME.

每一个不曾起舞的日子都是对生命的辜负。

学习笔记:Flyway

Flyway is an open-source database migration tool. It strongly favors simplicity and convention over configuration. It is based around just 6 basic commands: Migrate, Clean, Info, Validate, Baseline and Repair. Migrations can be written in SQL (database-specific syntax (such as PL/SQL, T-SQL, …) is supported) or Java (for advanced data transformations or dealing with LOBs). It has a Command-line client. If you are on the JVM, we recommend using the Java API (also works on Android) for migrating the database on application startup. Alternatively, you can also use the Maven plugin, Gradle plugin, SBT plugin or the Ant tasks. And if that not enough, there are plugins available for Spring Boot, Dropwizard, Grails, Play, Griffon, Grunt, Ninja and more!


Maven配置

需要在pom.xml文件中加入flywaydb的插件:

1
2
3
4
5
6
7
8
<plugin>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>3.1</version>
<configuration>
....
</configuration>
</plugin>
  • 配置方法一:在pom.xml里配置

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    <plugin>
    ...
    <configuration>
    <user>myUser</user>
    <password>mySecretPwd</password>
    <schemas>
    <schema>schema1</schema>
    <schema>schema2</schema>
    <schema>schema3</schema>
    </schemas>
    <placeholders>
    <keyABC>valueXYZ</keyABC>
    <otherplaceholder>value123</otherplaceholder>
    </placeholders>
    </configuration>
    </plugin>
  • 配置方法二:外部的配置文件来配置

    1
    2
    3
    4
    5
    flyway.user=
    flyway.password=
    flyway.schemas=schema1,schema2,schema3
    flyway.placeholders.keyABC=
    flyway.placeholders.otherplaceholder=
  • 配置方法三:执行mvn时通过指定参数flyway.configFile的值来指定配置文件

    1
    mvn -Dflyway.configFile=myConfig.properties
  • 配置方法四:使用外部配置文件的方式进行配置,默认的flyway.properties文件和pom.xml在相同的路径下

    1
    2
    3
    4
    5
    6
    7
    8
    9
    flyway.user=
    flyway.password=
    flyway.driver=org.postgresql.Driver
    flyway.url=jdbc:postgresql://localhost:5432/wjia
    flyway.locations=classpath:db/migration
    flyway.sqlMigrationPrefix=V
    flyway.sqlMigrationSeparator=__
    flyway.sqlMigrationSuffix>=.sql
    flyway.encoding=UTF-8

六个命令

  • Migrate

    flyway的最重要的功能当然是完成数据库迁移了,使用mvn flyway:migrate命令就可以方便的帮助我们执行flyway.locations目录中定义的migration任务。由于我们的使用了外部的flyway配置文件,因此在执行migration任务时需要加上参数flyway.properties来指定配置文件的位置。所以,在我们的工程中需要执行的任务为:

    mvn flyway:migrate -Dflyway.configFile=localPath/flyway.properties

    其中localPath/flyway.properties文件在本地的路径。

  • Clean

    如果我们想初始化数据库,删除所有的表和数据,那么只需要执行命令:

    mvn flyway:clean

  • Info

    如果我们想知道所有migration的详细信息,可以通过执行下面命令来打印信息:

    mvn flyway:info

    执行之后,Terminal中会打印出所有migration的详细信息,其中包括版本号,描述,执行时间,以及状态.

  • Validate

    mvn flyway:validate 用来执行已经执行过的migration任务.

  • Baseline

    mvn flyway:baseline 用来回滚数据库到一个配置文件中设定好的baseline.

  • Repair

    mvn flyway:repair 用来删除执行失败的migration任务.


查看官方文档

更多关于flyway的详细用法,请参见其官方文档