FlyWay 迁移脚本

Posted

技术标签:

【中文标题】FlyWay 迁移脚本【英文标题】:FlyWay migrate scripts 【发布时间】:2018-02-28 11:55:44 【问题描述】:

你能帮忙解释为什么flyway不能安装脚本吗 ?

我的架构是空的,只有一个创建 flyway 的表。但它也是空的。

如何正确配置 FlyWay?我的脚本有以下形式

V1__20170918_Initial_structure.sql

18:43:30,773 INFO  [stdout] (ServerService Thread Pool -- 14) 2017-09-19 18:43:30 DEBUG Flyway:40 - DDL Transactions Supported: false
18:43:30,773 INFO  [stdout] (ServerService Thread Pool -- 14) 2017-09-19 18:43:30 DEBUG Flyway:40 - Schema: TEST_SERVER
18:43:30,778 INFO  [stdout] (ServerService Thread Pool -- 14) 2017-09-19 18:43:30 DEBUG FeatureDetector:40 - Spring Jdbc available: false
18:43:30,779 INFO  [stdout] (ServerService Thread Pool -- 14) 2017-09-19 18:43:30 DEBUG SqlScriptFlywayCallback:40 - Scanning for SQL callbacks ...
18:43:30,779 INFO  [stdout] (ServerService Thread Pool -- 14) 2017-09-19 18:43:30 DEBUG ClassPathScanner:40 - Scanning for classpath resources at 'classpath:db/migration' (Prefix: '', Suffix: '.sql')
18:43:30,780 INFO  [stdout] (ServerService Thread Pool -- 14) 2017-09-19 18:43:30 DEBUG ClassPathScanner:40 - Determining location urls for classpath:db/migration using ClassLoader ModuleClassLoader for Module "deployment.mobile.bank.services-1.0.5.ear.flyway-1.0-SNAPSHOT.jar:main" from Service Module Loader ...
18:43:30,780 INFO  [stdout] (ServerService Thread Pool -- 14) 2017-09-19 18:43:30 DEBUG ClassPathScanner:40 - Scanning URL: vfs:/E:/Soft/Server_10.1.0/bin/content/mobile.bank.services-1.0.5.ear/flyway-1.0-SNAPSHOT.jar/db/migration/
18:43:30,780 INFO  [stdout] (ServerService Thread Pool -- 14) 2017-09-19 18:43:30 DEBUG FeatureDetector:40 - JBoss VFS v2 available: false
18:43:30,781 INFO  [stdout] (ServerService Thread Pool -- 14) 2017-09-19 18:43:30 DEBUG FeatureDetector:40 - JBoss VFS v3 available: true
18:43:30,782 INFO  [stdout] (ServerService Thread Pool -- 14) 2017-09-19 18:43:30 DEBUG JBossVFSv3ClassPathLocationScanner:40 - Scanning starting at classpath root on JBoss VFS: E:\Soft\Server_10.1.0\bin\content\mobile.bank.services-1.0.5.ear\flyway-1.0-SNAPSHOT.jar\/
18:43:30,834 INFO  [stdout] (ServerService Thread Pool -- 14) 2017-09-19 18:43:30 DEBUG DbValidate:40 - Validating migrations ...
18:43:30,836 INFO  [stdout] (ServerService Thread Pool -- 14) 2017-09-19 18:43:30 DEBUG ClassPathScanner:40 - Scanning for classpath resources at 'classpath:db/migration' (Prefix: 'V', Suffix: '.sql')
18:43:30,836 INFO  [stdout] (ServerService Thread Pool -- 14) 2017-09-19 18:43:30 DEBUG ClassPathScanner:40 - Scanning URL: vfs:/E:/Soft/Server_10.1.0/bin/content/mobile.bank.services-1.0.5.ear/flyway-1.0-SNAPSHOT.jar/db/migration/
18:43:30,836 INFO  [stdout] (ServerService Thread Pool -- 14) 2017-09-19 18:43:30 DEBUG FeatureDetector:40 - JBoss VFS v2 available: false
18:43:30,836 INFO  [stdout] (ServerService Thread Pool -- 14) 2017-09-19 18:43:30 DEBUG ClassPathScanner:40 - Scanning for classpath resources at 'classpath:db/migration' (Prefix: 'R', Suffix: '.sql')
18:43:30,836 INFO  [stdout] (ServerService Thread Pool -- 14) 2017-09-19 18:43:30 DEBUG ClassPathScanner:40 - Scanning URL: vfs:/E:/Soft/Server_10.1.0/bin/content/mobile.bank.services-1.0.5.ear/flyway-1.0-SNAPSHOT.jar/db/migration/
18:43:30,837 INFO  [stdout] (ServerService Thread Pool -- 14) 2017-09-19 18:43:30 DEBUG FeatureDetector:40 - JBoss VFS v2 available: false
18:43:30,837 INFO  [stdout] (ServerService Thread Pool -- 14) 2017-09-19 18:43:30 DEBUG ClassPathScanner:40 - Scanning for classes at 'classpath:db/migration' (Implementing: 'org.flywaydb.core.api.migration.jdbc.JdbcMigration')
18:43:30,837 INFO  [stdout] (ServerService Thread Pool -- 14) 2017-09-19 18:43:30 DEBUG ClassPathScanner:40 - Scanning URL: vfs:/E:/Soft/Server_10.1.0/bin/content/mobile.bank.services-1.0.5.ear/flyway-1.0-SNAPSHOT.jar/db/migration/
18:43:30,838 INFO  [stdout] (ServerService Thread Pool -- 14) 2017-09-19 18:43:30 DEBUG FeatureDetector:40 - JBoss VFS v2 available: false
18:43:30,853 INFO  [stdout] (ServerService Thread Pool -- 14) 2017-09-19 18:43:30 INFO  DbValidate:44 - Successfully validated 0 migrations (execution time 00:00.018s)
18:43:30,859 INFO  [stdout] (ServerService Thread Pool -- 14) 2017-09-19 18:43:30 DEBUG DbSchemas:40 - Schema "TEST_SERVER" already exists. Skipping schema creation.
18:43:30,935 INFO  [stdout] (ServerService Thread Pool -- 14) 2017-09-19 18:43:30 DEBUG Table:40 - Locking table "TEST_SERVER"."SERVER_VERSION_DB"...
18:43:30,936 INFO  [stdout] (ServerService Thread Pool -- 14) 2017-09-19 18:43:30 DEBUG Table:40 - Lock acquired for table "TEST_SERVER"."SERVER_VERSION_DB"
18:43:30,943 INFO  [stdout] (ServerService Thread Pool -- 20) 2017-09-19 18:43:30 DEBUG Reflections:184 - going to scan these urls:
18:43:30,943 INFO  [stdout] (ServerService Thread Pool -- 20) vfs:/E:/Soft/Server_10.1.0/bin/content/mobile.bank.services-1.0.5.ear/service-web-1.0-SNAPSHOT.war/WEB-INF/classes/
18:43:30,943 INFO  [stdout] (ServerService Thread Pool -- 14) 2017-09-19 18:43:30 INFO  DbMigrate:44 - Current version of schema "TEST_SERVER": << Empty Schema >>
18:43:30,945 INFO  [stdout] (ServerService Thread Pool -- 14) 2017-09-19 18:43:30 INFO  DbMigrate:44 - Schema "TEST_SERVER" is up to date. No migration necessary.
18:43:30,974 INFO  [stdout] (ServerService Thread Pool -- 14) 2017-09-19 18:43:30 INFO  DEBUG_LOGGER:48 - FlyWayMigrator - flywayMigrateStop

