'I cant create h2 database before tests, but after first test other tests work fine
i am trying to make my tests on h2 database. But my first test always falling with error
nested exception is io.r2dbc.spi.R2dbcBadGrammarException: [90079] [90079] Schema "DEF" not found; SQL statement:
although other tests for fine.
For making database i use @BeforeEach annotation here code:
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();
I tried rewriting sql code, or using another sql dialect
Solution 1:[1]
After some research i found new way of executing sql before tests:
Mono.from(connectionFactory.create())
.map(
c -> {
ScriptUtils.executeSqlScript(c, new ClassPathResource("scripts/create.sql"))
.subscribe();
return "123";
})
.subscribe();
Here u create connection from your connection factory, then using sql file(in my case it is create.sql at scripts package at resources),execute your sql script
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | aleksf16 |