/
Sturon
/
Diplom
Обзор
Документация
Войти
/
Sturon
/
Diplom
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
backend/src/main/java/ru/diplom/vision/service/SchemaCompatibilityService.java
72 строки
3 KB
Sturon
Initial
03 июн 2026, 15:01
03 июн 2026, 15:01
17b44a2
Код
Авторство
О чём код?
package ru.diplom.vision.service; import java.util.List; import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Component; @Component public class SchemaCompatibilityService implements ApplicationRunner { private final JdbcTemplate jdbcTemplate; public SchemaCompatibilityService(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } @Override public void run(ApplicationArguments args) { dropLegacyInspectionStatusChecks(); normalizeAnalysisEnabledColumn(); } private void dropLegacyInspectionStatusChecks() { dropConstraintIfExists("inspection_item_status_check"); List<String> constraintNames; try { constraintNames = jdbcTemplate.queryForList(""" select tc.constraint_name from information_schema.table_constraints tc join information_schema.check_constraints cc on tc.constraint_catalog = cc.constraint_catalog and tc.constraint_schema = cc.constraint_schema and tc.constraint_name = cc.constraint_name where lower(tc.table_name) = 'inspection_item' and lower(cc.check_clause) like '%status%' and lower(cc.check_clause) not like '%pending%' """, String.class); } catch (Exception ignored) { return; } for (String constraintName : constraintNames) { dropConstraintIfExists(constraintName); } } private void dropConstraintIfExists(String constraintName) { try { jdbcTemplate.execute("alter table inspection_item drop constraint if exists " + quoteIdentifier(constraintName)); } catch (Exception ignored) { try { jdbcTemplate.execute("alter table inspection_item drop constraint " + quoteIdentifier(constraintName)); } catch (Exception ignoredAgain) { // The migration is best-effort because fresh schemas already have the correct enum values. } } } private void normalizeAnalysisEnabledColumn() { try { jdbcTemplate.update("update inspection_profile set analysis_enabled = false where analysis_enabled is null"); } catch (Exception ignored) { // Fresh schemas or older local databases may not have the column until Hibernate updates the model. } } private String quoteIdentifier(String value) { return "\"" + value.replace("\"", "\"\"") + "\""; } }