暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

太火了!Flayway这款数据迁移工具了解一下。

Java面试那些事儿 2020-06-26
1645

对于数据迁移的概念,相信大家已经都比较熟悉。那么,什么是数据迁移?为什么需要数据迁移?在这里就不再做相关分享啦~。接下来主要分享一下数据迁移工具 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 个主要基本命令:MigrateCleanInfoValidateBaseline 和 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:


       $ gradle init --type java-libray
    复制



    Add Gradle Flyway plugin


    1.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"
      复制


      2.Build repo

        $ ./gradlew build
        复制


        Config Flyway


        1.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'
          }
          复制


          2.Build repo

            $ ./gradlew build
            复制


            Add Migration Scripts


            1.Create file MySQL-1_1__create_users_table.sql

              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)
              );
              复制


              2.Create Mysql Database flyway_dev with user mysql and password mysql


              3 Migration

                $ ./gradlew flywayMigrate -i
                复制


                # 配置管理


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


                1.在工程目录下创建文件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
                  复制


                  2.修改 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'
                    }
                    复制


                    3.Create Mysql Database flyway_test with user mysql and password mysql


                    4.Migration test env

                      $ ./gradlew -Penv=test flywayMigrate -i
                      复制


                      # 写在最后


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

                      来源:http://aikin.me/2017/02/26/db-migration-flyway/


                      复制

                       往期推荐 

                      🔗



                       

                      点击阅读原文,获得更多精彩内容!

                      复制

                      复制
                      文章转载自Java面试那些事儿,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

                      评论