Flyway输出到SQL文件

时间:2011-08-18 14:00:40

标签: flyway

是否可以将db迁移输出到SQL文件而不是直接调用flyway中的数据库更改?

3 个答案:

答案 0 :(得分:1)

大多数情况下,这不需要像Flyway一样,数据库迁移本身已经用SQL编写。

答案 1 :(得分:1)

是的,这是可能的,就我而言,这个功能对于那些不希望在飞行中允许飞行的DBA来说是绝对必要的。

我从这里修改代码,这是flyway的干运行命令,你可以添加一个文件编写器并写出migrationDetails:

https://github.com/killbill/killbill/commit/996a3d5fd096525689dced825eac7a95a8a7817e

我是这样做的......项目结构(刚从killbill的项目中复制出来并将包重命名为flywaydr:

.
./main
./main/java
./main/java/com
./main/java/com/flywaydr
./main/java/com/flywaydr/CapturingMetaDataTable.java
./main/java/com/flywaydr/CapturingSqlMigrationExecutor.java
./main/java/com/flywaydr/DbMigrateWithDryRun.java
./main/java/com/flywaydr/MigrationInfoCallback.java
./main/java/com/flywaydr/Migrator.java
./main/java/org
./main/java/org/flywaydb
./main/java/org/flywaydb/core
./main/java/org/flywaydb/core/FlywayWithDryRun.java

在Migrator.java中添加(实现回调并将其放在DbMigrateWithDryRun.java中):

  } else if ("dryRunMigrate".equals(operation)) {
      MigrationInfoCallback mcb = new MigrationInfoCallback();
      flyway.dryRunMigrate();
      MigrationInfoImpl[] migrationDetails = mcb.getPendingMigrationDetails();

      if(migrationDetails.length>0){              
          writeMasterScriptToFile(migrationDetails);
      }
 }

然后写东西给文件:

private static void writeMasterScriptToFile(MigrationInfoImpl[] migrationDetails){

    FileWriter fw = null;
    try{
        String masterScriptLoc="path/to/file";

        fw = new FileWriter(masterScriptLoc);
        LOG.info("Writing output to " + masterScriptLoc);
        for (final MigrationInfoImpl migration : migrationDetails){
             Path file =Paths.get(migration.getResolvedMigration().getPhysicalLocation());
             //if you want to copy actual script files parsed by flyway
             Files.copy(file, Paths.get(new StringBuilder(scriptspathloc).append(File.separator).append(file.getFileName().toString()).toString()), REPLACE_EXISTING);


        }
             //or just get the sql
             for (final SqlStatement sqlStatement : sqlStatements) {  
                  //sqlStatement.getSql();  
             }
        fw.write(stuff.toString());
    } catch(Exception e){
            LOG.error("Could not write to file, io exception was thrown.",e);
    } finally{
            try{fw.close();}catch(Exception e){LOG.error("Could not close file writer.",e);}
    }

}

最后要提一下的是,我通过maven(google assembly plugin + jar with dependencies)将其编译并打包到一个“with dependencies”(又名fatjar)中,并通过如下命令运行它,或者你可以将它包含为依赖并通过mvn exec:exec目标调用它,这也是我成功的目标。

$ java -jar /path/to/flywaydr-fatjar.jar dryRunMigrate -regular.flyway.configs -etc -etc  

答案 2 :(得分:0)

我没找到方法。切换到mybatis迁移。看起来很不错。