开发者

problem with INIT=RUNSCRIPT and relative paths

开发者 https://www.devze.com 2023-01-31 16:33 出处:网络
I use maven conventions for source paths (src/main src/test) and i have my sql scripts in src/main/resources/scripts.

I use maven conventions for source paths (src/main src/test) and i have my sql scripts in src/main/resources/scripts.

I want to run my app with H2 memory and i'd like to use the jdbc url to initialize my db :

database.url=jdbc:h2:mem:;INIT=RUNSCRIPT FROM 'src/main/resources/scripts/create.sql';

My problem is that this relative path (src/main/... ) does n开发者_高级运维ot work, and that H2 won't crash if the init=runscript command targets nothing.

Does someone know what is the path i should use to make this work ?

Thanks


You can use the following url:
"jdbc:h2:mem:sample;INIT=RUNSCRIPT FROM 'classpath:scripts/create.sql'"

With that one it is possible to run script from classpath. So you can just put it src/main/resources/scripts or src/test/resources/scripts in your maven (or something else) project.


I'd suggest trying to use an absolute path for starters, just to check everything works. Afterwards, check your classpath. For example, bin/main/resources/scripts/create.sql, assuming bin is where your classes are compiled, and is on your classpath.

Since src, where your source lives, usually isn't on the classpath, this could be the source of your problem.


For me it works if I keep the scripts under src/test/resources:

database.url=jdbc:h2:mem:;INIT=RUNSCRIPT FROM 'src/test/resources/create.sql';

Anyway, in a JUnit context, if you use the conventional file paths schema.sql (for the DDL) and data.sql (for the DDL) under src/test/resources, you don't need the specify the scripts with the INIT variable in the connection string. You can simply use:

database.url=jdbc:h2:mem:testdb

and JUnit will automatically run the scripts when creating the H2 instance in memory.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号