diff options
| author | Paul-Christian Volkmer | 2026-11-11 15:13:46 +0100 |
|---|---|---|
| committer | Paul-Christian Volkmer | 2026-11-13 09:23:31 +0100 |
| commit | 86b4026bcfaed73eda1a435f76c23e0c23e95307 (patch) | |
| tree | 20b041f0111e82b16fc0b4a07f32e9c2a684e232 /src/main/java/dev | |
| parent | 7e2bc062a4331b6df2cf50b177d943e76e72ae54 (diff) | |
feat: initial support for data(base) migration infrastructure
Diffstat (limited to 'src/main/java/dev')
3 files changed, 251 insertions, 0 deletions
diff --git a/src/main/java/dev/dnpm/oshelper/analyzer/MigrationAnalyzer.java b/src/main/java/dev/dnpm/oshelper/analyzer/MigrationAnalyzer.java new file mode 100644 index 0000000..3299a58 --- /dev/null +++ b/src/main/java/dev/dnpm/oshelper/analyzer/MigrationAnalyzer.java @@ -0,0 +1,106 @@ +/* + * This file is part of DNPMHelper + * + * Copyright (C) 2023-2026 the original author or authors. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +package dev.dnpm.oshelper.analyzer; + +import de.itc.onkostar.api.Disease; +import de.itc.onkostar.api.IOnkostarApi; +import de.itc.onkostar.api.Procedure; +import de.itc.onkostar.api.analysis.AnalyseTriggerEvent; +import de.itc.onkostar.api.analysis.AnalyzerRequirement; +import dev.dnpm.oshelper.migration.DatabaseMigrationService; +import java.util.Set; +import javax.annotation.PostConstruct; +import javax.sql.DataSource; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.core.io.support.ResourcePatternResolver; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.stereotype.Component; + +/** + * Diese Klasse implementiert einen Analyzer zur Durchführung von Migrationen des Plugins + * + * @since 2.3.0 + */ +@Component +public class MigrationAnalyzer extends Analyzer { + + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + + private final IOnkostarApi onkostarApi; + + private final DataSource dataSource; + + private final DatabaseMigrationService databaseMigrationService; + + public MigrationAnalyzer( + final IOnkostarApi onkostarApi, + final DataSource dataSource, + final ResourcePatternResolver resourcePatternResolver) { + logger.info("Ctor migration"); + this.onkostarApi = onkostarApi; + this.dataSource = dataSource; + this.databaseMigrationService = + new DatabaseMigrationService(new JdbcTemplate(dataSource), resourcePatternResolver); + } + + @Override + public String getDescription() { + return "Migration des Plugins"; + } + + /** + * @deprecated + */ + @Override + public boolean isRelevantForDeletedProcedure() { + return false; + } + + @Override + public boolean isRelevantForAnalyzer(Procedure procedure, Disease disease) { + return null != procedure && procedure.getFormName().startsWith("DNPM"); + } + + @Override + public boolean isSynchronous() { + return true; + } + + @Override + public AnalyzerRequirement getRequirement() { + return AnalyzerRequirement.PROCEDURE; + } + + @Override + public Set<AnalyseTriggerEvent> getTriggerEvents() { + return Set.of(AnalyseTriggerEvent.REORG); + } + + @Override + public void analyze(Procedure procedure, Disease disease) { + this.init(); + } + + @PostConstruct + public void init() { + this.databaseMigrationService.migrate(); + } +} diff --git a/src/main/java/dev/dnpm/oshelper/migration/DatabaseMigrationService.java b/src/main/java/dev/dnpm/oshelper/migration/DatabaseMigrationService.java new file mode 100644 index 0000000..e052286 --- /dev/null +++ b/src/main/java/dev/dnpm/oshelper/migration/DatabaseMigrationService.java @@ -0,0 +1,83 @@ +/* + * This file is part of DNPMHelper + * + * Copyright (C) 2023-2026 the original author or authors. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +package dev.dnpm.oshelper.migration; + +import java.util.List; +import java.util.stream.Collectors; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.core.io.support.ResourcePatternResolver; +import org.springframework.dao.DataAccessException; +import org.springframework.jdbc.core.JdbcTemplate; + +/** + * Diese Klasse implementiert die Durchführung einer Datenbankmigration + * + * @since 2.3.0 + */ +public class DatabaseMigrationService { + + private final Logger log = LoggerFactory.getLogger(DatabaseMigrationService.class); + + private final JdbcTemplate jdbcTemplate; + private final ResourcePatternResolver resourcePatternResolver; + + public DatabaseMigrationService( + final JdbcTemplate jdbcTemplate, final ResourcePatternResolver resourcePatternResolver) { + this.jdbcTemplate = jdbcTemplate; + this.resourcePatternResolver = resourcePatternResolver; + } + + /** + * Diese Methode führt eine Datenbankmigration durch. Es werden alle Migrationen durchgeführt, die + * noch nicht angewendet wurden. + */ + public void migrate() { + log.info("Starting database migration"); + try { + for (var migration : this.getNewMigrationIds()) { + log.info("Apply database migration: '{}'", migration); + try { + jdbcTemplate.execute(MigrationUtils.getSqlQuery(resourcePatternResolver, migration)); + } catch (DataAccessException e) { + jdbcTemplate.update( + "INSERT INTO update_db (date, update_script, failed, fail_description) VALUES (NOW(), ?, ?, ?)", + migration, + true, + e.getMessage().length() > 1000 ? e.getMessage().substring(0, 1000) : e.getMessage()); + continue; + } + jdbcTemplate.update( + "INSERT INTO update_db (date, update_script, failed) VALUES (NOW(), ?, ?)", + migration, + false); + } + } catch (Exception e) { + log.error("Error while migrating database", e); + } + } + + List<String> getNewMigrationIds() throws Exception { + var existingMigrations = MigrationUtils.getExecutedDatabaseMigrations(this.jdbcTemplate); + return MigrationUtils.getDatabaseMigrationScripts(this.resourcePatternResolver).stream() + .filter(migration -> !existingMigrations.contains(migration)) + .collect(Collectors.toList()); + } +} diff --git a/src/main/java/dev/dnpm/oshelper/migration/MigrationUtils.java b/src/main/java/dev/dnpm/oshelper/migration/MigrationUtils.java new file mode 100644 index 0000000..bde2612 --- /dev/null +++ b/src/main/java/dev/dnpm/oshelper/migration/MigrationUtils.java @@ -0,0 +1,62 @@ +/* + * This file is part of DNPMHelper + * + * Copyright (C) 2023-2026 the original author or authors. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +package dev.dnpm.oshelper.migration; + +import java.io.IOException; +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.jdbc.core.JdbcTemplate; + +/** + * In dieser Klasse werden Hilfsmethoden zur Durchführung von Migrationen des Plugins implementiert + * + * @since 2.3.0 + */ +class MigrationUtils { + + static List<String> getExecutedDatabaseMigrations(JdbcTemplate jdbcTemplate) { + return jdbcTemplate.query( + "SELECT id FROM update_db WHERE update_script LIKE 'updatednpm_%'", + (rs, rowNum) -> rs.getString("update_script")); + } + + static List<String> getDatabaseMigrationScripts(ResourcePatternResolver resourcePatternResolver) + throws IOException { + var resources = + List.of( + resourcePatternResolver.getResources("classpath:dev/dnpm/oshelper/migration/*.sql")); + return resources.stream() + .map(Resource::getFilename) + .filter(s -> s.startsWith("updatednpm_") && s.endsWith(".sql")) + .collect(Collectors.toList()); + } + + static String getSqlQuery(ResourcePatternResolver resourcePatternResolver, String id) + throws IOException { + final var sql = + resourcePatternResolver + .getResource(String.format("classpath:dev/dnpm/oshelper/migration/%s", id)) + .getInputStream() + .readAllBytes(); + return new String(sql); + } +} |
