flyway gradle plugin使用指南

1.引入flyway plugin

buildscript {
    dependencies {
        classpath("org.flywaydb:flyway-gradle-plugin:6.0.4")
    }
}
apply plugin: "org.flywaydb.flyway"

dependencies {
    // java -cp .\h2-1.4.199.jar org.h2.tools.Server -tcp -tcpAllowOthers -web -webAllowOthers -browser -ifNotExists
  implementation "com.h2database:h2:1.4.199"
}

2. 配置

flyway {
    url = 'jdbc:h2:tcp://localhost/~/Documents/db/flyway'
    user = 'sa'
    password = ''
    schemas = ['TEST'] //schema可以指定多个
    placeholders = [
            'tableName1': 'DEV_PUBLISHER_CLIENT_KEY'
    ]
    // encoding = 'utf-8'
    // baselineOnMigrate = false 当迁移时发现目标schema非空,而且带有没有元数据的表时,是否自动执行基准迁移,默认false.
    // baselineVersion = 5 // Only migrations above baselineVersion will then be applied.

    // default filesystem:src/main/resources/db/migration
    // locations = ['classpath:migrations', 'classpath:db/pkg', 'filesystem:/sql-migrations']
    // Include your custom configuration here in addition to any default ones you want included
    // configurations = [ 'compileClasspath', 'flywayMigration' ]
}

// multiple databases
task migrateDatabase1(type: org.flywaydb.gradle.task.FlywayMigrateTask) {
    url = 'jdbc:h2:mem:mydb1'
    user = 'myUsr1'
    password = 'mySecretPwd1'
}

task migrateDatabase2(type: org.flywaydb.gradle.task.FlywayMigrateTask) {
    url = 'jdbc:h2:mem:mydb2'
    user = 'myUsr2'
    password = 'mySecretPwd2'
}

使用的是默认location,所以在db/migration下面创建sql文件:

  • V1_initail.sql
CREATE TABLE ${tableName1}
(
    PUBLISHER_CLIENT_KEY_NAME CHARACTER VARYING(255) NOT NULL,
    REGISTRATION_GROUP_NAME CHARACTER VARYING(255) NOT NULL,
    REGISTRATION_STATUS CHARACTER VARYING(255) NOT NULL,
    REGISTRATION_ENVIRONMENT CHARACTER VARYING(3) NOT NULL,
    SERVICENOW_NUMBER CHARACTER VARYING(255),
    LAST_EDITED_USER CHARACTER VARYING(7) NOT NULL,
    LAST_APPROVED_ADMIN CHARACTER VARYING(7),
    HOST_NAME CHARACTER VARYING(4000),
    FUNCTIONAL_ID CHARACTER VARYING(4000),
    RELEASE_DATE CHARACTER VARYING(255),
    EXPECTED_VOLUME INTEGER,
    CONNECTIVITY_COUNT INTEGER,
    CLIENT_KEY_STATUS CHARACTER VARYING(255) NOT NULL,
    CREATED_TS TIMESTAMP NOT NULL,
    MODIFIED_TS TIMESTAMP NOT NULL
);
ALTER TABLE DEV_PUBLISHER_CLIENT_KEY
ADD CONSTRAINT DEV_PUBLISHER_CLIENT_KEY_PK
PRIMARY KEY (PUBLISHER_CLIENT_KEY_NAME);


CREATE TABLE ADDITIONAL_OWNERS
(
    REGISTRATION_GROUP_NAME CHARACTER VARYING(255) NOT NULL,
    CLIENT_TYPE CHARACTER VARYING(255) NOT NULL,
    ADDITIONAL_OWNER_SOEID CHARACTER VARYING(7) NOT NULL,
    ADDITIONAL_OWNER_NAME CHARACTER VARYING(255) NOT NULL
);
ALTER TABLE ADDITIONAL_OWNERS
ADD CONSTRAINT ADDITIONAL_OWNERS_PK
PRIMARY KEY (REGISTRATION_GROUP_NAME, CLIENT_TYPE, ADDITIONAL_OWNER_SOEID);

--show columns from ADDITIONAL_OWNERS;
--show columns from DEV_PUBLISHER_CLIENT_KEY;
/*
Multi-line
comment
*/
  • V1.1_First_Changes.sql
insert into ADDITIONAL_OWNERS
(REGISTRATION_GROUP_NAME, CLIENT_TYPE, ADDITIONAL_OWNER_SOEID, ADDITIONAL_OWNER_NAME)
values ('rio', 'pub', 'hw', 'test_name');
-- comments

3. 操作介绍

flyway plugin task:
baseline: create flyway_schema_history table
repair: delete flyway_schema_history failed records
validate: check sql
migrate: execute sql  // -i to show infomation
clean: delete all tables of current schemas


h2 database schema:
CREATE SCHEMA IF NOT EXISTS QQ
SET SCHEMA QQ
DROP SCHEMA QQ

DROP TABLE HAHA

4. 也可以使用 Java API

gradle 添加依赖

dependencies {
  implementation 'org.flywaydb:flyway-core'
}
  public void flyway_Ops() {
    String url = "jdbc:h2:tcp://localhost/~/Documents/db/flyway";
    String user = "root";
    String password = "tiger";
    Flyway flyway = Flyway.configure().dataSource(url, user, password).load();

    // 创建 flyway_schema_history 表
    flyway.baseline();
    // 删除 flyway_schema_history 表中失败的记录
    flyway.repair();
    // 检查 sql 文件
    flyway.validate();
    // 执行数据迁移
    flyway.migrate();
    // 删除当前 schema 下所有表
    flyway.clean();
  }

你可能感兴趣的:(flyway gradle plugin使用指南)