java数据迁移工具_数据迁移工具 - Flyway

对于数据迁移的概念,相信大家已经都比较熟悉。那么,什么是数据迁移?为什么需要数据迁移?在这里就不再做相关分享啦~。接下来主要分享一下数据迁移工具 Flyway 使用。

0d645898c96a829ebdb8ae6bef1b7bcf.png

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的使用。

$ gradle init --type java-libray

d280bb9be6a571c10067cbdd6ac4d514.png

Add Gradle Flyway plugin

Change build.gradle file content

buildscript {

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

$ ./gradlew build

Config Flyway

Change [build.gradle] file content

buildscript {

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

$ ./gradlew build

Add Migration Scripts

CREATE 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 user mysql and password mysql

Migration

$ ./gradlew flywayMigrate -i

d687c96a35e6563a11aee0e9753b6119.png

配置管理

对于DEV、QA、TEST、STAGE、PROD针对不同的环境,需要会使用不一样的数据库配置。

在工程目录下创建文件config/dev/db.properties, config/test/db.properties, config/prod/db.properties:

// 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 文件

buildscript {

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 user mysql and password mysql

Migration test env

$ ./gradlew -Penv=test flywayMigrate -i

写在最后

这里只是简单的分享了一下,Gradle 和 Flyway 的集成。Flyway 还有很多功能值得去探索...

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值