Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/main/java/kr/co/mcmp/config/DatabaseInitializer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package kr.co.mcmp.config;

import java.nio.charset.StandardCharsets;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StreamUtils;

@Component
public class DatabaseInitializer implements CommandLineRunner{

@Autowired
private JdbcTemplate jdbcTemplate;

@Autowired
private ResourceLoader resourceLoader;

@Override
public void run(String... args) throws Exception {
if (isDatabaseEmpty()) {
Resource resource = resourceLoader.getResource("classpath:import.sql");
String sql = StreamUtils.copyToString(resource.getInputStream(), StandardCharsets.UTF_8);
jdbcTemplate.execute(sql);
}
}

private boolean isDatabaseEmpty() {
Long count = jdbcTemplate.queryForObject("SELECT COUNT(*) FROM workflow", Long.class);
return count != null && count == 0;
}
}
4 changes: 2 additions & 2 deletions src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ spring:
name: workflow-manager
#h2 db 설정
datasource:
url: jdbc:h2:file:./document/workflow # 프로젝트 루트/db/ 안에 h2.db파일(test.mv.db) 생성
url: jdbc:h2:file:./document/test/workflow # 프로젝트 루트/db/ 안에 h2.db파일(test.mv.db) 생성
driver-class-name: org.h2.Driver
username: ${DB_ID:workflow}
password: ${DB_PW:workflow!23}
Expand Down Expand Up @@ -38,7 +38,7 @@ spring:
sql:
init:
data-locations: classpath:import.sql
mode: ${SQL_DATA_INIT:always} # ${SQL_DATA_INIT:never}
mode: always # ${SQL_DATA_INIT:never}
platform: h2
#schema-locations: classpath:ddl.sql

Expand Down