Flyway V 7.8.1 不会迁移它创建 H2 表的第一个脚本

Posted

技术标签:

【中文标题】Flyway V 7.8.1 不会迁移它创建 H2 表的第一个脚本【英文标题】:Flyway V 7.8.1 doesn't migrate the first script that it creates H2 table 【发布时间】:2021-07-15 14:51:23 【问题描述】:

Flyway V 7.8.1 标识第一次创建 H2 数据库时更新了 PUBLIC 架构,并且它不执行创建示例表的第一个 sql 脚本。

日志执行为:

[INFO] Running example.credentials.flyway.entities.Test1Flyway
13:39:09:593 (main) DEBUG [i.q.f.r.QuarkusPathLocationScanner] Locations: [classpath:db/migration-test]
13:39:09:596 (main) DEBUG [i.q.f.r.QuarkusPathLocationScanner] Loading db/migration-test/V1.0.0__INITIAL_STRUCTURE_OF_TABLES_WITH_SOME_DATA
13:39:09:603 (main) DEBUG [o.f.c.i.s.c.ClassPathScanner] Scanning for classpath resources at 'classpath:db/callback' ...
13:39:09:604 (main) DEBUG [o.f.c.i.s.c.ClassPathScanner] Determining location urls for classpath:db/callback using ClassLoader QuarkusClassLoader:Quarkus Base Runtime ClassLoader ...
13:39:09:604 (main) DEBUG [o.f.c.i.s.c.ClassPathScanner] Unable to resolve location classpath:db/callback.
13:39:09:609 (main) INFO  [o.f.c.i.l.VersionPrinter] Flyway Community Edition 7.8.1 by Redgate
13:39:09:730 (agroal-11) DEBUG [e.c.f.s.c.CipherManager] Shared key File: shared.key
13:39:09:731 (agroal-11) DEBUG [e.c.f.s.c.CipherManager] Shared key Type File: resource
13:39:09:731 (agroal-11) DEBUG [e.c.f.s.c.CipherManager] LaunchMode (current): TEST
13:39:09:750 (agroal-11) DEBUG [e.c.f.s.MyBbddCredentialsProvider] my-bbdd-credentials-provider/username:tools
13:39:09:750 (agroal-11) DEBUG [e.c.f.s.MyBbddCredentialsProvider] my-bbdd-credentials-provider/password:********
13:39:10:238 (main) INFO  [o.f.c.i.d.b.DatabaseType] Database: jdbc:h2:./target/db/example (H2 1.4)
13:39:10:238 (main) DEBUG [o.f.c.i.d.b.DatabaseType] Driver  : H2 JDBC Driver 1.4.200 (2019-10-14)
13:39:10:328 (main) DEBUG [o.f.c.Flyway] DDL Transactions Supported: false
13:39:10:328 (main) DEBUG [o.f.c.Flyway] Schemas: public
13:39:10:328 (main) DEBUG [o.f.c.Flyway] Default schema: null
13:39:10:333 (main) DEBUG [o.f.c.i.c.SqlScriptCallbackFactory] Scanning for SQL callbacks ...
13:39:10:334 (main) DEBUG [i.q.f.r.FlywayRecorder] Filtering out resource: db/migration-test/V1.0.0__INITIAL_STRUCTURE_OF_TABLES_WITH_SOME_DATA (filename: V1.0.0__INITIAL_STRUCTURE_OF_TABLES_WITH_SOME_DATA)
13:39:10:363 (main) DEBUG [o.f.c.i.c.DbClean] Dropping pre-schema database level objects...
13:39:10:365 (main) INFO  [o.f.c.i.c.DbClean] Successfully dropped pre-schema database level objects (execution time 00:00.001s)
13:39:10:366 (main) WARN  [o.f.c.i.c.DbClean] Unable to clean unknown schema: "public"
13:39:10:366 (main) DEBUG [o.f.c.i.c.DbClean] Dropping post-schema database level objects...
13:39:10:367 (main) INFO  [o.f.c.i.c.DbClean] Successfully dropped post-schema database level objects (execution time 00:00.000s)
13:39:10:367 (agroal-11) DEBUG [e.c.f.s.c.CipherManager] Shared key File: shared.key
13:39:10:368 (agroal-11) DEBUG [e.c.f.s.c.CipherManager] Shared key Type File: resource
13:39:10:368 (agroal-11) DEBUG [e.c.f.s.c.CipherManager] LaunchMode (current): TEST
13:39:10:370 (agroal-11) DEBUG [e.c.f.s.MyBbddCredentialsProvider] my-bbdd-credentials-provider/username:tools
13:39:10:371 (agroal-11) DEBUG [e.c.f.s.MyBbddCredentialsProvider] my-bbdd-credentials-provider/password:********
13:39:10:373 (main) DEBUG [o.f.c.Flyway] Memory usage: 122 of 305M
13:39:10:374 (main) INFO  [o.f.c.i.l.VersionPrinter] Flyway Community Edition 7.8.1 by Redgate
13:39:10:376 (main) DEBUG [o.f.c.Flyway] DDL Transactions Supported: false
13:39:10:377 (main) DEBUG [o.f.c.Flyway] Schemas: public
13:39:10:377 (main) DEBUG [o.f.c.Flyway] Default schema: null
13:39:10:379 (main) DEBUG [o.f.c.i.c.SqlScriptCallbackFactory] Scanning for SQL callbacks ...
13:39:10:379 (main) DEBUG [i.q.f.r.FlywayRecorder] Filtering out resource: db/migration-test/V1.0.0__INITIAL_STRUCTURE_OF_TABLES_WITH_SOME_DATA (filename: V1.0.0__INITIAL_STRUCTURE_OF_TABLES_WITH_SOME_DATA)
13:39:10:390 (main) DEBUG [o.f.c.i.c.DbSchemas] Creating schema: "public"
13:39:10:390 (main) INFO  [o.f.c.i.d.b.Schema] Creating schema "public" ...
13:39:10:400 (main) INFO  [o.f.c.i.s.JdbcTableSchemaHistory] Creating Schema History table "public"."flyway_schema_history" ...
13:39:10:410 (main) DEBUG [o.f.c.i.p.Parser] Parsing  ...
13:39:10:421 (main) DEBUG [o.f.c.i.s.ParserSqlScript] Found statement at line 1: CREATE TABLE IF NOT EXISTS "public"."flyway_schema_history" (
    "installed_rank" INT NOT NULL,
    "version" VARCHAR(50),
    "description" VARCHAR(200) NOT NULL,
    "type" VARCHAR(20) NOT NULL,
    "script" VARCHAR(1000) NOT NULL,
    "checksum" INT,
    "installed_by" VARCHAR(100) NOT NULL,
    "installed_on" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "execution_time" INT NOT NULL,
    "success" BOOLEAN NOT NULL,
    CONSTRAINT "flyway_schema_history_pk" PRIMARY KEY ("installed_rank")
) AS SELECT -1, NULL, '<< Flyway Schema History table created >>', 'TABLE', '', NULL, 'TOOLS', CURRENT_TIMESTAMP, 0, TRUE
13:39:10:422 (main) DEBUG [o.f.c.i.s.ParserSqlScript] Found statement at line 14: CREATE INDEX "public"."flyway_schema_history_s_idx" ON "public"."flyway_schema_history" ("success")
13:39:10:422 (main) DEBUG [o.f.c.i.s.DefaultSqlScriptExecutor] Executing SQL: CREATE TABLE IF NOT EXISTS "public"."flyway_schema_history" (
    "installed_rank" INT NOT NULL,
    "version" VARCHAR(50),
    "description" VARCHAR(200) NOT NULL,
    "type" VARCHAR(20) NOT NULL,
    "script" VARCHAR(1000) NOT NULL,
    "checksum" INT,
    "installed_by" VARCHAR(100) NOT NULL,
    "installed_on" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "execution_time" INT NOT NULL,
    "success" BOOLEAN NOT NULL,
    CONSTRAINT "flyway_schema_history_pk" PRIMARY KEY ("installed_rank")
) AS SELECT -1, NULL, '<< Flyway Schema History table created >>', 'TABLE', '', NULL, 'TOOLS', CURRENT_TIMESTAMP, 0, TRUE
13:39:10:434 (main) DEBUG [o.f.c.i.s.DefaultSqlScriptExecutor] 0 rows affected
13:39:10:434 (main) DEBUG [o.f.c.i.s.DefaultSqlScriptExecutor] Executing SQL: CREATE INDEX "public"."flyway_schema_history_s_idx" ON "public"."flyway_schema_history" ("success")
13:39:10:437 (main) DEBUG [o.f.c.i.s.DefaultSqlScriptExecutor] 0 rows affected
13:39:10:438 (main) DEBUG [o.f.c.i.s.JdbcTableSchemaHistory] Created Schema History table "public"."flyway_schema_history"
13:39:10:456 (main) DEBUG [o.f.c.i.s.JdbcTableSchemaHistory] Schema History table "public"."flyway_schema_history" successfully updated to reflect changes
13:39:10:482 (main) DEBUG [i.q.f.r.FlywayRecorder] Filtering out resource: db/migration-test/V1.0.0__INITIAL_STRUCTURE_OF_TABLES_WITH_SOME_DATA (filename: V1.0.0__INITIAL_STRUCTURE_OF_TABLES_WITH_SOME_DATA)
13:39:10:482 (main) DEBUG [i.q.f.r.FlywayRecorder] Filtering out resource: db/migration-test/V1.0.0__INITIAL_STRUCTURE_OF_TABLES_WITH_SOME_DATA (filename: V1.0.0__INITIAL_STRUCTURE_OF_TABLES_WITH_SOME_DATA)
13:39:10:492 (main) INFO  [o.f.c.i.c.DbMigrate] Current version of schema "public": null
13:39:10:497 (main) INFO  [o.f.c.i.c.DbMigrate] Schema "public" is up to date. No migration necessary.
13:39:10:498 (main) DEBUG [o.f.c.Flyway] Memory usage: 128 of 305M
13:39:11:910 (main) INFO  [e.c.f.e.Test1Flyway] Starting flyway test
13:39:11:916 (main) ERROR [e.c.f.e.ExampleE] SQL Error: : org.h2.jdbc.JdbcSQLSyntaxErrorException: Tabla "T_EXAMPLES" no encontrada
Table "T_EXAMPLES" not found; SQL statement:
INSERT INTO T_EXAMPLES (example_id, description) VALUES (?,?) [42102-200]
        at org.h2.message.DbException.getJdbcSQLException(DbException.java:453)
        at org.h2.message.DbException.getJdbcSQLException(DbException.java:429)

