fix: keep ambiguous legacy phones unbound
This commit is contained in:
@@ -20,15 +20,18 @@ DROP PROCEDURE IF EXISTS auth_backfill_login_phone;
|
||||
DELIMITER $$
|
||||
CREATE PROCEDURE auth_backfill_login_phone()
|
||||
BEGIN
|
||||
IF EXISTS (
|
||||
SELECT user.phone
|
||||
FROM baiyue_iam.iam_user user
|
||||
WHERE user.deleted = 0 AND user.phone REGEXP '^1[3-9][0-9]{9}$'
|
||||
GROUP BY user.phone HAVING COUNT(DISTINCT user.auth_user_id) > 1) THEN
|
||||
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'IAM 存在重复手机号,禁止建立 Auth 唯一登录索引';
|
||||
END IF;
|
||||
UPDATE auth_account
|
||||
SET phone = NULL
|
||||
WHERE phone IS NOT NULL AND phone NOT REGEXP '^1[3-9][0-9]{9}$';
|
||||
UPDATE auth_account account
|
||||
JOIN baiyue_iam.iam_user user ON user.auth_user_id = account.id AND user.deleted = 0
|
||||
JOIN (
|
||||
SELECT phone
|
||||
FROM baiyue_iam.iam_user
|
||||
WHERE deleted = 0 AND phone REGEXP '^1[3-9][0-9]{9}$'
|
||||
GROUP BY phone
|
||||
HAVING COUNT(DISTINCT auth_user_id) = 1
|
||||
) unique_phone ON unique_phone.phone = user.phone
|
||||
SET account.phone = user.phone
|
||||
WHERE account.phone IS NULL AND user.phone REGEXP '^1[3-9][0-9]{9}$';
|
||||
IF NOT EXISTS (
|
||||
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package com.baiyue.auth.repository;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class AuthLoginIdentifiersSqlContractTest {
|
||||
@Test
|
||||
void backfillsOnlyUniquelyOwnedPhonesAndKeepsDuplicateLegacyPhonesUnbound() throws Exception {
|
||||
assertSafeBackfill(new ClassPathResource("db/auth-login-identifiers.sql")
|
||||
.getContentAsString(StandardCharsets.UTF_8));
|
||||
assertSafeBackfill(Files.readString(unifiedSql(), StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
private static void assertSafeBackfill(String sql) {
|
||||
assertThat(sql)
|
||||
.contains("phone NOT REGEXP '^1[3-9][0-9]{9}$'")
|
||||
.contains("HAVING COUNT(DISTINCT auth_user_id) = 1")
|
||||
.contains("ADD UNIQUE KEY uk_auth_account_phone (phone)")
|
||||
.doesNotContain("IAM 存在重复手机号,禁止建立 Auth 唯一登录索引");
|
||||
}
|
||||
|
||||
private static Path unifiedSql() {
|
||||
Path path = Path.of("deploy", "sql", "头程数据库统一增量-MySQL8.sql");
|
||||
return Files.exists(path) ? path : Path.of("..", "deploy", "sql", "头程数据库统一增量-MySQL8.sql");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user