|
| 1 | +package com.dbaagent.service.migration; |
| 2 | + |
| 3 | +import com.dbaagent.dto.MigrationRiskReport; |
| 4 | +import com.dbaagent.dto.TableFacts; |
| 5 | +import com.dbaagent.model.ConnectionRequest; |
| 6 | +import com.dbaagent.provider.DatabaseProviderRegistry; |
| 7 | +import com.dbaagent.service.ConnectionService; |
| 8 | +import com.dbaagent.service.CredentialService; |
| 9 | +import com.dbaagent.service.security.AccessControlService; |
| 10 | +import lombok.RequiredArgsConstructor; |
| 11 | +import lombok.extern.slf4j.Slf4j; |
| 12 | +import org.springframework.stereotype.Service; |
| 13 | + |
| 14 | +import java.util.List; |
| 15 | + |
| 16 | +@Slf4j |
| 17 | +@Service |
| 18 | +@RequiredArgsConstructor |
| 19 | +public class MigrationRiskService { |
| 20 | + |
| 21 | + private final DdlStatementParser parser; |
| 22 | + private final DatabaseProviderRegistry registry; |
| 23 | + private final CredentialService credentialService; |
| 24 | + private final ConnectionService connectionService; |
| 25 | + private final AccessControlService accessControlService; |
| 26 | + |
| 27 | + public MigrationRiskReport analyze(String connectionId, String sql) { |
| 28 | + accessControlService.assertCanReadConnectionContent(connectionId); |
| 29 | + |
| 30 | + var parsed = parser.parse(sql); |
| 31 | + if (parsed.isEmpty()) { |
| 32 | + return failClosed("DeepSQL could not parse this statement, so it cannot verify what " |
| 33 | + + "it will do. Review it by hand before running it."); |
| 34 | + } |
| 35 | + var facts = parsed.get(); |
| 36 | + |
| 37 | + var request = credentialService.getDecryptedConnection(connectionId); |
| 38 | + var dialect = registry.getDialect(request.getDbType()); |
| 39 | + var provider = dialect.migrationRisk(); |
| 40 | + |
| 41 | + TableFacts table = tableFacts(connectionId, request, facts.table()); |
| 42 | + return provider.classify(facts, table); |
| 43 | + } |
| 44 | + |
| 45 | + private TableFacts tableFacts(String connectionId, ConnectionRequest request, String table) { |
| 46 | + try { |
| 47 | + var jdbc = connectionService.getJdbcTemplate(connectionId, request); |
| 48 | + var row = jdbc.queryForMap(""" |
| 49 | + SELECT COALESCE(c.reltuples, 0)::bigint AS rows, |
| 50 | + COALESCE(pg_total_relation_size(c.oid), 0)::bigint AS bytes |
| 51 | + FROM pg_class c WHERE c.oid = ?::regclass |
| 52 | + """, table); |
| 53 | + long rows = ((Number) row.get("rows")).longValue(); |
| 54 | + long bytes = ((Number) row.get("bytes")).longValue(); |
| 55 | + return new TableFacts(Math.max(rows, 0), bytes, rows == 0); |
| 56 | + } catch (Exception e) { |
| 57 | + // Unknown size is not a reason to claim safety — assume large. |
| 58 | + log.warn("Could not read table facts for {}: {}", table, e.getMessage()); |
| 59 | + return new TableFacts(Long.MAX_VALUE, 0L, false); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + private MigrationRiskReport failClosed(String reason) { |
| 64 | + return new MigrationRiskReport("unknown", "UNKNOWN", false, true, "UNKNOWN", null, |
| 65 | + List.of(), false, 0L, 0L, "unknown", reason, null, null, "unverified"); |
| 66 | + } |
| 67 | +} |
0 commit comments