44import com .fasterxml .jackson .databind .ObjectMapper ;
55import org .jooq .DSLContext ;
66import org .jooq .impl .DSL ;
7+ import org .jooq .impl .SQLDataType ;
78import org .slf4j .Logger ;
89import org .slf4j .LoggerFactory ;
10+ import org .springframework .beans .factory .annotation .Value ;
911import org .springframework .stereotype .Service ;
1012import org .springframework .transaction .annotation .Transactional ;
1113import org .springframework .web .server .ResponseStatusException ;
1214
15+ import jakarta .annotation .PostConstruct ;
1316import javax .sql .DataSource ;
1417import java .sql .Connection ;
1518import 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