Skip to content

Commit 2587af6

Browse files
authored
[exec-sql-h2] run sql scripts in H2 (#18697)
1 parent 696c7f4 commit 2587af6

File tree

7 files changed

+100
-0
lines changed

7 files changed

+100
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.baeldung.h2execscript;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class H2ExecScriptDemoApplication {
8+
public static void main(String... args) {
9+
SpringApplication.run(H2ExecScriptDemoApplication.class, args);
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.baeldung.h2execscript;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.util.List;
6+
7+
import org.junit.jupiter.api.MethodOrderer;
8+
import org.junit.jupiter.api.Order;
9+
import org.junit.jupiter.api.Test;
10+
import org.junit.jupiter.api.TestMethodOrder;
11+
import org.junit.jupiter.api.extension.ExtendWith;
12+
import org.springframework.beans.factory.annotation.Autowired;
13+
import org.springframework.boot.test.context.SpringBootTest;
14+
import org.springframework.test.context.ActiveProfiles;
15+
import org.springframework.test.context.junit.jupiter.SpringExtension;
16+
import org.springframework.transaction.annotation.Transactional;
17+
18+
import jakarta.persistence.EntityManager;
19+
20+
@ExtendWith(SpringExtension.class)
21+
@SpringBootTest(classes = H2ExecScriptDemoApplication.class)
22+
@ActiveProfiles("h2-exec-sql")
23+
@Transactional
24+
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
25+
public class H2ExecSqlIntegrationTest {
26+
27+
@Autowired
28+
private EntityManager entityManager;
29+
30+
@Test
31+
void whenUsingRunscriptInJdbcUrl_thenSqlExecuted() {
32+
List<String> expectedTaskNames = List.of("Start the application", "Check if data table is filled");
33+
List<String> taskNames = entityManager.createNativeQuery("SELECT NAME FROM TASK_TABLE ORDER BY ID")
34+
.getResultStream()
35+
.map(Object::toString)
36+
.toList();
37+
assertEquals(expectedTaskNames, taskNames);
38+
}
39+
40+
@Test
41+
@Order(1)
42+
void whenSpringAutoDetectSchemaAndDataSql_thenSqlExecuted() {
43+
List<String> expectedCityNames = List.of("New York", "Hamburg", "Shanghai");
44+
List<String> cityNames = entityManager.createNativeQuery("SELECT NAME FROM CITY ORDER BY ID")
45+
.getResultStream()
46+
.map(Object::toString)
47+
.toList();
48+
assertEquals(expectedCityNames, cityNames);
49+
}
50+
51+
@Test
52+
@Order(2)
53+
void whenRunscriptInNativeQuery_thenSqlExecuted() {
54+
entityManager.createNativeQuery("RUNSCRIPT FROM 'classpath:/sql/add_cities.sql'")
55+
.executeUpdate();
56+
List<String> expectedCityNames = List.of("New York", "Hamburg", "Shanghai", "Paris", "Berlin", "Tokyo");
57+
List<String> cityNames = entityManager.createNativeQuery("SELECT NAME FROM CITY ORDER BY ID")
58+
.getResultStream()
59+
.map(Object::toString)
60+
.toList();
61+
assertEquals(expectedCityNames, cityNames);
62+
}
63+
64+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
spring:
2+
datasource:
3+
driverClassName: org.h2.Driver
4+
url: jdbc:h2:mem:demodb;INIT=RUNSCRIPT FROM 'classpath:/sql/init_my_db.sql'
5+
username: sa
6+
password:
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
INSERT INTO CITY (ID, NAME) VALUES (1, 'New York');
2+
INSERT INTO CITY (ID, NAME) VALUES (2, 'Hamburg');
3+
INSERT INTO CITY (ID, NAME) VALUES (3, 'Shanghai');
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CREATE TABLE CITY
2+
(
3+
ID INT PRIMARY KEY ,
4+
NAME VARCHAR(255)
5+
);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
INSERT INTO CITY (ID, NAME) VALUES (4, 'Paris');
2+
INSERT INTO CITY (ID, NAME) VALUES (5, 'Berlin');
3+
INSERT INTO CITY (ID, NAME) VALUES (6, 'Tokyo');
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CREATE TABLE TASK_TABLE
2+
(
3+
ID INT PRIMARY KEY,
4+
NAME VARCHAR(255)
5+
);
6+
7+
INSERT INTO TASK_TABLE (ID, NAME) VALUES (1, 'Start the application');
8+
INSERT INTO TASK_TABLE (ID, NAME) VALUES (2, 'Check if data table is filled');

0 commit comments

Comments
 (0)