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); + } + } }