Skip to content

Commit 7059baf

Browse files
committed
Enhance PipelineService to create schema and table if not exists
1 parent bef21b8 commit 7059baf

1 file changed

Lines changed: 19 additions & 11 deletions

File tree

src/main/java/org/texttechnologylab/udav/api/service/PipelineService.java

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@
44
import com.fasterxml.jackson.databind.ObjectMapper;
55
import org.jooq.DSLContext;
66
import org.jooq.impl.DSL;
7+
import org.jooq.impl.SQLDataType;
78
import org.slf4j.Logger;
89
import org.slf4j.LoggerFactory;
10+
import org.springframework.beans.factory.annotation.Value;
911
import org.springframework.stereotype.Service;
1012
import org.springframework.transaction.annotation.Transactional;
1113
import org.springframework.web.server.ResponseStatusException;
1214

15+
import jakarta.annotation.PostConstruct;
1316
import javax.sql.DataSource;
1417
import java.sql.Connection;
1518
import java.sql.SQLException;
@@ -28,6 +31,9 @@ public class PipelineService {
2831
private final DataSource dataSource;
2932
private final ObjectMapper objectMapper;
3033

34+
@Value("${app.db.schema:public}")
35+
private String schema;
36+
3137
Logger LOGGER = LoggerFactory.getLogger(PipelineService.class);
3238

3339
public PipelineService(SourceBuildService sourceBuildService, DataSource dataSource, ObjectMapper objectMapper) {
@@ -36,17 +42,19 @@ public PipelineService(SourceBuildService sourceBuildService, DataSource dataSou
3642
this.objectMapper = objectMapper;
3743
}
3844

39-
// @PostConstruct
40-
// void ensureTable() throws Exception {
41-
// try (Connection c = dataSource.getConnection()) {
42-
// DSLContext dsl = DSL.using(c);
43-
// dsl.createTableIfNotExists(TABLE)
44-
// .column(COL_NAME, SQLDataType.VARCHAR(255).nullable(false))
45-
// .column(COL_JSON, SQLDataType.CLOB.nullable(false)) // switch to JSONB if on Postgres
46-
// .constraints(DSL.constraint("PK_" + TABLE).primaryKey(COL_NAME))
47-
// .execute();
48-
// }
49-
// }
45+
@PostConstruct
46+
void ensureTable() throws Exception {
47+
try (Connection c = dataSource.getConnection()) {
48+
DSLContext dsl = DSL.using(c);
49+
dsl.createSchemaIfNotExists(DSL.name(schema)).execute();
50+
dsl.createTableIfNotExists(DSL.name(schema, TABLE))
51+
.column(DSL.name(COL_ID), SQLDataType.VARCHAR(255).nullable(false))
52+
.column(DSL.name(COL_NAME), SQLDataType.VARCHAR(255).nullable(false))
53+
.column(DSL.name(COL_JSON), SQLDataType.CLOB.nullable(false))
54+
.constraints(DSL.constraint("PK_" + TABLE).primaryKey(DSL.name(COL_ID)))
55+
.execute();
56+
}
57+
}
5058

5159
@Transactional(readOnly = true)
5260
public List<String> listIds(int page, int size, String q) throws Exception {

0 commit comments

Comments
 (0)