Flyway 在 sbt 中找不到我的迁移文件
Flyway not finding my migrations file in sbt
我正在尝试应用迁移来建立 H2 数据库进行测试,我的文件夹结构是:
src
- test
-- resources
--- db
---- migration
----- V1_0__init_tables.sql
我的配置设置为通过
指向 classpath:db/migration
Flyway
.configure()
.locations(migrateLocations)
.dataSource(url, user, password)
.load()
.migrate()
当我 运行 我的测试时,我得到以下输出
2018-11-29 16:14:14,351 DEBUG [ScalaTest-run] slf4j.Slf4jLog (Slf4jLog.java:45) - Scanning for classpath resources at 'classpath:db/migration' ...
2018-11-29 16:14:14,351 DEBUG [ScalaTest-run] slf4j.Slf4jLog (Slf4jLog.java:45) - Determining location urls for classpath:db/migration using ClassLoader sun.misc.Launcher$AppClassLoader@18b4aac2 ...
2018-11-29 16:14:14,420 WARN [ScalaTest-run] slf4j.Slf4jLog (Slf4jLog.java:53) - Unable to resolve location classpath:db/migration
2018-11-29 16:14:14,421 DEBUG [ScalaTest-run] slf4j.Slf4jLog (Slf4jLog.java:45) - Scanning for classes at classpath:db/migration
这让我很困惑为什么 Flyway
找不到我的文件。据我所知,我已经按照文档的建议完成了所有操作。我的测试最终失败了,因为我的 table 从未迁移过,因此 Slick
无法找到。
我的迁移文件中可能存在错误,但我假设 Flyway
会对此进行某种记录?我没有看到类似的东西。
您是否故意将迁移文件放在 src/test/resources
下?无论如何,我认为 Flyway 不会在那里找到它们,因为您的 classpath:db/migration
很可能会引用 src/main/resources
,而不是测试资源。
只需尝试将您的迁移文件放在主要资源下即可。
Spring Boot 2 将 spring.flyway.locations=classpath:db/migration
设置为默认值并指向 src/main/resources/db/migration
文件夹,而不管您 运行 单元测试。您可以将 SQL 文件目录放入文件系统,然后设置 spring.flyway.locations=filesystem:path/to/yourdir
.
可以在项目文件夹内的任何位置添加 sql 迁移脚本,例如 prj_root/resources/migrations
(其中 resources
是 src
文件夹的兄弟)。
要将其作为资源嵌入,需要在 build.sbt
:
unmanagedResourceDirectories in Compile += baseDirectory.value / "resources"
然后在迁移代码中指定这个位置:
Flyway.configure().locations("/migrations")...
在idea中将这个新资源目录标记为Resources Root
也是有道理的,但不是必须的,只是美化一下。
我正在尝试应用迁移来建立 H2 数据库进行测试,我的文件夹结构是:
src
- test
-- resources
--- db
---- migration
----- V1_0__init_tables.sql
我的配置设置为通过
指向classpath:db/migration
Flyway
.configure()
.locations(migrateLocations)
.dataSource(url, user, password)
.load()
.migrate()
当我 运行 我的测试时,我得到以下输出
2018-11-29 16:14:14,351 DEBUG [ScalaTest-run] slf4j.Slf4jLog (Slf4jLog.java:45) - Scanning for classpath resources at 'classpath:db/migration' ...
2018-11-29 16:14:14,351 DEBUG [ScalaTest-run] slf4j.Slf4jLog (Slf4jLog.java:45) - Determining location urls for classpath:db/migration using ClassLoader sun.misc.Launcher$AppClassLoader@18b4aac2 ...
2018-11-29 16:14:14,420 WARN [ScalaTest-run] slf4j.Slf4jLog (Slf4jLog.java:53) - Unable to resolve location classpath:db/migration
2018-11-29 16:14:14,421 DEBUG [ScalaTest-run] slf4j.Slf4jLog (Slf4jLog.java:45) - Scanning for classes at classpath:db/migration
这让我很困惑为什么 Flyway
找不到我的文件。据我所知,我已经按照文档的建议完成了所有操作。我的测试最终失败了,因为我的 table 从未迁移过,因此 Slick
无法找到。
我的迁移文件中可能存在错误,但我假设 Flyway
会对此进行某种记录?我没有看到类似的东西。
您是否故意将迁移文件放在 src/test/resources
下?无论如何,我认为 Flyway 不会在那里找到它们,因为您的 classpath:db/migration
很可能会引用 src/main/resources
,而不是测试资源。
只需尝试将您的迁移文件放在主要资源下即可。
Spring Boot 2 将 spring.flyway.locations=classpath:db/migration
设置为默认值并指向 src/main/resources/db/migration
文件夹,而不管您 运行 单元测试。您可以将 SQL 文件目录放入文件系统,然后设置 spring.flyway.locations=filesystem:path/to/yourdir
.
可以在项目文件夹内的任何位置添加 sql 迁移脚本,例如 prj_root/resources/migrations
(其中 resources
是 src
文件夹的兄弟)。
要将其作为资源嵌入,需要在 build.sbt
:
unmanagedResourceDirectories in Compile += baseDirectory.value / "resources"
然后在迁移代码中指定这个位置:
Flyway.configure().locations("/migrations")...
在idea中将这个新资源目录标记为Resources Root
也是有道理的,但不是必须的,只是美化一下。