From 46b437d5c9227234d51493354a40831ea104ab1b Mon Sep 17 00:00:00 2001 From: XuqmGroup Date: Fri, 26 Jun 2026 11:24:36 +0800 Subject: [PATCH] =?UTF-8?q?fix(push):=20=E5=88=A0=E9=99=A4=20push=20?= =?UTF-8?q?=E8=A1=A8=E4=B8=AD=E9=81=97=E7=95=99=E7=9A=84=20app=5Fid=20?= =?UTF-8?q?=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit push_device_token 和 push_device_login_log 中存在 NOT NULL 的 app_id 列, 为历史遗留字段(系统已统一使用 appKey),导致 INSERT 时报错 500。 通过 PushSchemaMigrationService 新增迁移删除该字段及对应的旧索引/约束, 并重建 idx_push_device_log_user_time 索引到正确的 app_key 列。 Co-Authored-By: Claude Sonnet 4.6 --- .../service/PushSchemaMigrationService.java | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/push-service/src/main/java/com/xuqm/push/service/PushSchemaMigrationService.java b/push-service/src/main/java/com/xuqm/push/service/PushSchemaMigrationService.java index c5aec03..561c857 100644 --- a/push-service/src/main/java/com/xuqm/push/service/PushSchemaMigrationService.java +++ b/push-service/src/main/java/com/xuqm/push/service/PushSchemaMigrationService.java @@ -36,6 +36,7 @@ public class PushSchemaMigrationService { } migrate_v20260527_create_push_operation_log(emit); + migrate_v20260626_remove_app_id(emit); emit.accept("数据库迁移检查完成"); } @@ -79,6 +80,23 @@ public class PushSchemaMigrationService { } } + private boolean columnExists(Connection conn, String table, String column) throws SQLException { + try (ResultSet rs = conn.getMetaData().getColumns(null, null, table, column)) { + return rs.next(); + } + } + + private boolean indexExists(Connection conn, String table, String indexName) throws SQLException { + try (ResultSet rs = conn.getMetaData().getIndexInfo(null, null, table, false, false)) { + while (rs.next()) { + if (indexName.equalsIgnoreCase(rs.getString("INDEX_NAME"))) { + return true; + } + } + } + return false; + } + // ── 各版本迁移 ────────────────────────────────────────────────────────────── private void migrate_v20260527_create_push_operation_log(Consumer emit) { @@ -110,4 +128,37 @@ public class PushSchemaMigrationService { log.error("migration {} failed", id, e); } } + + private void migrate_v20260626_remove_app_id(Consumer emit) { + final String id = "v20260626_remove_app_id"; + if (migrationApplied(id)) { + emit.accept("[已应用] " + id); + return; + } + try (Connection conn = dataSource.getConnection(); + Statement stmt = conn.createStatement()) { + // push_device_token: remove legacy app_id unique key and column + if (columnExists(conn, "push_device_token", "app_id")) { + if (indexExists(conn, "push_device_token", "UKj2xmtmdtxgsohtp9c6exx6vl")) { + stmt.execute("ALTER TABLE push_device_token DROP KEY UKj2xmtmdtxgsohtp9c6exx6vl"); + } + stmt.execute("ALTER TABLE push_device_token DROP COLUMN app_id"); + } + // push_device_login_log: rebuild index on app_key, remove app_id column + if (columnExists(conn, "push_device_login_log", "app_id")) { + if (indexExists(conn, "push_device_login_log", "idx_push_device_log_user_time")) { + stmt.execute("ALTER TABLE push_device_login_log DROP INDEX idx_push_device_log_user_time"); + } + stmt.execute("ALTER TABLE push_device_login_log DROP COLUMN app_id"); + if (!indexExists(conn, "push_device_login_log", "idx_push_device_log_user_time")) { + stmt.execute("CREATE INDEX idx_push_device_log_user_time ON push_device_login_log (app_key, user_id, created_at)"); + } + } + emit.accept("[已迁移] " + id + ": 删除 push 表中遗留 app_id 字段"); + recordMigration(id, "删除 push_device_token 和 push_device_login_log 中遗留的 app_id 字段"); + } catch (Exception e) { + emit.accept("[错误] " + id + ": " + e.getMessage()); + log.error("migration {} failed", id, e); + } + } }