我对我的一个项目使用的 Flyway 迁移有点困惑,并且无法运行迁移脚本。这是我的项目设置(这是一个 Scala 项目),如下所示:
我有一个 application.conf,其中包含以下内容:
my-platform.jdbc {
driver = "org.h2.Driver"
dbName = "my-platform-h2-test.db;DB_CLOSE_ON_EXIT=FALSE;MODE=MYSQL"
url = "jdbc:h2:~/my-platform/database/"${my-platform.jdbc.dbName}
user = "sa"
migrations-table = "MyPlatformDBSchema"
migrations-locations = [
"classpath:h2/"
]
}
我正在使用 Typesafe 配置读取该配置,然后遍历该配置并运行我的 sbt 项目,这会导致以下错误:
INFO: Current version of schema "PUBLIC": << Empty Schema >>
Aug 28, 2023 9:33:23 PM org.flywaydb.core.internal.command.DbMigrate doMigrateGroup
INFO: Migrating schema "PUBLIC" to version "001 - baseline create initial tables"
org.flywaydb.core.internal.exception.FlywaySqlException: Unable to insert row for version '001' in Schema History table "PUBLIC"."MyPlatformDBSchema"
---------------------------------------------------------------------------------------------
SQL State : 42S02
Error Code : 42102
Message : Table "MyPlatformDBSchema" not found; SQL statement:
INSERT INTO "PUBLIC"."MyPlatformDBSchema" ("installed_rank", "version", "description", "type", "script", "checksum", "installed_by", "execution_time", "success") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) [42102-214]
at org.flywaydb.core.internal.schemahistory.JdbcTableSchemaHistory.doAddAppliedMigration(JdbcTableSchemaHistory.java:177)
at org.flywaydb.core.internal.schemahistory.SchemaHistory.addAppliedMigration(SchemaHistory.java:192)
at org.flywaydb.core.internal.command.DbMigrate.doMigrateGroup(DbMigrate.java:402)
at org.flywaydb.core.internal.command.DbMigrate.lambda$applyMigrations$1(DbMigrate.java:274)
我的 src/main/resources 文件夹中有一个名为 h2 的文件夹,其中有名为 V001__baseline_create_initial_tables.sql 的 sql 文件,其中包含我想要用于数据库的架构,如下所示:
--
-- Drop everything if exists
--
DROP ALL OBJECTS DELETE FILES;
--
-- Schema for my-platform
--
CREATE SCHEMA my_platform_schema AUTHORIZATION SA;
SET SCHEMA my_platform_schema;
....
....
-- The remaining create tables
....
....
我在这里做错了什么?
事实证明,这对我来说是一个简单的错误。在我的 SQL 脚本中,我将以下内容作为第一行:
DROP ALL OBJECTS DELETE FILES
这就是导致问题的原因。一旦我删除了这一行,我的迁移就开始运行!