Skip to content

Commit

Permalink
BAEL-8008 - H2 running script from file (eugenp#16765)
Browse files Browse the repository at this point in the history
* BAEL-8008 - H2 running script from file

* BAEL-8008 - changing script file locations

* BAEL-8008 - moving to another module

* BAEL-8008 - moving to another module
  • Loading branch information
abh1navv authored Jun 15, 2024
1 parent 32d522a commit bb4a527
Show file tree
Hide file tree
Showing 10 changed files with 140 additions and 1 deletion.
2 changes: 1 addition & 1 deletion persistence-modules/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,9 @@
<module>spring-data-cassandra-2</module>
<module>spring-data-jpa-repo-3</module>
<module>spring-boot-persistence-4</module>
<module>spring-boot-persistence-5</module>
<module>hibernate-annotations-2</module>
<module>hibernate-reactive</module>

</modules>

<properties>
Expand Down
2 changes: 2 additions & 0 deletions persistence-modules/spring-boot-persistence-5/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
## Relevant Articles

35 changes: 35 additions & 0 deletions persistence-modules/spring-boot-persistence-5/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung.boot.persistence</groupId>
<artifactId>spring-boot-persistence-5</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-persistence-5</name>

<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-boot-3</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent-boot-3</relativePath>
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>


</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.baeldung.h2;

import jakarta.annotation.PostConstruct;
import org.h2.tools.RunScript;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.io.ClassPathResource;

import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

@SpringBootApplication
public class H2Application {
public static final Logger log = LoggerFactory.getLogger(H2Application.class);

@Value("${spring.datasource.url}")
private String url;
@Value("${spring.datasource.username}")
private String user;
@Value("${spring.datasource.password}")
private String password;

public static void main(String[] args) {
SpringApplication.run(H2Application.class, args);
}


@PostConstruct
public void init() throws SQLException, IOException {
Connection connection = DriverManager.getConnection(url, user, password);
ResultSet rs = RunScript.execute(connection, new FileReader(new ClassPathResource("db/script.sql").getFile()));
log.info("Reading Data from the employee table");
while (rs.next()) {
log.info("ID: {}, Name: {}", rs.getInt("id"), rs.getString("name"));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto=none
spring.sql.init.data-locations=classpath:db/data.sql
spring.sql.init.schema-locations=classpath:db/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
INSERT INTO employee (name) VALUES ('John');
INSERT INTO employee (name) VALUES ('Jane');
UPDATE EMPLOYEE SET NAME = 'Jane Doe' WHERE ID = 2;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CREATE TABLE IF NOT EXISTS employee (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CREATE TABLE IF NOT EXISTS employee (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL
);
INSERT INTO employee (name) VALUES ('John');
INSERT INTO employee (name) VALUES ('Jane');
UPDATE EMPLOYEE SET NAME = 'Jane Doe' WHERE ID = 2;
UPDATE employee SET NAME = 'John Doe' WHERE ID = 1;
SELECT * FROM employee;
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.baeldung.h2;

import org.h2.tools.RunScript;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.ClassPathResource;

import java.io.FileReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;

@SpringBootTest
class H2ApplicationIntegrationTest {


@Test
void givenApplication_whenBootstrapped_thenDataAvailable() throws Exception {
Connection connection = DriverManager.getConnection("jdbc:h2:mem:testdb", "sa", "password");
ResultSet rs = RunScript.execute(connection, new FileReader(new ClassPathResource("db/script.sql").getFile()));
Assertions.assertTrue(rs.next());
Assertions.assertEquals(1, rs.getInt("id"));
Assertions.assertEquals("John Doe", rs.getString("name"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto=none
spring.sql.init.data-locations=classpath:db/data.sql
spring.sql.init.schema-locations=classpath:db/schema.sql

0 comments on commit bb4a527

Please sign in to comment.