我的quarkus 1.13.2_Final application.yaml配置是:

"%test":
  quarkus:
    log:
      level: ERROR
      console:
        format: "%dHH:mm:ss:SSS (%t) %-5p [%c1.] %s%e%n"
        color: true
      category:
        "example.credentials.flyway":
          level: DEBUG
        "org.flywaydb.core":
          level: DEBUG
        "io.quarkus.flyway":
          level: DEBUG
        "org.h2":
          level: DEBUG
    datasource:
      db-kind: h2
      jdbc:
        driver: org.h2.Driver
#        url: jdbc:h2:tcp://localhost/mem:example;MODE=Oracle
#        url: jdbc:h2:./target/db/example;MODE=Oracle,AUTO_SERVER=TRUE
        url: jdbc:h2:./target/db/example;AUTO_SERVER=TRUE
        max-size: 13
    flyway:
      locations: db/migration-test
      clean-at-start: true
      migrate-at-start: true
      schemas: public

对于解决此问题的任何帮助,我将不胜感激。

【问题讨论】:

【参考方案1】:

Flyway 似乎忽略了您的迁移:

[i.q.f.r.FlywayRecorder] Filtering out resource: db/migration-test/V1.0.0__INITIAL_STRUCTURE_OF_TABLES_WITH_SOME_DATA (filename: V1.0.0__INITIAL_STRUCTURE_OF_TABLES_WITH_SOME_DATA)
[i.q.f.r.FlywayRecorder] Filtering out resource: db/migration-test/V1.0.0__INITIAL_STRUCTURE_OF_TABLES_WITH_SOME_DATA (filename: V1.0.0__INITIAL_STRUCTURE_OF_TABLES_WITH_SOME_DATA)

您的脚本中可能缺少 .sql 文件扩展名。

【讨论】:

谢谢。这就是原因。

以上是关于Flyway V 7.8.1 不会迁移它创建 H2 表的第一个脚本的主要内容,如果未能解决你的问题,请参考以下文章

Flyway 迁移在 MS SQL Server 中成功,但在 H2 数据库中失败

在 Spring Boot 应用程序上使用 Flyway 时如何在 H2 中加载初始数据?

如何创建具有快照隔离级别的 Flyway 模式历史记录?

Flyway / Spring和H2嵌入式数据库的架构相关问题

如何在spring boot中创建H2+flyway测试数据库?

使用 Flyway 和 Spring Boot 迁移基线