From 286e6aad0189a006f0b95c7f53a74a24de61e91b Mon Sep 17 00:00:00 2001 From: hailin Date: Mon, 15 Dec 2025 21:53:31 -0800 Subject: [PATCH] =?UTF-8?q?fix(db):=20=E6=B7=BB=E5=8A=A0=E7=BC=BA=E5=A4=B1?= =?UTF-8?q?=E7=9A=84=E6=95=B0=E6=8D=AE=E5=BA=93=E8=BF=81=E7=A7=BB=E6=96=87?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 问题: - wallet-service schema 中有 version 字段,但迁移文件中缺失 - presence-service 缺少初始化迁移文件 修复: - wallet-service: 添加 20241216000000_add_version_column 迁移 - presence-service: 添加 20241204000000_init 初始化迁移(包含 version 字段) - presence-service: 删除无效的 20251215100000 迁移(依赖不存在的表) 验证所有服务 schema 与 migration 一致性: - identity-service: ✅ OK - wallet-service: ✅ 已修复 - blockchain-service: ✅ OK (无 version 字段) - planting-service: ✅ OK - reward-service: ✅ OK - referral-service: ✅ OK - leaderboard-service: ✅ OK - presence-service: ✅ 已修复 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- .../20241204000000_init/migration.sql | 52 +++++++++++++++++++ .../migration.sql | 9 ---- .../migration.sql | 2 + 3 files changed, 54 insertions(+), 9 deletions(-) create mode 100644 backend/services/presence-service/prisma/migrations/20241204000000_init/migration.sql delete mode 100644 backend/services/presence-service/prisma/migrations/20251215100000_change_user_id_to_string/migration.sql create mode 100644 backend/services/wallet-service/prisma/migrations/20241216000000_add_version_column/migration.sql diff --git a/backend/services/presence-service/prisma/migrations/20241204000000_init/migration.sql b/backend/services/presence-service/prisma/migrations/20241204000000_init/migration.sql new file mode 100644 index 00000000..c3ac9795 --- /dev/null +++ b/backend/services/presence-service/prisma/migrations/20241204000000_init/migration.sql @@ -0,0 +1,52 @@ +-- CreateTable +CREATE TABLE "analytics_event_log" ( + "id" BIGSERIAL NOT NULL, + "user_id" VARCHAR(20), + "install_id" VARCHAR(64) NOT NULL, + "event_name" VARCHAR(64) NOT NULL, + "event_time" TIMESTAMPTZ NOT NULL, + "properties" JSONB, + "created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + + CONSTRAINT "analytics_event_log_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "analytics_daily_active_users" ( + "day" DATE NOT NULL, + "dau_count" INTEGER NOT NULL, + "dau_by_province" JSONB, + "dau_by_city" JSONB, + "calculated_at" TIMESTAMPTZ NOT NULL, + "version" INTEGER NOT NULL DEFAULT 1, + + CONSTRAINT "analytics_daily_active_users_pkey" PRIMARY KEY ("day") +); + +-- CreateTable +CREATE TABLE "analytics_online_snapshots" ( + "id" BIGSERIAL NOT NULL, + "ts" TIMESTAMPTZ NOT NULL, + "online_count" INTEGER NOT NULL, + "window_seconds" INTEGER NOT NULL DEFAULT 180, + + CONSTRAINT "analytics_online_snapshots_pkey" PRIMARY KEY ("id") +); + +-- CreateIndex +CREATE INDEX "idx_event_log_event_time" ON "analytics_event_log"("event_time"); + +-- CreateIndex +CREATE INDEX "idx_event_log_event_name" ON "analytics_event_log"("event_name"); + +-- CreateIndex +CREATE INDEX "idx_event_log_event_name_time" ON "analytics_event_log"("event_name", "event_time"); + +-- CreateIndex +CREATE INDEX "idx_event_log_user_id" ON "analytics_event_log"("user_id"); + +-- CreateIndex +CREATE UNIQUE INDEX "analytics_online_snapshots_ts_key" ON "analytics_online_snapshots"("ts"); + +-- CreateIndex +CREATE INDEX "idx_online_snapshots_ts" ON "analytics_online_snapshots"("ts" DESC); diff --git a/backend/services/presence-service/prisma/migrations/20251215100000_change_user_id_to_string/migration.sql b/backend/services/presence-service/prisma/migrations/20251215100000_change_user_id_to_string/migration.sql deleted file mode 100644 index d003427e..00000000 --- a/backend/services/presence-service/prisma/migrations/20251215100000_change_user_id_to_string/migration.sql +++ /dev/null @@ -1,9 +0,0 @@ --- Migration: Change user_id from BIGINT to VARCHAR(20) --- Reason: user_id stores userSerialNum (e.g., "D25121400005") which is a string, not a number - --- Alter the user_id column type from BIGINT to VARCHAR(20) -ALTER TABLE "analytics_event_log" - ALTER COLUMN "user_id" TYPE VARCHAR(20); - --- Add index for user_id for better query performance -CREATE INDEX IF NOT EXISTS "idx_event_log_user_id" ON "analytics_event_log"("user_id"); diff --git a/backend/services/wallet-service/prisma/migrations/20241216000000_add_version_column/migration.sql b/backend/services/wallet-service/prisma/migrations/20241216000000_add_version_column/migration.sql new file mode 100644 index 00000000..6c312f0d --- /dev/null +++ b/backend/services/wallet-service/prisma/migrations/20241216000000_add_version_column/migration.sql @@ -0,0 +1,2 @@ +-- Add version column for optimistic locking +ALTER TABLE "wallet_accounts" ADD COLUMN "version" INTEGER NOT NULL DEFAULT 0;