数据迁移工具 - Flyway

aikin

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

flyway home

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, BaselineRepair

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

$ gradle init --type java-libray

init

Add Gradle Flyway plugin

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

database

配置管理

对于DEVQATESTSTAGEPROD针对不同的环境,需要会使用不一样的数据库配置。

  • 在工程目录下创建文件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

写在最后

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

原文链接

阅读 4k

喜欢 coding 喜欢大海。。。

594 声望
17 粉丝
0 条评论
你知道吗?

喜欢 coding 喜欢大海。。。

594 声望
17 粉丝
文章目录
宣传栏