From 9cf9362229e83f1a8523bec8261fd5a1bb88d2ba Mon Sep 17 00:00:00 2001 From: Jose Ignacio Recuerda Cambil Date: Wed, 30 Jan 2019 14:23:57 +0100 Subject: [PATCH] TSK-778 - Check if exist the history_event's table before running the script --- .../sampledata/SampleDataGenerator.java | 27 +++++++++++++++++-- .../sample-data/check-history-event-exist.sql | 1 + 2 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 rest/taskana-rest-spring-example/src/main/resources/sql/sample-data/check-history-event-exist.sql diff --git a/rest/taskana-rest-spring-example/src/main/java/pro/taskana/sampledata/SampleDataGenerator.java b/rest/taskana-rest-spring-example/src/main/java/pro/taskana/sampledata/SampleDataGenerator.java index 709306e18..46a973934 100644 --- a/rest/taskana-rest-spring-example/src/main/java/pro/taskana/sampledata/SampleDataGenerator.java +++ b/rest/taskana-rest-spring-example/src/main/java/pro/taskana/sampledata/SampleDataGenerator.java @@ -11,6 +11,8 @@ import java.nio.charset.StandardCharsets; import java.sql.SQLException; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; +import java.util.ArrayList; +import java.util.Arrays; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; @@ -41,6 +43,7 @@ public class SampleDataGenerator { private static final String OBJECT_REFERENCE = SQL + TEST_DATA + "/object-reference.sql"; private static final String ATTACHMENT = SQL + TEST_DATA + "/attachment.sql"; private static final String HISTORY_EVENT = SQL + TEST_DATA + "/history-event.sql"; + private static final String CHECK_HISTORY_EVENT_EXIST = SQL + TEST_DATA + "/check-history-event-exist.sql"; private static final String RELATIVE_DATE_REGEX = "RELATIVE_DATE\\((-?\\d+)\\)"; private static final Pattern RELATIVE_DATE_PATTERN = Pattern.compile(RELATIVE_DATE_REGEX); @@ -113,9 +116,10 @@ public class SampleDataGenerator { runner.setLogWriter(logWriter); runner.setErrorLogWriter(errorLogWriter); + String[] script = this.getScriptList(); + LocalDateTime now = LocalDateTime.now(); - Stream.of(WORKBASKET, DISTRIBUTION_TARGETS, CLASSIFICATION, TASK, ATTACHMENT, WORKBASKET_ACCESS_LIST, - OBJECT_REFERENCE, HISTORY_EVENT) + Stream.of(script) .map(this.getClass()::getResourceAsStream) .map(s -> SampleDataGenerator.parseAndReplace(now, s)) .map(StringReader::new) @@ -136,4 +140,23 @@ public class SampleDataGenerator { : "SET SCHEMA " + schemaName + ";"); } + /** + * Create a array with the necessary script. + * @return a array with the corresponding scripts files + */ + private String[] getScriptList() { + String[] script = {WORKBASKET, DISTRIBUTION_TARGETS, CLASSIFICATION, TASK, ATTACHMENT, WORKBASKET_ACCESS_LIST, + OBJECT_REFERENCE}; + ArrayList scriptsList = new ArrayList<>(Arrays.asList(script)); + + try { + runner.runScript(new BufferedReader( + new InputStreamReader(this.getClass().getResourceAsStream(CHECK_HISTORY_EVENT_EXIST), + StandardCharsets.UTF_8))); + scriptsList.add(HISTORY_EVENT); + } catch (Exception e) { + LOGGER.info("The HISTORY_EVENTS table is not created"); + } + return scriptsList.toArray(new String[0]); + } } diff --git a/rest/taskana-rest-spring-example/src/main/resources/sql/sample-data/check-history-event-exist.sql b/rest/taskana-rest-spring-example/src/main/resources/sql/sample-data/check-history-event-exist.sql new file mode 100644 index 000000000..697c955b2 --- /dev/null +++ b/rest/taskana-rest-spring-example/src/main/resources/sql/sample-data/check-history-event-exist.sql @@ -0,0 +1 @@ +SELECT 1 FROM HISTORY_EVENTS LIMIT 1;