From f99a985f7531f7b9135fa104b96cedc0663c6cb5 Mon Sep 17 00:00:00 2001 From: pray4skylark Date: Fri, 12 Jun 2026 10:57:26 +0900 Subject: [PATCH] fix: V24 encrypt existing plain text phone numbers --- .../V24__encrypt_existing_phones.java | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 backend/src/main/java/com/srsystem/migration/V24__encrypt_existing_phones.java diff --git a/backend/src/main/java/com/srsystem/migration/V24__encrypt_existing_phones.java b/backend/src/main/java/com/srsystem/migration/V24__encrypt_existing_phones.java new file mode 100644 index 0000000..eb72aee --- /dev/null +++ b/backend/src/main/java/com/srsystem/migration/V24__encrypt_existing_phones.java @@ -0,0 +1,55 @@ +package com.srsystem.migration; + +import com.srsystem.util.PhoneEncryptionConverter; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.flywaydb.core.api.migration.BaseJavaMigration; +import org.flywaydb.core.api.migration.Context; +import org.springframework.stereotype.Component; + +import java.sql.PreparedStatement; +import java.sql.ResultSet; + +@Slf4j +@Component +@RequiredArgsConstructor +public class V24__encrypt_existing_phones extends BaseJavaMigration { + + private final PhoneEncryptionConverter converter; + + @Override + public void migrate(Context context) throws Exception { + var conn = context.getConnection(); + int encrypted = 0; + int skipped = 0; + + ResultSet rs = conn.createStatement().executeQuery( + "SELECT id, phone FROM tb_employee WHERE phone IS NOT NULL" + ); + + PreparedStatement update = conn.prepareStatement( + "UPDATE tb_employee SET phone = ? WHERE id = ?" + ); + + while (rs.next()) { + Long id = rs.getLong("id"); + String phone = rs.getString("phone"); + + try { + converter.convertToEntityAttribute(phone); + skipped++; + } catch (Exception e) { + String encryptedPhone = converter.convertToDatabaseColumn(phone); + update.setString(1, encryptedPhone); + update.setLong(2, id); + update.executeUpdate(); + encrypted++; + } + } + + rs.close(); + update.close(); + + log.info("V24 migration complete: {} encrypted, {} already encrypted", encrypted, skipped); + } +}