4

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

4

3 回答 3

1

大多数情况下,这将是不需要的,因为 Flyway 数据库迁移本身已经用 SQL 编写。

于 2012-01-03T01:00:01.023 回答
1

是的,这是可能的,就我而言,该功能对于不想在 prod 中允许 flyway 的 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(谷歌程序集插件 + 带有依赖项的 jar)将它编译并打包到一个“带有依赖项”(又名 fatjar)的 jar 中,并通过如下命令运行它,或者您可以将其作为依赖项包含并调用它是通过mvn exec:exec目标实现的,这也是我成功的地方。

$ java -jar /path/to/flywaydr-fatjar.jar dryRunMigrate -regular.flyway.configs -etc -etc  
于 2016-06-28T21:07:28.297 回答
0

我没有找到办法。改用mybatis迁移。看起来相当不错。

于 2011-08-19T16:06:07.667 回答