我无法在测试前创建 h2 数据库,但在第一次测试后其他测试工作正常
I cant create h2 database before tests, but after first test other tests work fine
我正在尝试对 h2 数据库进行测试。但是我的第一次测试总是报错
nested exception is io.r2dbc.spi.R2dbcBadGrammarException: [90079] [90079] Schema "DEF" not found; SQL statement:
虽然其他测试没问题。
为了制作数据库,我使用@BeforeEach 注释
这里代码:
String sql =
/*"DROP SCHEMA DEF;\n" +*/
"CREATE SCHEMA IF NOT EXISTS DEF; \n " +
"CREATE TABLE IF NOT EXISTS def.definition ();\n" +
"ALTER TABLE def.definition ADD COLUMN IF NOT EXISTS id varchar NOT NULL ;\n" +
"ALTER TABLE def.definition ADD COLUMN IF NOT EXISTS path varchar NOT NULL default NULL;\n" +
"ALTER TABLE def.definition ADD COLUMN IF NOT EXISTS name varchar NULL;\n" +
"ALTER TABLE def.definition ADD COLUMN IF NOT EXISTS description varchar NULL;\n" +
"ALTER TABLE def.definition ADD COLUMN IF NOT EXISTS type varchar NULL;\n" +
"ALTER TABLE def.definition ADD COLUMN IF NOT EXISTS \"schema\" json NULL;\n" +
"ALTER TABLE def.definition ADD CONSTRAINT IF NOT EXISTS definition_pk PRIMARY KEY (id);\n" +
"ALTER TABLE def.definition ADD CONSTRAINT IF NOT EXISTS definition_un UNIQUE (path);\n" +
"CREATE INDEX IF NOT EXISTS definition_name_idx ON def.definition (name);\n" +
"CREATE INDEX IF NOT EXISTS definition_description_idx ON def.definition (description);";
Mono<Integer> integerMono = databaseClient.sql(sql)
.fetch()
.rowsUpdated();
StepVerifier.create(integerMono)
.expectNextCount(1)
.verifyComplete();
我尝试重写 sql 代码,或使用另一种 sql 方言
经过一些研究,我发现了在测试前执行 sql 的新方法:
Mono.from(connectionFactory.create())
.map(
c -> {
ScriptUtils.executeSqlScript(c, new ClassPathResource("scripts/create.sql"))
.subscribe();
return "123";
})
.subscribe();
在这里你从你的连接工厂创建连接,然后使用 sql 文件(在我的例子中它是 create.sql 在资源的脚本包中),执行你的 sql 脚本
我正在尝试对 h2 数据库进行测试。但是我的第一次测试总是报错
nested exception is io.r2dbc.spi.R2dbcBadGrammarException: [90079] [90079] Schema "DEF" not found; SQL statement:
虽然其他测试没问题。
为了制作数据库,我使用@BeforeEach 注释 这里代码:
String sql =
/*"DROP SCHEMA DEF;\n" +*/
"CREATE SCHEMA IF NOT EXISTS DEF; \n " +
"CREATE TABLE IF NOT EXISTS def.definition ();\n" +
"ALTER TABLE def.definition ADD COLUMN IF NOT EXISTS id varchar NOT NULL ;\n" +
"ALTER TABLE def.definition ADD COLUMN IF NOT EXISTS path varchar NOT NULL default NULL;\n" +
"ALTER TABLE def.definition ADD COLUMN IF NOT EXISTS name varchar NULL;\n" +
"ALTER TABLE def.definition ADD COLUMN IF NOT EXISTS description varchar NULL;\n" +
"ALTER TABLE def.definition ADD COLUMN IF NOT EXISTS type varchar NULL;\n" +
"ALTER TABLE def.definition ADD COLUMN IF NOT EXISTS \"schema\" json NULL;\n" +
"ALTER TABLE def.definition ADD CONSTRAINT IF NOT EXISTS definition_pk PRIMARY KEY (id);\n" +
"ALTER TABLE def.definition ADD CONSTRAINT IF NOT EXISTS definition_un UNIQUE (path);\n" +
"CREATE INDEX IF NOT EXISTS definition_name_idx ON def.definition (name);\n" +
"CREATE INDEX IF NOT EXISTS definition_description_idx ON def.definition (description);";
Mono<Integer> integerMono = databaseClient.sql(sql)
.fetch()
.rowsUpdated();
StepVerifier.create(integerMono)
.expectNextCount(1)
.verifyComplete();
我尝试重写 sql 代码,或使用另一种 sql 方言
经过一些研究,我发现了在测试前执行 sql 的新方法:
Mono.from(connectionFactory.create())
.map(
c -> {
ScriptUtils.executeSqlScript(c, new ClassPathResource("scripts/create.sql"))
.subscribe();
return "123";
})
.subscribe();
在这里你从你的连接工厂创建连接,然后使用 sql 文件(在我的例子中它是 create.sql 在资源的脚本包中),执行你的 sql 脚本