原创

FlyWay 迁移后无法连接到 H2 数据库

温馨提示:
本文最后更新于 2024年04月12日,已超过 37 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我

I want to use H2 database for running code locally, migrated with FlyWay

the connection parameters are

url="jdbc:h2:file:./target/my-database;MODE=PostgreSQL;DATABASE_TO_LOWER=TRUE;DEFAULT_NULL_ORDERING=HIGH"
user=sa
password=sa

pom.xml:

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>2.2.224</version>
        </dependency>
        <dependency>
            <groupId>org.flywaydb</groupId>
            <artifactId>flyway-core</artifactId>
            <version>10.11.0</version>
        </dependency>

I have a single migration:

CREATE TABLE workspaces
(
    id uuid default RANDOM_UUID() primary key,
    name text
);

I run FlyWay like this:

    val flyway = Flyway.configure().dataSource(url, user, password).load()
    flyway.migrate()

FlyWay migrations run without errors:

 o.f.c.i.s.classpath.ClassPathScanner - Scanning for classpath resources at 'classpath:db/callback' ...
 o.f.c.i.s.classpath.ClassPathScanner - Determining location urls for classpath:db/callback using ClassLoader jdk.internal.loader.ClassLoaders$AppClassLoader@4e0e2f2a ...
 o.f.c.i.s.classpath.ClassPathScanner - Unable to resolve location classpath:db/callback.
 o.f.c.i.s.classpath.ClassPathScanner - Scanning for classpath resources at 'classpath:db/migration/h2' ...
 o.f.c.i.s.classpath.ClassPathScanner - Determining location urls for classpath:db/migration/h2 using ClassLoader jdk.internal.loader.ClassLoaders$AppClassLoader@4e0e2f2a ...
 o.f.c.i.s.classpath.ClassPathScanner - Scanning URL: file:.../target/classes/db/migration/h2
 o.f.c.i.s.c.FileSystemClassPathLocationScanner - Scanning starting at classpath root in filesystem: .../target/classes/
 o.f.c.i.s.c.FileSystemClassPathLocationScanner - Scanning for resources in path: .../target/classes/db/migration/h2 (db/migration/h2)
 o.f.c.i.s.classpath.ClassPathScanner - Found resource: db/migration/h2/V001__workspaces.sql
 o.f.c.i.s.classpath.ClassPathScanner - Scanning for classes at classpath:db/migration/h2
 o.f.c.i.r.ResourceNameValidator - Validating V001__workspaces.sql
  org.flywaydb.core.FlywayExecutor - Database: jdbc:h2:file:./target/my-database (H2 2.2)
2024-04-11 17:57:31.395 [main] DEBUG org.flywaydb.core.FlywayExecutor - Driver: H2 JDBC Driver 2.2.224 (2023-09-17)
2024-04-11 17:57:31.396 [main] DEBUG org.flywaydb.core.FlywayExecutor - DDL Transactions Supported: false
 o.f.c.i.s.SchemaHistoryFactory - Schemas: 
 o.f.c.i.s.SchemaHistoryFactory - Default schema: null
 o.f.c.i.c.SqlScriptCallbackFactory - Scanning for SQL callbacks ...
 o.f.core.internal.command.DbValidate - Validating migrations ...
 o.f.c.a.c.ClassicConfiguration - CherryPickConfigurationExtension not found
 o.f.core.internal.scanner.Scanner - Filtering out resource: db/migration/h2/V001__workspaces.sql (filename: V001__workspaces.sql)
 o.f.core.internal.scanner.Scanner - Filtering out resource: db/migration/h2/V001__workspaces.sql (filename: V001__workspaces.sql)
  o.f.c.i.s.JdbcTableSchemaHistory - Schema history table "public"."flyway_schema_history" does not exist yet
  o.f.core.internal.command.DbValidate - Successfully validated 1 migration (execution time 00:00.007s)
 o.f.core.internal.command.DbSchemas - Skipping creation of existing schema: "public"
  o.f.c.i.s.JdbcTableSchemaHistory - Creating Schema History table "public"."flyway_schema_history" ...
 o.f.core.internal.parser.Parser - Parsing  ...
 o.f.c.i.sqlscript.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, 'sa', CURRENT_TIMESTAMP, 0, TRUE
 o.f.c.i.sqlscript.ParserSqlScript - Found statement at line 14: CREATE INDEX "public"."flyway_schema_history_s_idx" ON "public"."flyway_schema_history" ("success")
 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, 'sa', CURRENT_TIMESTAMP, 0, TRUE
 o.f.c.i.s.DefaultSqlScriptExecutor - 0rows affected
 o.f.c.i.s.DefaultSqlScriptExecutor - Executing SQL: CREATE INDEX "public"."flyway_schema_history_s_idx" ON "public"."flyway_schema_history" ("success")
 o.f.c.i.s.DefaultSqlScriptExecutor - 0rows affected
 o.f.c.i.s.JdbcTableSchemaHistory - Created Schema History table "public"."flyway_schema_history"
 o.f.c.a.c.ClassicConfiguration - CherryPickConfigurationExtension not found
  o.f.core.internal.command.DbMigrate - Current version of schema "public": << Empty Schema >>
 o.f.core.internal.parser.Parser - Parsing V001__workspaces.sql ...
 o.f.c.i.sqlscript.ParserSqlScript - Found statement at line 1: CREATE TABLE workspaces
(
    id uuid default RANDOM_UUID() primary key,
    name text
)
 o.f.core.internal.command.DbMigrate - Starting migration of schema "public" to version "001 - workspaces" ...
  o.f.core.internal.command.DbMigrate - Migrating schema "public" to version "001 - workspaces"
 o.f.c.i.s.DefaultSqlScriptExecutor - Executing SQL: CREATE TABLE workspaces
(
    id uuid default RANDOM_UUID() primary key,
    name text
)
 o.f.c.i.s.DefaultSqlScriptExecutor - 0rows affected
 o.f.core.internal.command.DbMigrate - Successfully completed migration of schema "public" to version "001 - workspaces"
 o.f.c.i.s.JdbcTableSchemaHistory - Schema History table "public"."flyway_schema_history" successfully updated to reflect changes
 o.f.c.a.c.ClassicConfiguration - CherryPickConfigurationExtension not found
  o.f.core.internal.command.DbMigrate - Successfully applied 1 migration to schema "public", now at version v001 (execution time 00:00.001s)
 org.flywaydb.core.FlywayExecutor - Memory usage: 39 of 80M

Now after I terminate my application (it's running with ktor) and try to connect to the database with any client (Tried DataGrip, DBeaver, jOOQ Generator), I got the following error:

[90079][90079] Schema "public" not found; SQL statement:
 CREATE CACHED TABLE "public"."flyway_schema_history"(
 "installed_rank" INTEGER NOT NULL,
 "version" CHARACTER VARYING(50), 
"description" CHARACTER VARYING(200) NOT NULL, 
"type" CHARACTER VARYING(20) NOT NULL,
 "script" CHARACTER VARYING(1000) NOT NULL, 
"checksum" INTEGER,
 "installed_by" CHARACTER VARYING(100) NOT NULL,
 "installed_on" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
 "execution_time" INTEGER NOT NULL, 
"success" BOOLEAN NOT NULL ) [90079-220].

Then a "my-database.trace.db" file is created which also contains this message. I need to be able to connect because I want to generate record classes with jooq-codegen:3.16.22:generate.

Why is that happening?

正文到此结束
热门推荐
本文目录