summaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
Diffstat (limited to 'src/main')
-rw-r--r--src/main/java/dev/dnpm/oshelper/migration/DatabaseMigrationService.java41
-rw-r--r--src/main/java/dev/dnpm/oshelper/migration/MigrationUtils.java14
-rw-r--r--src/main/resources/dev/dnpm/oshelper/migration/README.md18
-rw-r--r--src/main/resources/dev/dnpm/oshelper/migration/updatednpm_001.sql4
4 files changed, 77 insertions, 0 deletions
diff --git a/src/main/java/dev/dnpm/oshelper/migration/DatabaseMigrationService.java b/src/main/java/dev/dnpm/oshelper/migration/DatabaseMigrationService.java
index e052286..985ccf4 100644
--- a/src/main/java/dev/dnpm/oshelper/migration/DatabaseMigrationService.java
+++ b/src/main/java/dev/dnpm/oshelper/migration/DatabaseMigrationService.java
@@ -19,7 +19,10 @@
package dev.dnpm.oshelper.migration;
+import java.io.IOException;
import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -53,6 +56,11 @@ public class DatabaseMigrationService {
log.info("Starting database migration");
try {
for (var migration : this.getNewMigrationIds()) {
+ if (!requiredFormRevisionAvailable(migration)) {
+ log.info("Skipping database migration due to pre-condition: '{}'", migration);
+ continue;
+ }
+
log.info("Apply database migration: '{}'", migration);
try {
jdbcTemplate.execute(MigrationUtils.getSqlQuery(resourcePatternResolver, migration));
@@ -74,6 +82,39 @@ public class DatabaseMigrationService {
}
}
+ private boolean requiredFormRevisionAvailable(String migration) throws IOException {
+ final var sqlQuery = MigrationUtils.getSqlQuery(resourcePatternResolver, migration);
+
+ String formName = null;
+ Pattern formNamePattern = Pattern.compile("(?m)^-- form: (.+)$");
+ Matcher formNameMatcher = formNamePattern.matcher(sqlQuery);
+ if (formNameMatcher.find()) {
+ formName = formNameMatcher.group(1);
+ }
+
+ String minRev = "0";
+ Pattern minRevPattern = Pattern.compile("(?m)^-- min-rev: (\\d+)$");
+ Matcher minRevMatcher = minRevPattern.matcher(sqlQuery);
+ if (minRevMatcher.find()) {
+ minRev = minRevMatcher.group(1);
+ }
+
+ String maxRev = "99999999999999";
+ Pattern maxRevPattern = Pattern.compile("(?m)^-- max-rev: (\\d+)$");
+ Matcher maxRevMatcher = maxRevPattern.matcher(sqlQuery);
+ if (maxRevMatcher.find()) {
+ maxRev = maxRevMatcher.group(1);
+ }
+
+ if (formName == null) {
+ // No spezial form revision required
+ return true;
+ }
+
+ return MigrationUtils.requiredFormRevisionAvailable(
+ this.jdbcTemplate, formName, minRev, maxRev);
+ }
+
List<String> getNewMigrationIds() throws Exception {
var existingMigrations = MigrationUtils.getExecutedDatabaseMigrations(this.jdbcTemplate);
return MigrationUtils.getDatabaseMigrationScripts(this.resourcePatternResolver).stream()
diff --git a/src/main/java/dev/dnpm/oshelper/migration/MigrationUtils.java b/src/main/java/dev/dnpm/oshelper/migration/MigrationUtils.java
index bde2612..fd17f22 100644
--- a/src/main/java/dev/dnpm/oshelper/migration/MigrationUtils.java
+++ b/src/main/java/dev/dnpm/oshelper/migration/MigrationUtils.java
@@ -24,6 +24,7 @@ import java.util.List;
import java.util.stream.Collectors;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.ResourcePatternResolver;
+import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
/**
@@ -39,6 +40,19 @@ class MigrationUtils {
(rs, rowNum) -> rs.getString("update_script"));
}
+ static boolean requiredFormRevisionAvailable(
+ JdbcTemplate jdbcTemplate, String formName, String minRev, String maxRev) {
+ try {
+ return 1
+ == jdbcTemplate.queryForObject(
+ "SELECT COUNT(*) FROM data_form WHERE name = ? AND revision >= ? AND revision <= ?",
+ new Object[] {formName, minRev, maxRev},
+ Integer.class);
+ } catch (EmptyResultDataAccessException e) {
+ return false;
+ }
+ }
+
static List<String> getDatabaseMigrationScripts(ResourcePatternResolver resourcePatternResolver)
throws IOException {
var resources =
diff --git a/src/main/resources/dev/dnpm/oshelper/migration/README.md b/src/main/resources/dev/dnpm/oshelper/migration/README.md
new file mode 100644
index 0000000..14a530b
--- /dev/null
+++ b/src/main/resources/dev/dnpm/oshelper/migration/README.md
@@ -0,0 +1,18 @@
+# Format der Migrationsdateien
+
+Die Migrationsdateien sollen fortlaufend aufsteigend nummeriert sein, mit `.sql` enden.
+
+Zudem sollen sie immer Angaben zum betreffenden Formular und Revision enthalten:
+
+```sql
+-- form: DNPM Beispiel
+-- min-rev: 1
+-- max-rev: 2
+
+ALTER TABLE dk_dnpm_beispiel
+ MODIFY COLUMN langer_text text;
+```
+
+Ist keine Angabe zum Formularnamen angegeben, wird die Migration immer ausgeführt,
+anderenfalls nur für eine entsprechende Revision des angegebenen Formulars.
+Wenn nicht angegeben, ist die minimal erforderliche Revision ist standardmäßig 0, die maximale 99999999999999. \ No newline at end of file
diff --git a/src/main/resources/dev/dnpm/oshelper/migration/updatednpm_001.sql b/src/main/resources/dev/dnpm/oshelper/migration/updatednpm_001.sql
index 44c6edc..6c6d80b 100644
--- a/src/main/resources/dev/dnpm/oshelper/migration/updatednpm_001.sql
+++ b/src/main/resources/dev/dnpm/oshelper/migration/updatednpm_001.sql
@@ -1 +1,5 @@
+-- form: DNPM UF Einzelempfehlung
+-- min-rev: 273
+-- max-rev: 273
+
ALTER TABLE dk_dnpm_uf_einzelempfehlung MODIFY COLUMN studie VARCHAR(255); \ No newline at end of file