对于数据迁移的概念,相信大家已经都比较熟悉。那么,什么是数据迁移?为什么需要数据迁移?在这里就不再做相关分享啦~。接下来主要分享一下数据迁移工具 Flyway 使用。
Flyway is the Apache v2 licensed open-source tool that makes database migrations easy. It strongly favors simplicity and convention over configuration.
Flyway
是一款开源的数据库迁移工具,它认为简单和约定优于配置。没有繁琐的配置,有 6 个主要基本命令:Migrate, Clean, Info, Validate, Baseline 和 Repair。
Flyway received the highest distinction on the Thoughtworks Technology Radar by being placed in the adopt category.
如何使用 Flyway?
Flyway
支持很多种和应用集成的方法,这里主要分享一下Gradle
集成Flyway
的使用。
Setup Repo db-migration-flyway:
1 | $ gradle init --type java-libray |
Add Gradle Flyway plugin
Change
build.gradle
file content1
2
3
4
5
6
7
8
9
10
11
12buildscript {
repositories {
maven { url "https://plugins.gradle.org/m2/" }
}
dependencies {
classpath "gradle.plugin.com.boxfuse.client:flyway-release:4.1.1"
}
}
apply plugin: "idea"
apply plugin: 'java'
apply plugin: "org.flywaydb.flyway"Build repo
1
$ ./gradlew build
Config Flyway
Change [
build.gradle
] file content1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30buildscript {
repositories {
maven { url "https://plugins.gradle.org/m2/" }
}
dependencies {
classpath "gradle.plugin.com.boxfuse.client:flyway-release:4.1.1"
}
}
apply plugin: "idea"
apply plugin: 'java'
apply plugin: "org.flywaydb.flyway"
flyway {
driver = 'com.mysql.jdbc.Driver'
url = "jdbc:mysql://127.0.0.1:3306/flyway_dev"
user = 'mysql'
password = 'mysql'
table = 'flyway_dev_schema_version'
locations = ["filesystem:${projectDir}/src/main/resources/db/migration/mysql"]
sqlMigrationPrefix = 'MySQL-'
}
repositories {
jcenter()
}
dependencies {
compile 'mysql:mysql-connector-java:5.1.34'
}Build repo
1
$ ./gradlew build
Add Migration Scripts
Create file MySQL-1_1__create_users_table.sql
1
2
3
4
5CREATE TABLE USERS (
ID BIGINT PRIMARY KEY AUTO_INCREMENT,
USERNAME VARCHAR(32) NOT NULL UNIQUE,
CREATED_AT TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3)
);Create Mysql Database
flyway_dev
with usermysql
and passwordmysql
Migration
1
$ ./gradlew flywayMigrate -i
配置管理
对于DEV
、QA
、TEST
、STAGE
、PROD
针对不同的环境,需要会使用不一样的数据库配置。
在工程目录下创建文件
config/dev/db.properties
,config/test/db.properties
,config/prod/db.properties
:1
2
3
4
5
6
7
8
9
10
11
12
13
14// config/dev/db.properties
db.url=jdbc:mysql://127.0.0.1:3306/flyway_dev
db.user=mysql
db.password=mysql
// config/test/db.properties
db.url=jdbc:mysql://127.0.0.1:3306/flyway_test
db.user=mysql
db.password=mysql
// config/prod/db.properties
db.url=jdbc:mysql://127.0.0.1:3306/flyway_prod
db.user=mysql
db.password=mysql修改
build.gradle
文件1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33buildscript {
repositories {
maven { url "https://plugins.gradle.org/m2/" }
}
dependencies {
classpath "gradle.plugin.com.boxfuse.client:flyway-release:4.1.1"
}
}
apply plugin: "idea"
apply plugin: 'java'
apply plugin: "org.flywaydb.flyway"
def properties = new Properties()
properties.load(project.file("config/${env}/db.properties").newReader())
flyway {
driver = 'com.mysql.jdbc.Driver'
url = properties.get('db.url')
user = properties.get('db.user')
password = properties.get('db.password')
table = 'flyway_dev_schema_version'
locations = ["filesystem:${projectDir}/src/main/resources/db/migration/mysql"]
sqlMigrationPrefix = 'MySQL-'
}
repositories {
jcenter()
}
dependencies {
compile 'mysql:mysql-connector-java:5.1.34'
}Create Mysql Database
flyway_test
with usermysql
and passwordmysql
- Migration test env
1
$ ./gradlew -Penv=test flywayMigrate -i
写在最后
这里只是简单的分享了一下,Gradle
和 Flyway
的集成。Flyway
还有很多功能值得去探索…