我的源代码

public void flywayMigrateStart() 
    LoggerUtil.getDEBUG_LOGGER().info("FlyWayMigrator - flywayMigrateStart");
    String flywayMinVersion = FlyWayProperty.getInstance().getProperty(FlyWayProperty.FLYWAY_MIN_VERSION);

    if (flywayMinVersion != null) 
        Flyway flyway = new Flyway();
        try 
            flyway.setDataSource(createDataSource());
            flyway.setTable(FlyWayProperty.getInstance().getProperty(FlyWayProperty.FLYWAY_MIGRATION_TABLE_NAME));
            flyway.setBaselineVersionAsString(FlyWayProperty.getInstance().getProperty(FlyWayProperty.FLYWAY_BASELINE_VERSION));
            flyway.setSqlMigrationSeparator(FlyWayProperty.getInstance().getProperty(FlyWayProperty.FLYWAY_MIGRATION_SEPARATOR));
            flyway.setEncoding(FlyWayProperty.getInstance().getProperty(FlyWayProperty.FLYWAY_ENCODING));
            flyway.setSchemas(FlyWayProperty.getInstance().getProperty(FlyWayProperty.ORACLE_DB_USERNAME));
            flyway.setBaselineOnMigrate(true);
            flyway.migrate();
            flywayMigrateStop();
         catch (Exception e) 
            throw new RuntimeException(e.getMessage(), e);
        
    

我的属性文件

ORACLE_DB_DRIVER_CLASS=oracle.jdbc.driver.OracleDriver
ORACLE_DB_URL=jdbc:oracle:thin:@192.168.100.210:1521:DB
ORACLE_DB_USERNAME=TEST_SERVER
ORACLE_DB_PASSWORD=TEST_SERVER
FLYWAY_MIN_VERSION=V1
FLYWAY_MIGRATION_TABLE_NAME=SERVER_VERSION_DB
FLYWAY_BASELINE_VERSION=1.0.0
FLYWAY_MIGRATION_SEPARATOR=#
FLYWAY_ENCODING=UTF-8

【问题讨论】:

需要更多信息。 什么不工作?这些日志声明表明 Flyway 认为不需要迁移。 18:43:30,945 INFO [stdout] (ServerService Thread Pool -- 14) 2017-09-19 18:43:30 INFO DbMigrate:44 - Schema "TEST_SERVER" is up to date. No migration necessary. 我添加了一些信息 @BobKuhar 有两个脚本没有执行。该方案完全是空的。 【参考方案1】:

Flyway 找不到您的迁移文件,因此它认为一切都是最新的:

INFO  DbValidate:44 - Successfully validated 0 migrations (execution time 00:00.018s)

您需要指定一个位置或将您的迁移移动到它已经在搜索的位置。使用setLocations() 这样做。

【讨论】:

【参考方案2】:

如果您使用 maven 构建此 jar - flyway-1.0-SNAPSHOT.jar,请确保 src/main/resources 有一个包含您的 V1__20170918_Initial_structure.sql 的 db/migration 文件夹

如果它能够解决您的问题,请告诉我。

【讨论】:

以上是关于FlyWay 迁移脚本的主要内容,如果未能解决你的问题,请参考以下文章

flyway:每次迁移后运行的通用脚本

Flyway 未接收 Java 迁移

最佳实践:使用后如何修改flyway迁移脚本

Flyway - 多个 git 分支上的 SQL 迁移脚本版本

Flyway 最佳实践:一个大型迁移脚本与多个增量脚本

基于 State/BaselineMigration 脚本的迁移后 FlyWay 迁移出错