diff --git a/backend/services/auth-service/prisma/migrations/20260111000000_init/migration.sql b/backend/services/auth-service/prisma/migrations/0001_init/migration.sql similarity index 94% rename from backend/services/auth-service/prisma/migrations/20260111000000_init/migration.sql rename to backend/services/auth-service/prisma/migrations/0001_init/migration.sql index 57d34ce1..0f9457eb 100644 --- a/backend/services/auth-service/prisma/migrations/20260111000000_init/migration.sql +++ b/backend/services/auth-service/prisma/migrations/0001_init/migration.sql @@ -1,3 +1,9 @@ +-- ============================================================================ +-- auth-service 初始化 migration +-- 合并自: 20260111000000_init, 20260111083500_allow_nullable_phone_password, +-- 20260112110000_add_nickname_to_synced_legacy_users +-- ============================================================================ + -- CreateEnum CREATE TYPE "UserStatus" AS ENUM ('ACTIVE', 'DISABLED', 'DELETED'); @@ -46,8 +52,9 @@ CREATE TABLE "synced_legacy_users" ( "id" BIGSERIAL NOT NULL, "legacy_id" BIGINT NOT NULL, "account_sequence" TEXT NOT NULL, - "phone" TEXT NOT NULL, - "password_hash" TEXT NOT NULL, + "phone" TEXT, + "password_hash" TEXT, + "nickname" TEXT, "status" TEXT NOT NULL, "legacy_created_at" TIMESTAMP(3) NOT NULL, "migrated_to_v2" BOOLEAN NOT NULL DEFAULT false, @@ -234,4 +241,3 @@ ALTER TABLE "sms_logs" ADD CONSTRAINT "sms_logs_user_id_fkey" FOREIGN KEY ("user -- AddForeignKey ALTER TABLE "login_logs" ADD CONSTRAINT "login_logs_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE; - diff --git a/backend/services/auth-service/prisma/migrations/20260111083500_allow_nullable_phone_password/migration.sql b/backend/services/auth-service/prisma/migrations/20260111083500_allow_nullable_phone_password/migration.sql deleted file mode 100644 index 83238cd0..00000000 --- a/backend/services/auth-service/prisma/migrations/20260111083500_allow_nullable_phone_password/migration.sql +++ /dev/null @@ -1,3 +0,0 @@ --- AlterTable: 允许 phone 和 password_hash 为空(支持系统账户同步) -ALTER TABLE "synced_legacy_users" ALTER COLUMN "phone" DROP NOT NULL; -ALTER TABLE "synced_legacy_users" ALTER COLUMN "password_hash" DROP NOT NULL; diff --git a/backend/services/auth-service/prisma/migrations/20260112110000_add_nickname_to_synced_legacy_users/migration.sql b/backend/services/auth-service/prisma/migrations/20260112110000_add_nickname_to_synced_legacy_users/migration.sql deleted file mode 100644 index b1cf09d1..00000000 --- a/backend/services/auth-service/prisma/migrations/20260112110000_add_nickname_to_synced_legacy_users/migration.sql +++ /dev/null @@ -1,2 +0,0 @@ --- Add nickname field to synced_legacy_users table -ALTER TABLE "synced_legacy_users" ADD COLUMN "nickname" TEXT; diff --git a/backend/services/auth-service/prisma/migrations/migration_lock.toml b/backend/services/auth-service/prisma/migrations/migration_lock.toml new file mode 100644 index 00000000..99e4f200 --- /dev/null +++ b/backend/services/auth-service/prisma/migrations/migration_lock.toml @@ -0,0 +1,3 @@ +# Please do not edit this file manually +# It should be added in your version-control system (i.e. Git) +provider = "postgresql" diff --git a/backend/services/contribution-service/prisma/migrations/20260111000000_init/migration.sql b/backend/services/contribution-service/prisma/migrations/0001_init/migration.sql similarity index 77% rename from backend/services/contribution-service/prisma/migrations/20260111000000_init/migration.sql rename to backend/services/contribution-service/prisma/migrations/0001_init/migration.sql index 5064cfdf..60dd28d7 100644 --- a/backend/services/contribution-service/prisma/migrations/20260111000000_init/migration.sql +++ b/backend/services/contribution-service/prisma/migrations/0001_init/migration.sql @@ -1,18 +1,8 @@ --- ============================================ --- Migration: contribution-service 初始化 --- --- 包含所有表的创建: --- 1. CDC 同步表 (synced_users, synced_adoptions, synced_referrals) --- 2. 算力账户表 (contribution_accounts) --- 3. 算力明细表 (contribution_records) --- 4. 解锁事件表 (unlock_events) --- 5. 未分配算力表 (unallocated_contributions) --- 6. 系统账户表 (system_accounts, system_contribution_records) --- 7. 快照与统计表 (daily_contribution_snapshots, user_team_stats) --- 8. CDC 同步状态表 (cdc_sync_progress, processed_events) --- 9. 配置表 (contribution_configs, distribution_rate_configs) --- 10. Outbox 事件表 (outbox_events) --- ============================================ +-- ============================================================================ +-- contribution-service 初始化 migration +-- 合并自: 20260111000000_init, 20260111100000_add_referral_user_ids, +-- 20260112020000_fix_status_varchar_length, 20260112200000_add_adoption_province_city +-- ============================================================================ -- ============================================ -- 1. CDC 同步数据表 @@ -45,12 +35,15 @@ CREATE TABLE "synced_adoptions" ( "account_sequence" VARCHAR(20) NOT NULL, "tree_count" INTEGER NOT NULL, "adoption_date" DATE NOT NULL, - "status" VARCHAR(20), + "status" VARCHAR(30), + "selected_province" VARCHAR(10), + "selected_city" VARCHAR(10), "contribution_per_tree" DECIMAL(20,10) NOT NULL, "source_sequence_num" BIGINT NOT NULL, "synced_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, "contribution_distributed" BOOLEAN NOT NULL DEFAULT false, "contribution_distributed_at" TIMESTAMP(3), + "distribution_summary" TEXT, "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, CONSTRAINT "synced_adoptions_pkey" PRIMARY KEY ("id") @@ -60,12 +53,15 @@ CREATE UNIQUE INDEX "synced_adoptions_original_adoption_id_key" ON "synced_adopt CREATE INDEX "synced_adoptions_account_sequence_idx" ON "synced_adoptions"("account_sequence"); CREATE INDEX "synced_adoptions_adoption_date_idx" ON "synced_adoptions"("adoption_date"); CREATE INDEX "synced_adoptions_contribution_distributed_idx" ON "synced_adoptions"("contribution_distributed"); +CREATE INDEX "synced_adoptions_selected_province_selected_city_idx" ON "synced_adoptions"("selected_province", "selected_city"); -- 同步的推荐关系数据 CREATE TABLE "synced_referrals" ( "id" BIGSERIAL NOT NULL, "account_sequence" VARCHAR(20) NOT NULL, "referrer_account_sequence" VARCHAR(20), + "referrer_user_id" BIGINT, + "original_user_id" BIGINT, "ancestor_path" TEXT, "depth" INTEGER NOT NULL DEFAULT 0, "source_sequence_num" BIGINT NOT NULL, @@ -77,70 +73,45 @@ CREATE TABLE "synced_referrals" ( CREATE UNIQUE INDEX "synced_referrals_account_sequence_key" ON "synced_referrals"("account_sequence"); CREATE INDEX "synced_referrals_referrer_account_sequence_idx" ON "synced_referrals"("referrer_account_sequence"); +CREATE INDEX "synced_referrals_referrer_user_id_idx" ON "synced_referrals"("referrer_user_id"); +CREATE INDEX "synced_referrals_original_user_id_idx" ON "synced_referrals"("original_user_id"); -- ============================================ -- 2. 算力账户表 -- ============================================ --- 算力账户表(汇总) --- 设计说明: --- - 个人算力:自己认种,立即生效 --- - 层级算力:下级1-15层认种的分成,每层0.5%,共7.5% --- - 加成算力:团队加成,3档各2.5%,共7.5% --- 解锁规则: --- - 自己认种:解锁层级1-5 + 加成第1档 --- - 直推≥2人认种:解锁加成第2档 --- - 直推≥3人认种:解锁层级6-10 --- - 直推≥4人认种:解锁加成第3档 --- - 直推≥5人认种:解锁层级11-15 CREATE TABLE "contribution_accounts" ( "id" BIGSERIAL NOT NULL, "account_sequence" VARCHAR(20) NOT NULL, - - -- 个人算力(立即生效) "personal_contribution" DECIMAL(30,10) NOT NULL DEFAULT 0, - - -- 15级层级算力(待解锁) - -- 第1档(1-5级):自己认种解锁 "level_1_pending" DECIMAL(30,10) NOT NULL DEFAULT 0, "level_2_pending" DECIMAL(30,10) NOT NULL DEFAULT 0, "level_3_pending" DECIMAL(30,10) NOT NULL DEFAULT 0, "level_4_pending" DECIMAL(30,10) NOT NULL DEFAULT 0, "level_5_pending" DECIMAL(30,10) NOT NULL DEFAULT 0, - -- 第2档(6-10级):直推≥3人认种解锁 "level_6_pending" DECIMAL(30,10) NOT NULL DEFAULT 0, "level_7_pending" DECIMAL(30,10) NOT NULL DEFAULT 0, "level_8_pending" DECIMAL(30,10) NOT NULL DEFAULT 0, "level_9_pending" DECIMAL(30,10) NOT NULL DEFAULT 0, "level_10_pending" DECIMAL(30,10) NOT NULL DEFAULT 0, - -- 第3档(11-15级):直推≥5人认种解锁 "level_11_pending" DECIMAL(30,10) NOT NULL DEFAULT 0, "level_12_pending" DECIMAL(30,10) NOT NULL DEFAULT 0, "level_13_pending" DECIMAL(30,10) NOT NULL DEFAULT 0, "level_14_pending" DECIMAL(30,10) NOT NULL DEFAULT 0, "level_15_pending" DECIMAL(30,10) NOT NULL DEFAULT 0, - - -- 3档加成算力(待解锁) "bonus_tier_1_pending" DECIMAL(30,10) NOT NULL DEFAULT 0, "bonus_tier_2_pending" DECIMAL(30,10) NOT NULL DEFAULT 0, "bonus_tier_3_pending" DECIMAL(30,10) NOT NULL DEFAULT 0, - - -- 汇总字段 "total_level_pending" DECIMAL(30,10) NOT NULL DEFAULT 0, "total_bonus_pending" DECIMAL(30,10) NOT NULL DEFAULT 0, "total_pending" DECIMAL(30,10) NOT NULL DEFAULT 0, "total_unlocked" DECIMAL(30,10) NOT NULL DEFAULT 0, "effective_contribution" DECIMAL(30,10) NOT NULL DEFAULT 0, - - -- 解锁条件状态 "has_adopted" BOOLEAN NOT NULL DEFAULT false, "direct_referral_adopted_count" INTEGER NOT NULL DEFAULT 0, "unlocked_level_depth" INTEGER NOT NULL DEFAULT 0, "unlocked_bonus_tiers" INTEGER NOT NULL DEFAULT 0, - - -- 乐观锁 "version" INTEGER NOT NULL DEFAULT 1, - "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, "updated_at" TIMESTAMP(3) NOT NULL, @@ -152,29 +123,6 @@ CREATE INDEX "contribution_accounts_effective_contribution_idx" ON "contribution CREATE INDEX "contribution_accounts_has_adopted_idx" ON "contribution_accounts"("has_adopted"); CREATE INDEX "contribution_accounts_direct_referral_adopted_count_idx" ON "contribution_accounts"("direct_referral_adopted_count"); --- 添加字段注释 -COMMENT ON COLUMN "contribution_accounts"."level_1_pending" IS '第1级待解锁算力 (0.5%)'; -COMMENT ON COLUMN "contribution_accounts"."level_2_pending" IS '第2级待解锁算力 (0.5%)'; -COMMENT ON COLUMN "contribution_accounts"."level_3_pending" IS '第3级待解锁算力 (0.5%)'; -COMMENT ON COLUMN "contribution_accounts"."level_4_pending" IS '第4级待解锁算力 (0.5%)'; -COMMENT ON COLUMN "contribution_accounts"."level_5_pending" IS '第5级待解锁算力 (0.5%)'; -COMMENT ON COLUMN "contribution_accounts"."level_6_pending" IS '第6级待解锁算力 (0.5%) - 直推≥3人认种解锁'; -COMMENT ON COLUMN "contribution_accounts"."level_7_pending" IS '第7级待解锁算力 (0.5%) - 直推≥3人认种解锁'; -COMMENT ON COLUMN "contribution_accounts"."level_8_pending" IS '第8级待解锁算力 (0.5%) - 直推≥3人认种解锁'; -COMMENT ON COLUMN "contribution_accounts"."level_9_pending" IS '第9级待解锁算力 (0.5%) - 直推≥3人认种解锁'; -COMMENT ON COLUMN "contribution_accounts"."level_10_pending" IS '第10级待解锁算力 (0.5%) - 直推≥3人认种解锁'; -COMMENT ON COLUMN "contribution_accounts"."level_11_pending" IS '第11级待解锁算力 (0.5%) - 直推≥5人认种解锁'; -COMMENT ON COLUMN "contribution_accounts"."level_12_pending" IS '第12级待解锁算力 (0.5%) - 直推≥5人认种解锁'; -COMMENT ON COLUMN "contribution_accounts"."level_13_pending" IS '第13级待解锁算力 (0.5%) - 直推≥5人认种解锁'; -COMMENT ON COLUMN "contribution_accounts"."level_14_pending" IS '第14级待解锁算力 (0.5%) - 直推≥5人认种解锁'; -COMMENT ON COLUMN "contribution_accounts"."level_15_pending" IS '第15级待解锁算力 (0.5%) - 直推≥5人认种解锁'; -COMMENT ON COLUMN "contribution_accounts"."bonus_tier_1_pending" IS '第1档加成待解锁算力 (2.5%) - 自己认种解锁'; -COMMENT ON COLUMN "contribution_accounts"."bonus_tier_2_pending" IS '第2档加成待解锁算力 (2.5%) - 直推≥2人认种解锁'; -COMMENT ON COLUMN "contribution_accounts"."bonus_tier_3_pending" IS '第3档加成待解锁算力 (2.5%) - 直推≥4人认种解锁'; -COMMENT ON COLUMN "contribution_accounts"."unlocked_level_depth" IS '已解锁层级深度: 0=未解锁, 5=1-5级, 10=1-10级, 15=全部'; -COMMENT ON COLUMN "contribution_accounts"."unlocked_bonus_tiers" IS '已解锁加成档位数: 0/1/2/3'; -COMMENT ON COLUMN "contribution_accounts"."effective_contribution" IS '有效算力 = 个人算力 + 已解锁算力'; - -- ============================================ -- 3. 算力明细表 -- ============================================ @@ -182,36 +130,23 @@ COMMENT ON COLUMN "contribution_accounts"."effective_contribution" IS '有效算 CREATE TABLE "contribution_records" ( "id" BIGSERIAL NOT NULL, "account_sequence" VARCHAR(20) NOT NULL, - - -- 来源信息(可追溯) "source_type" VARCHAR(30) NOT NULL, "source_adoption_id" BIGINT NOT NULL, "source_account_sequence" VARCHAR(20) NOT NULL, - - -- 计算参数(审计用) "tree_count" INTEGER NOT NULL, "base_contribution" DECIMAL(20,10) NOT NULL, "distribution_rate" DECIMAL(10,6) NOT NULL, "level_depth" INTEGER, "bonus_tier" INTEGER, - - -- 金额 "amount" DECIMAL(30,10) NOT NULL, - - -- 解锁状态 "status" VARCHAR(20) NOT NULL DEFAULT 'PENDING', "unlocked_at" TIMESTAMP(3), "unlock_reason" VARCHAR(200), - - -- 有效期 "effective_date" DATE NOT NULL, "expire_date" DATE NOT NULL, "is_expired" BOOLEAN NOT NULL DEFAULT false, "expired_at" TIMESTAMP(3), - - -- 备注 "remark" VARCHAR(500), - "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, CONSTRAINT "contribution_records_pkey" PRIMARY KEY ("id") @@ -226,9 +161,6 @@ CREATE INDEX "contribution_records_status_idx" ON "contribution_records"("status CREATE INDEX "contribution_records_expire_date_idx" ON "contribution_records"("expire_date"); CREATE INDEX "contribution_records_is_expired_idx" ON "contribution_records"("is_expired"); -COMMENT ON COLUMN "contribution_records"."status" IS '状态: PENDING(待解锁)/UNLOCKED(已解锁)/EFFECTIVE(已生效)'; -COMMENT ON COLUMN "contribution_records"."source_type" IS '来源类型: PERSONAL/LEVEL_1~15/BONUS_TIER_1~3'; - -- ============================================ -- 4. 解锁事件表 -- ============================================ @@ -236,31 +168,20 @@ COMMENT ON COLUMN "contribution_records"."source_type" IS '来源类型: PERSONA CREATE TABLE "unlock_events" ( "id" BIGSERIAL NOT NULL, "account_sequence" VARCHAR(20) NOT NULL, - - -- 触发信息 "trigger_type" VARCHAR(30) NOT NULL, "trigger_adoption_id" BIGINT NOT NULL, "trigger_account_sequence" VARCHAR(20) NOT NULL, - - -- 解锁内容 "unlock_type" VARCHAR(30) NOT NULL, "unlock_condition" VARCHAR(100) NOT NULL, - - -- 解锁前后状态 "before_direct_referral_count" INTEGER NOT NULL, "after_direct_referral_count" INTEGER NOT NULL, "before_unlocked_level_depth" INTEGER NOT NULL, "after_unlocked_level_depth" INTEGER NOT NULL, "before_unlocked_bonus_tiers" INTEGER NOT NULL, "after_unlocked_bonus_tiers" INTEGER NOT NULL, - - -- 解锁金额 "unlocked_amount" DECIMAL(30,10) NOT NULL, "unlocked_record_count" INTEGER NOT NULL, - - -- 备注 "remark" VARCHAR(500), - "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, CONSTRAINT "unlock_events_pkey" PRIMARY KEY ("id") @@ -272,11 +193,6 @@ CREATE INDEX "unlock_events_trigger_adoption_id_idx" ON "unlock_events"("trigger CREATE INDEX "unlock_events_unlock_type_idx" ON "unlock_events"("unlock_type"); CREATE INDEX "unlock_events_created_at_idx" ON "unlock_events"("created_at" DESC); -COMMENT ON TABLE "unlock_events" IS '解锁事件记录表 - 记录每次算力解锁的触发原因和结果'; -COMMENT ON COLUMN "unlock_events"."trigger_type" IS '触发类型: SELF_ADOPT(自己认种) / REFERRAL_ADOPT(直推认种)'; -COMMENT ON COLUMN "unlock_events"."unlock_type" IS '解锁类型: LEVEL_1_5/LEVEL_6_10/LEVEL_11_15/BONUS_TIER_1/BONUS_TIER_2/BONUS_TIER_3'; -COMMENT ON COLUMN "unlock_events"."unlock_condition" IS '解锁条件描述,如"自己认种"、"直推认种人数达到3人"'; - -- ============================================ -- 5. 未分配算力表 -- ============================================ @@ -285,25 +201,17 @@ CREATE TABLE "unallocated_contributions" ( "id" BIGSERIAL NOT NULL, "source_adoption_id" BIGINT NOT NULL, "source_account_sequence" VARCHAR(20) NOT NULL, - - -- 未分配类型 "unalloc_type" VARCHAR(30) NOT NULL, "would_be_account_sequence" VARCHAR(20), "level_depth" INTEGER, "bonus_tier" INTEGER, - "amount" DECIMAL(30,10) NOT NULL, "reason" VARCHAR(200), - - -- 分配状态 "status" VARCHAR(20) NOT NULL DEFAULT 'PENDING', "allocated_at" TIMESTAMP(3), "allocated_to_account_sequence" VARCHAR(20), - - -- 有效期 "effective_date" DATE NOT NULL, "expire_date" DATE NOT NULL, - "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, CONSTRAINT "unallocated_contributions_pkey" PRIMARY KEY ("id") @@ -357,7 +265,6 @@ ALTER TABLE "system_contribution_records" ADD CONSTRAINT "system_contribution_re -- 7. 快照与统计表 -- ============================================ --- 每日算力快照 CREATE TABLE "daily_contribution_snapshots" ( "id" BIGSERIAL NOT NULL, "snapshot_date" DATE NOT NULL, @@ -374,7 +281,6 @@ CREATE UNIQUE INDEX "daily_contribution_snapshots_snapshot_date_account_sequence CREATE INDEX "daily_contribution_snapshots_snapshot_date_idx" ON "daily_contribution_snapshots"("snapshot_date"); CREATE INDEX "daily_contribution_snapshots_account_sequence_idx" ON "daily_contribution_snapshots"("account_sequence"); --- 用户团队统计 CREATE TABLE "user_team_stats" ( "id" BIGSERIAL NOT NULL, "account_sequence" VARCHAR(20) NOT NULL, @@ -468,7 +374,52 @@ CREATE UNIQUE INDEX "distribution_rate_configs_rate_type_key" ON "distribution_r CREATE INDEX "distribution_rate_configs_is_active_idx" ON "distribution_rate_configs"("is_active"); -- ============================================ --- 10. Outbox 事件表 +-- 10. 全网进度表 +-- ============================================ + +CREATE TABLE "network_adoption_progress" ( + "id" BIGSERIAL NOT NULL, + "total_tree_count" INTEGER NOT NULL DEFAULT 0, + "total_adoption_orders" INTEGER NOT NULL DEFAULT 0, + "total_adopted_users" INTEGER NOT NULL DEFAULT 0, + "current_unit" INTEGER NOT NULL DEFAULT 0, + "current_multiplier" DECIMAL(10,6) NOT NULL DEFAULT 1.0, + "current_contribution_per_tree" DECIMAL(20,10) NOT NULL DEFAULT 22617, + "next_unit_tree_count" INTEGER NOT NULL DEFAULT 1000, + "last_adoption_id" BIGINT, + "last_adoption_date" DATE, + "updated_at" TIMESTAMP(3) NOT NULL, + "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + + CONSTRAINT "network_adoption_progress_pkey" PRIMARY KEY ("id") +); + +CREATE TABLE "daily_contribution_rates" ( + "id" BIGSERIAL NOT NULL, + "effective_date" DATE NOT NULL, + "start_tree_count" INTEGER NOT NULL DEFAULT 0, + "start_unit" INTEGER NOT NULL DEFAULT 0, + "start_multiplier" DECIMAL(10,6) NOT NULL DEFAULT 1.0, + "contribution_per_tree" DECIMAL(20,10) NOT NULL, + "end_tree_count" INTEGER, + "end_unit" INTEGER, + "end_multiplier" DECIMAL(10,6), + "daily_tree_count" INTEGER NOT NULL DEFAULT 0, + "daily_adoption_orders" INTEGER NOT NULL DEFAULT 0, + "daily_adopted_users" INTEGER NOT NULL DEFAULT 0, + "is_closed" BOOLEAN NOT NULL DEFAULT false, + "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updated_at" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "daily_contribution_rates_pkey" PRIMARY KEY ("id") +); + +CREATE UNIQUE INDEX "daily_contribution_rates_effective_date_key" ON "daily_contribution_rates"("effective_date"); +CREATE INDEX "daily_contribution_rates_effective_date_idx" ON "daily_contribution_rates"("effective_date"); +CREATE INDEX "daily_contribution_rates_is_closed_idx" ON "daily_contribution_rates"("is_closed"); + +-- ============================================ +-- 11. Outbox 事件表 -- ============================================ CREATE TABLE "outbox_events" ( diff --git a/backend/services/contribution-service/prisma/migrations/20260111100000_add_referral_user_ids/migration.sql b/backend/services/contribution-service/prisma/migrations/20260111100000_add_referral_user_ids/migration.sql deleted file mode 100644 index 7bfbb8cf..00000000 --- a/backend/services/contribution-service/prisma/migrations/20260111100000_add_referral_user_ids/migration.sql +++ /dev/null @@ -1,12 +0,0 @@ --- Migration: 添加推荐关系的 user_id 字段 --- 用于完整同步 1.0 referral_relationships 表数据 - --- 添加 referrer_user_id 字段 (1.0 的 referrer_id) -ALTER TABLE "synced_referrals" ADD COLUMN "referrer_user_id" BIGINT; - --- 添加 original_user_id 字段 (1.0 的 user_id) -ALTER TABLE "synced_referrals" ADD COLUMN "original_user_id" BIGINT; - --- 创建索引 -CREATE INDEX "synced_referrals_referrer_user_id_idx" ON "synced_referrals"("referrer_user_id"); -CREATE INDEX "synced_referrals_original_user_id_idx" ON "synced_referrals"("original_user_id"); diff --git a/backend/services/contribution-service/prisma/migrations/20260112020000_fix_status_varchar_length/migration.sql b/backend/services/contribution-service/prisma/migrations/20260112020000_fix_status_varchar_length/migration.sql deleted file mode 100644 index bf3449bf..00000000 --- a/backend/services/contribution-service/prisma/migrations/20260112020000_fix_status_varchar_length/migration.sql +++ /dev/null @@ -1,4 +0,0 @@ --- 修复 synced_adoptions.status 字段长度 --- 1.0 planting_orders.status 是 VARCHAR(30),需要匹配 - -ALTER TABLE "synced_adoptions" ALTER COLUMN "status" TYPE VARCHAR(30); diff --git a/backend/services/contribution-service/prisma/migrations/20260112200000_add_adoption_province_city/migration.sql b/backend/services/contribution-service/prisma/migrations/20260112200000_add_adoption_province_city/migration.sql deleted file mode 100644 index c48b0a33..00000000 --- a/backend/services/contribution-service/prisma/migrations/20260112200000_add_adoption_province_city/migration.sql +++ /dev/null @@ -1,7 +0,0 @@ --- AlterTable: 添加认种省市字段到 synced_adoptions 表 --- 这些字段从 1.0 planting_orders 表的 selected_province/selected_city 同步 -ALTER TABLE "synced_adoptions" ADD COLUMN "selected_province" VARCHAR(10); -ALTER TABLE "synced_adoptions" ADD COLUMN "selected_city" VARCHAR(10); - --- CreateIndex: 添加省市组合索引 -CREATE INDEX "synced_adoptions_selected_province_selected_city_idx" ON "synced_adoptions"("selected_province", "selected_city"); diff --git a/backend/services/mining-admin-service/prisma/migrations/20260111000000_init/migration.sql b/backend/services/mining-admin-service/prisma/migrations/0001_init/migration.sql similarity index 83% rename from backend/services/mining-admin-service/prisma/migrations/20260111000000_init/migration.sql rename to backend/services/mining-admin-service/prisma/migrations/0001_init/migration.sql index 5e34ec03..51ee26f3 100644 --- a/backend/services/mining-admin-service/prisma/migrations/20260111000000_init/migration.sql +++ b/backend/services/mining-admin-service/prisma/migrations/0001_init/migration.sql @@ -1,3 +1,11 @@ +-- ============================================================================ +-- mining-admin-service 初始化 migration +-- 合并自: 20260111000000_init, 20260112110000_add_referral_adoption_nickname, +-- 20260112150000_add_unlocked_bonus_tiers, 20260112200000_add_contribution_records_network_progress, +-- 20260113000000_use_prisma_relation_mode, 20260113100000_add_distribution_summary +-- 注意: 使用 Prisma relationMode = "prisma",不在数据库层创建FK约束 +-- ============================================================================ + -- CreateTable CREATE TABLE "admin_users" ( "id" TEXT NOT NULL, @@ -102,6 +110,7 @@ CREATE TABLE "synced_users" ( "originalUserId" TEXT NOT NULL, "accountSequence" TEXT NOT NULL, "phone" TEXT NOT NULL, + "nickname" TEXT, "status" TEXT NOT NULL, "kycStatus" TEXT NOT NULL, "realName" TEXT, @@ -113,6 +122,37 @@ CREATE TABLE "synced_users" ( CONSTRAINT "synced_users_pkey" PRIMARY KEY ("id") ); +-- CreateTable +CREATE TABLE "synced_referrals" ( + "id" TEXT NOT NULL, + "accountSequence" TEXT NOT NULL, + "referrerAccountSequence" TEXT, + "referrerUserId" BIGINT, + "originalUserId" BIGINT, + "ancestorPath" TEXT, + "depth" INTEGER NOT NULL DEFAULT 0, + "syncedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "synced_referrals_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "synced_adoptions" ( + "id" TEXT NOT NULL, + "originalAdoptionId" BIGINT NOT NULL, + "accountSequence" TEXT NOT NULL, + "treeCount" INTEGER NOT NULL, + "adoptionDate" DATE NOT NULL, + "status" TEXT, + "contributionPerTree" DECIMAL(20,10) NOT NULL, + "distributionSummary" TEXT, + "syncedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "synced_adoptions_pkey" PRIMARY KEY ("id") +); + -- CreateTable CREATE TABLE "synced_contribution_accounts" ( "id" TEXT NOT NULL, @@ -125,12 +165,53 @@ CREATE TABLE "synced_contribution_accounts" ( "hasAdopted" BOOLEAN NOT NULL DEFAULT false, "directReferralCount" INTEGER NOT NULL DEFAULT 0, "unlockedLevelDepth" INTEGER NOT NULL DEFAULT 0, + "unlockedBonusTiers" INTEGER NOT NULL DEFAULT 0, "syncedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, "updatedAt" TIMESTAMP(3) NOT NULL, CONSTRAINT "synced_contribution_accounts_pkey" PRIMARY KEY ("id") ); +-- CreateTable +CREATE TABLE "synced_contribution_records" ( + "id" TEXT NOT NULL, + "originalRecordId" BIGINT NOT NULL, + "accountSequence" TEXT NOT NULL, + "sourceType" TEXT NOT NULL, + "sourceAdoptionId" BIGINT NOT NULL, + "sourceAccountSequence" TEXT NOT NULL, + "treeCount" INTEGER NOT NULL, + "baseContribution" DECIMAL(20,10) NOT NULL, + "distributionRate" DECIMAL(10,6) NOT NULL, + "levelDepth" INTEGER, + "bonusTier" INTEGER, + "amount" DECIMAL(30,10) NOT NULL, + "effectiveDate" DATE NOT NULL, + "expireDate" DATE NOT NULL, + "isExpired" BOOLEAN NOT NULL DEFAULT false, + "syncedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMP(3) NOT NULL, + "createdAt" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "synced_contribution_records_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "synced_network_progress" ( + "id" TEXT NOT NULL, + "totalTreeCount" INTEGER NOT NULL DEFAULT 0, + "totalAdoptionOrders" INTEGER NOT NULL DEFAULT 0, + "totalAdoptedUsers" INTEGER NOT NULL DEFAULT 0, + "currentUnit" INTEGER NOT NULL DEFAULT 0, + "currentMultiplier" DECIMAL(10,6) NOT NULL DEFAULT 1.0, + "currentContributionPerTree" DECIMAL(20,10) NOT NULL DEFAULT 22617, + "nextUnitTreeCount" INTEGER NOT NULL DEFAULT 1000, + "syncedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "synced_network_progress_pkey" PRIMARY KEY ("id") +); + -- CreateTable CREATE TABLE "synced_mining_accounts" ( "id" TEXT NOT NULL, @@ -543,15 +624,63 @@ CREATE INDEX "synced_users_kycStatus_idx" ON "synced_users"("kycStatus"); -- CreateIndex CREATE INDEX "synced_users_createdAt_idx" ON "synced_users"("createdAt" DESC); +-- CreateIndex +CREATE UNIQUE INDEX "synced_referrals_accountSequence_key" ON "synced_referrals"("accountSequence"); + +-- CreateIndex +CREATE INDEX "synced_referrals_referrerAccountSequence_idx" ON "synced_referrals"("referrerAccountSequence"); + +-- CreateIndex +CREATE INDEX "synced_referrals_depth_idx" ON "synced_referrals"("depth"); + +-- CreateIndex +CREATE INDEX "synced_referrals_accountSequence_idx" ON "synced_referrals"("accountSequence"); + +-- CreateIndex +CREATE UNIQUE INDEX "synced_adoptions_originalAdoptionId_key" ON "synced_adoptions"("originalAdoptionId"); + +-- CreateIndex +CREATE INDEX "synced_adoptions_accountSequence_idx" ON "synced_adoptions"("accountSequence"); + +-- CreateIndex +CREATE INDEX "synced_adoptions_adoptionDate_idx" ON "synced_adoptions"("adoptionDate"); + -- CreateIndex CREATE UNIQUE INDEX "synced_contribution_accounts_accountSequence_key" ON "synced_contribution_accounts"("accountSequence"); +-- CreateIndex +CREATE INDEX "synced_contribution_accounts_accountSequence_idx" ON "synced_contribution_accounts"("accountSequence"); + +-- CreateIndex +CREATE UNIQUE INDEX "synced_contribution_records_originalRecordId_key" ON "synced_contribution_records"("originalRecordId"); + +-- CreateIndex +CREATE INDEX "synced_contribution_records_accountSequence_idx" ON "synced_contribution_records"("accountSequence"); + +-- CreateIndex +CREATE INDEX "synced_contribution_records_sourceAccountSequence_idx" ON "synced_contribution_records"("sourceAccountSequence"); + +-- CreateIndex +CREATE INDEX "synced_contribution_records_sourceAdoptionId_idx" ON "synced_contribution_records"("sourceAdoptionId"); + +-- CreateIndex +CREATE INDEX "synced_contribution_records_sourceType_idx" ON "synced_contribution_records"("sourceType"); + +-- CreateIndex +CREATE INDEX "synced_contribution_records_createdAt_idx" ON "synced_contribution_records"("createdAt" DESC); + -- CreateIndex CREATE UNIQUE INDEX "synced_mining_accounts_accountSequence_key" ON "synced_mining_accounts"("accountSequence"); +-- CreateIndex +CREATE INDEX "synced_mining_accounts_accountSequence_idx" ON "synced_mining_accounts"("accountSequence"); + -- CreateIndex CREATE UNIQUE INDEX "synced_trading_accounts_accountSequence_key" ON "synced_trading_accounts"("accountSequence"); +-- CreateIndex +CREATE INDEX "synced_trading_accounts_accountSequence_idx" ON "synced_trading_accounts"("accountSequence"); + -- CreateIndex CREATE UNIQUE INDEX "synced_daily_mining_stats_statDate_key" ON "synced_daily_mining_stats"("statDate"); @@ -729,21 +858,5 @@ CREATE UNIQUE INDEX "synced_fee_configs_original_id_key" ON "synced_fee_configs" -- CreateIndex CREATE UNIQUE INDEX "synced_fee_configs_fee_type_key" ON "synced_fee_configs"("fee_type"); --- AddForeignKey +-- AddForeignKey (保留 admin 相关的外键) ALTER TABLE "audit_logs" ADD CONSTRAINT "audit_logs_adminId_fkey" FOREIGN KEY ("adminId") REFERENCES "admin_users"("id") ON DELETE RESTRICT ON UPDATE CASCADE; - --- AddForeignKey -ALTER TABLE "synced_contribution_accounts" ADD CONSTRAINT "synced_contribution_accounts_accountSequence_fkey" FOREIGN KEY ("accountSequence") REFERENCES "synced_users"("accountSequence") ON DELETE RESTRICT ON UPDATE CASCADE; - --- AddForeignKey -ALTER TABLE "synced_mining_accounts" ADD CONSTRAINT "synced_mining_accounts_accountSequence_fkey" FOREIGN KEY ("accountSequence") REFERENCES "synced_users"("accountSequence") ON DELETE RESTRICT ON UPDATE CASCADE; - --- AddForeignKey -ALTER TABLE "synced_trading_accounts" ADD CONSTRAINT "synced_trading_accounts_accountSequence_fkey" FOREIGN KEY ("accountSequence") REFERENCES "synced_users"("accountSequence") ON DELETE RESTRICT ON UPDATE CASCADE; - --- AddForeignKey -ALTER TABLE "synced_cities" ADD CONSTRAINT "synced_cities_province_id_fkey" FOREIGN KEY ("province_id") REFERENCES "synced_provinces"("original_id") ON DELETE RESTRICT ON UPDATE CASCADE; - --- AddForeignKey -ALTER TABLE "synced_user_region_mappings" ADD CONSTRAINT "synced_user_region_mappings_city_id_fkey" FOREIGN KEY ("city_id") REFERENCES "synced_cities"("original_id") ON DELETE RESTRICT ON UPDATE CASCADE; - diff --git a/backend/services/mining-admin-service/prisma/migrations/20260112110000_add_referral_adoption_nickname/migration.sql b/backend/services/mining-admin-service/prisma/migrations/20260112110000_add_referral_adoption_nickname/migration.sql deleted file mode 100644 index c1e2af99..00000000 --- a/backend/services/mining-admin-service/prisma/migrations/20260112110000_add_referral_adoption_nickname/migration.sql +++ /dev/null @@ -1,45 +0,0 @@ --- Add nickname field to synced_users table -ALTER TABLE "synced_users" ADD COLUMN "nickname" TEXT; - --- Create synced_referrals table for referral relationships -CREATE TABLE "synced_referrals" ( - "id" TEXT NOT NULL, - "accountSequence" TEXT NOT NULL, - "referrerAccountSequence" TEXT, - "referrerUserId" BIGINT, - "originalUserId" BIGINT, - "ancestorPath" TEXT, - "depth" INTEGER NOT NULL DEFAULT 0, - "syncedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, - "updatedAt" TIMESTAMP(3) NOT NULL, - - CONSTRAINT "synced_referrals_pkey" PRIMARY KEY ("id") -); - --- Create synced_adoptions table for adoption records -CREATE TABLE "synced_adoptions" ( - "id" TEXT NOT NULL, - "originalAdoptionId" BIGINT NOT NULL, - "accountSequence" TEXT NOT NULL, - "treeCount" INTEGER NOT NULL, - "adoptionDate" DATE NOT NULL, - "status" TEXT, - "contributionPerTree" DECIMAL(20,10) NOT NULL, - "syncedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, - "updatedAt" TIMESTAMP(3) NOT NULL, - - CONSTRAINT "synced_adoptions_pkey" PRIMARY KEY ("id") -); - --- Create unique constraints -CREATE UNIQUE INDEX "synced_referrals_accountSequence_key" ON "synced_referrals"("accountSequence"); -CREATE UNIQUE INDEX "synced_adoptions_originalAdoptionId_key" ON "synced_adoptions"("originalAdoptionId"); - --- Create indexes -CREATE INDEX "synced_referrals_referrerAccountSequence_idx" ON "synced_referrals"("referrerAccountSequence"); -CREATE INDEX "synced_referrals_depth_idx" ON "synced_referrals"("depth"); -CREATE INDEX "synced_adoptions_accountSequence_idx" ON "synced_adoptions"("accountSequence"); -CREATE INDEX "synced_adoptions_adoptionDate_idx" ON "synced_adoptions"("adoptionDate"); - --- Add foreign key constraint for synced_referrals -ALTER TABLE "synced_referrals" ADD CONSTRAINT "synced_referrals_accountSequence_fkey" FOREIGN KEY ("accountSequence") REFERENCES "synced_users"("accountSequence") ON DELETE RESTRICT ON UPDATE CASCADE; diff --git a/backend/services/mining-admin-service/prisma/migrations/20260112150000_add_unlocked_bonus_tiers/migration.sql b/backend/services/mining-admin-service/prisma/migrations/20260112150000_add_unlocked_bonus_tiers/migration.sql deleted file mode 100644 index 6251801f..00000000 --- a/backend/services/mining-admin-service/prisma/migrations/20260112150000_add_unlocked_bonus_tiers/migration.sql +++ /dev/null @@ -1,2 +0,0 @@ --- AlterTable -ALTER TABLE "synced_contribution_accounts" ADD COLUMN "unlockedBonusTiers" INTEGER NOT NULL DEFAULT 0; diff --git a/backend/services/mining-admin-service/prisma/migrations/20260112200000_add_contribution_records_network_progress/migration.sql b/backend/services/mining-admin-service/prisma/migrations/20260112200000_add_contribution_records_network_progress/migration.sql deleted file mode 100644 index ff05eee6..00000000 --- a/backend/services/mining-admin-service/prisma/migrations/20260112200000_add_contribution_records_network_progress/migration.sql +++ /dev/null @@ -1,57 +0,0 @@ --- CreateTable -CREATE TABLE "synced_contribution_records" ( - "id" TEXT NOT NULL, - "originalRecordId" BIGINT NOT NULL, - "accountSequence" TEXT NOT NULL, - "sourceType" TEXT NOT NULL, - "sourceAdoptionId" BIGINT NOT NULL, - "sourceAccountSequence" TEXT NOT NULL, - "treeCount" INTEGER NOT NULL, - "baseContribution" DECIMAL(20,10) NOT NULL, - "distributionRate" DECIMAL(10,6) NOT NULL, - "levelDepth" INTEGER, - "bonusTier" INTEGER, - "amount" DECIMAL(30,10) NOT NULL, - "effectiveDate" DATE NOT NULL, - "expireDate" DATE NOT NULL, - "isExpired" BOOLEAN NOT NULL DEFAULT false, - "syncedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, - "updatedAt" TIMESTAMP(3) NOT NULL, - "createdAt" TIMESTAMP(3) NOT NULL, - - CONSTRAINT "synced_contribution_records_pkey" PRIMARY KEY ("id") -); - --- CreateTable -CREATE TABLE "synced_network_progress" ( - "id" TEXT NOT NULL, - "totalTreeCount" INTEGER NOT NULL DEFAULT 0, - "totalAdoptionOrders" INTEGER NOT NULL DEFAULT 0, - "totalAdoptedUsers" INTEGER NOT NULL DEFAULT 0, - "currentUnit" INTEGER NOT NULL DEFAULT 0, - "currentMultiplier" DECIMAL(10,6) NOT NULL DEFAULT 1.0, - "currentContributionPerTree" DECIMAL(20,10) NOT NULL DEFAULT 22617, - "nextUnitTreeCount" INTEGER NOT NULL DEFAULT 1000, - "syncedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, - "updatedAt" TIMESTAMP(3) NOT NULL, - - CONSTRAINT "synced_network_progress_pkey" PRIMARY KEY ("id") -); - --- CreateIndex -CREATE UNIQUE INDEX "synced_contribution_records_originalRecordId_key" ON "synced_contribution_records"("originalRecordId"); - --- CreateIndex -CREATE INDEX "synced_contribution_records_accountSequence_idx" ON "synced_contribution_records"("accountSequence"); - --- CreateIndex -CREATE INDEX "synced_contribution_records_sourceAccountSequence_idx" ON "synced_contribution_records"("sourceAccountSequence"); - --- CreateIndex -CREATE INDEX "synced_contribution_records_sourceAdoptionId_idx" ON "synced_contribution_records"("sourceAdoptionId"); - --- CreateIndex -CREATE INDEX "synced_contribution_records_sourceType_idx" ON "synced_contribution_records"("sourceType"); - --- CreateIndex -CREATE INDEX "synced_contribution_records_createdAt_idx" ON "synced_contribution_records"("createdAt" DESC); diff --git a/backend/services/mining-admin-service/prisma/migrations/20260113000000_use_prisma_relation_mode/migration.sql b/backend/services/mining-admin-service/prisma/migrations/20260113000000_use_prisma_relation_mode/migration.sql deleted file mode 100644 index fa74406f..00000000 --- a/backend/services/mining-admin-service/prisma/migrations/20260113000000_use_prisma_relation_mode/migration.sql +++ /dev/null @@ -1,36 +0,0 @@ --- ============================================================================= --- 切换到 Prisma relationMode = "prisma" --- 移除数据库层的外键约束,改由 Prisma Client 在应用层模拟外键关系 --- --- 原因:CDC 事件异步到达,顺序不可控,子表记录可能在父表记录之前到达 --- 参考:https://www.prisma.io/docs/orm/prisma-schema/data-model/relations/relation-mode --- ============================================================================= - --- 移除 synced_contribution_accounts 表的外键约束 -ALTER TABLE "synced_contribution_accounts" -DROP CONSTRAINT IF EXISTS "synced_contribution_accounts_accountSequence_fkey"; - --- 移除 synced_referrals 表的外键约束 -ALTER TABLE "synced_referrals" -DROP CONSTRAINT IF EXISTS "synced_referrals_accountSequence_fkey"; - --- 移除 synced_mining_accounts 表的外键约束 -ALTER TABLE "synced_mining_accounts" -DROP CONSTRAINT IF EXISTS "synced_mining_accounts_accountSequence_fkey"; - --- 移除 synced_trading_accounts 表的外键约束 -ALTER TABLE "synced_trading_accounts" -DROP CONSTRAINT IF EXISTS "synced_trading_accounts_accountSequence_fkey"; - --- 为 accountSequence 字段添加索引以优化 JOIN 性能(如果不存在) -CREATE INDEX IF NOT EXISTS "synced_contribution_accounts_accountSequence_idx" -ON "synced_contribution_accounts"("accountSequence"); - -CREATE INDEX IF NOT EXISTS "synced_referrals_accountSequence_idx" -ON "synced_referrals"("accountSequence"); - -CREATE INDEX IF NOT EXISTS "synced_mining_accounts_accountSequence_idx" -ON "synced_mining_accounts"("accountSequence"); - -CREATE INDEX IF NOT EXISTS "synced_trading_accounts_accountSequence_idx" -ON "synced_trading_accounts"("accountSequence"); diff --git a/backend/services/mining-admin-service/prisma/migrations/20260113100000_add_distribution_summary/migration.sql b/backend/services/mining-admin-service/prisma/migrations/20260113100000_add_distribution_summary/migration.sql deleted file mode 100644 index 569e3bbd..00000000 --- a/backend/services/mining-admin-service/prisma/migrations/20260113100000_add_distribution_summary/migration.sql +++ /dev/null @@ -1,2 +0,0 @@ --- AlterTable -ALTER TABLE "synced_adoptions" ADD COLUMN "distributionSummary" TEXT; diff --git a/backend/services/mining-service/prisma/migrations/20260111000000_init/migration.sql b/backend/services/mining-service/prisma/migrations/0001_init/migration.sql similarity index 98% rename from backend/services/mining-service/prisma/migrations/20260111000000_init/migration.sql rename to backend/services/mining-service/prisma/migrations/0001_init/migration.sql index 6588f86b..5f5c6655 100644 --- a/backend/services/mining-service/prisma/migrations/20260111000000_init/migration.sql +++ b/backend/services/mining-service/prisma/migrations/0001_init/migration.sql @@ -1,3 +1,8 @@ +-- ============================================================================ +-- mining-service 初始化 migration +-- 合并自: 20260111000000_init (只有一个,无需合并) +-- ============================================================================ + -- CreateEnum CREATE TYPE "PoolAccountType" AS ENUM ('SHARE_POOL', 'BLACK_HOLE_POOL', 'CIRCULATION_POOL'); diff --git a/backend/services/mining-wallet-service/prisma/migrations/20260111000000_init/migration.sql b/backend/services/mining-wallet-service/prisma/migrations/0001_init/migration.sql similarity index 69% rename from backend/services/mining-wallet-service/prisma/migrations/20260111000000_init/migration.sql rename to backend/services/mining-wallet-service/prisma/migrations/0001_init/migration.sql index 25c47a4a..f46f559d 100644 --- a/backend/services/mining-wallet-service/prisma/migrations/20260111000000_init/migration.sql +++ b/backend/services/mining-wallet-service/prisma/migrations/0001_init/migration.sql @@ -1,3 +1,10 @@ +-- ============================================================================ +-- mining-wallet-service 初始化 migration +-- 合并自: 20260111000000_init, 20260112180000_add_contribution_balance, +-- 20260112220000_remove_blockchain_tables +-- 注意: 区块链相关表已移除 +-- ============================================================================ + -- CreateEnum CREATE TYPE "SystemAccountType" AS ENUM ('HEADQUARTERS', 'OPERATION', 'PROVINCE', 'CITY', 'FEE', 'HOT_WALLET', 'COLD_WALLET'); @@ -16,9 +23,6 @@ CREATE TYPE "TransactionType" AS ENUM ('MINING_REWARD', 'MINING_DISTRIBUTE', 'TR -- CreateEnum CREATE TYPE "CounterpartyType" AS ENUM ('USER', 'SYSTEM_ACCOUNT', 'POOL', 'BLOCKCHAIN', 'EXTERNAL'); --- CreateEnum -CREATE TYPE "WithdrawStatus" AS ENUM ('PENDING', 'PROCESSING', 'CONFIRMING', 'COMPLETED', 'FAILED', 'CANCELLED'); - -- CreateEnum CREATE TYPE "OutboxStatus" AS ENUM ('PENDING', 'PUBLISHED', 'FAILED'); @@ -71,6 +75,7 @@ CREATE TABLE "system_accounts" ( "share_balance" DECIMAL(30,8) NOT NULL DEFAULT 0, "usdt_balance" DECIMAL(30,8) NOT NULL DEFAULT 0, "green_point_balance" DECIMAL(30,8) NOT NULL DEFAULT 0, + "contribution_balance" DECIMAL(30,8) NOT NULL DEFAULT 0, "frozen_share" DECIMAL(30,8) NOT NULL DEFAULT 0, "frozen_usdt" DECIMAL(30,8) NOT NULL DEFAULT 0, "total_inflow" DECIMAL(30,8) NOT NULL DEFAULT 0, @@ -195,118 +200,6 @@ CREATE TABLE "user_wallet_transactions" ( CONSTRAINT "user_wallet_transactions_pkey" PRIMARY KEY ("id") ); --- CreateTable -CREATE TABLE "withdraw_requests" ( - "id" TEXT NOT NULL, - "request_no" TEXT NOT NULL, - "account_sequence" TEXT NOT NULL, - "asset_type" "AssetType" NOT NULL, - "amount" DECIMAL(30,8) NOT NULL, - "fee" DECIMAL(30,8) NOT NULL DEFAULT 0, - "net_amount" DECIMAL(30,8) NOT NULL, - "to_address" TEXT NOT NULL, - "status" "WithdrawStatus" NOT NULL DEFAULT 'PENDING', - "tx_hash" TEXT, - "block_number" BIGINT, - "confirmations" INTEGER NOT NULL DEFAULT 0, - "error_message" TEXT, - "approved_by" TEXT, - "approved_at" TIMESTAMP(3), - "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, - "updated_at" TIMESTAMP(3) NOT NULL, - "completed_at" TIMESTAMP(3), - - CONSTRAINT "withdraw_requests_pkey" PRIMARY KEY ("id") -); - --- CreateTable -CREATE TABLE "deposit_records" ( - "id" TEXT NOT NULL, - "tx_hash" TEXT NOT NULL, - "from_address" TEXT NOT NULL, - "to_address" TEXT NOT NULL, - "asset_type" "AssetType" NOT NULL, - "amount" DECIMAL(30,8) NOT NULL, - "block_number" BIGINT NOT NULL, - "confirmations" INTEGER NOT NULL DEFAULT 0, - "matched_account_seq" TEXT, - "is_processed" BOOLEAN NOT NULL DEFAULT false, - "processed_at" TIMESTAMP(3), - "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, - "updated_at" TIMESTAMP(3) NOT NULL, - - CONSTRAINT "deposit_records_pkey" PRIMARY KEY ("id") -); - --- CreateTable -CREATE TABLE "dex_swap_records" ( - "id" TEXT NOT NULL, - "swap_no" TEXT NOT NULL, - "account_sequence" TEXT NOT NULL, - "from_asset" "AssetType" NOT NULL, - "to_asset" "AssetType" NOT NULL, - "from_amount" DECIMAL(30,8) NOT NULL, - "to_amount" DECIMAL(30,8) NOT NULL, - "exchange_rate" DECIMAL(30,18) NOT NULL, - "slippage" DECIMAL(10,4) NOT NULL DEFAULT 0, - "fee" DECIMAL(30,8) NOT NULL DEFAULT 0, - "status" "WithdrawStatus" NOT NULL DEFAULT 'PENDING', - "tx_hash" TEXT, - "block_number" BIGINT, - "error_message" TEXT, - "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, - "updated_at" TIMESTAMP(3) NOT NULL, - "completed_at" TIMESTAMP(3), - - CONSTRAINT "dex_swap_records_pkey" PRIMARY KEY ("id") -); - --- CreateTable -CREATE TABLE "blockchain_address_bindings" ( - "id" TEXT NOT NULL, - "account_sequence" TEXT NOT NULL, - "kava_address" TEXT NOT NULL, - "is_verified" BOOLEAN NOT NULL DEFAULT false, - "verified_at" TIMESTAMP(3), - "verification_tx_hash" TEXT, - "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, - "updated_at" TIMESTAMP(3) NOT NULL, - - CONSTRAINT "blockchain_address_bindings_pkey" PRIMARY KEY ("id") -); - --- CreateTable -CREATE TABLE "black_hole_contracts" ( - "id" TEXT NOT NULL, - "contract_address" TEXT NOT NULL, - "name" TEXT NOT NULL, - "total_burned" DECIMAL(30,8) NOT NULL DEFAULT 0, - "target_burn" DECIMAL(30,8) NOT NULL, - "remaining_burn" DECIMAL(30,8) NOT NULL, - "is_active" BOOLEAN NOT NULL DEFAULT true, - "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, - "updated_at" TIMESTAMP(3) NOT NULL, - - CONSTRAINT "black_hole_contracts_pkey" PRIMARY KEY ("id") -); - --- CreateTable -CREATE TABLE "burn_to_black_hole_records" ( - "id" TEXT NOT NULL, - "black_hole_id" TEXT NOT NULL, - "amount" DECIMAL(30,8) NOT NULL, - "source_type" "CounterpartyType" NOT NULL, - "source_account_seq" TEXT, - "source_user_id" TEXT, - "source_pool_type" "PoolAccountType", - "tx_hash" TEXT, - "block_number" BIGINT, - "memo" TEXT, - "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, - - CONSTRAINT "burn_to_black_hole_records_pkey" PRIMARY KEY ("id") -); - -- CreateTable CREATE TABLE "fee_configs" ( "id" TEXT NOT NULL, @@ -482,78 +375,6 @@ CREATE INDEX "user_wallet_transactions_reference_id_idx" ON "user_wallet_transac -- CreateIndex CREATE INDEX "user_wallet_transactions_created_at_idx" ON "user_wallet_transactions"("created_at" DESC); --- CreateIndex -CREATE UNIQUE INDEX "withdraw_requests_request_no_key" ON "withdraw_requests"("request_no"); - --- CreateIndex -CREATE INDEX "withdraw_requests_account_sequence_idx" ON "withdraw_requests"("account_sequence"); - --- CreateIndex -CREATE INDEX "withdraw_requests_status_idx" ON "withdraw_requests"("status"); - --- CreateIndex -CREATE INDEX "withdraw_requests_tx_hash_idx" ON "withdraw_requests"("tx_hash"); - --- CreateIndex -CREATE INDEX "withdraw_requests_created_at_idx" ON "withdraw_requests"("created_at" DESC); - --- CreateIndex -CREATE UNIQUE INDEX "deposit_records_tx_hash_key" ON "deposit_records"("tx_hash"); - --- CreateIndex -CREATE INDEX "deposit_records_from_address_idx" ON "deposit_records"("from_address"); - --- CreateIndex -CREATE INDEX "deposit_records_to_address_idx" ON "deposit_records"("to_address"); - --- CreateIndex -CREATE INDEX "deposit_records_matched_account_seq_idx" ON "deposit_records"("matched_account_seq"); - --- CreateIndex -CREATE INDEX "deposit_records_is_processed_idx" ON "deposit_records"("is_processed"); - --- CreateIndex -CREATE INDEX "deposit_records_created_at_idx" ON "deposit_records"("created_at" DESC); - --- CreateIndex -CREATE UNIQUE INDEX "dex_swap_records_swap_no_key" ON "dex_swap_records"("swap_no"); - --- CreateIndex -CREATE INDEX "dex_swap_records_account_sequence_idx" ON "dex_swap_records"("account_sequence"); - --- CreateIndex -CREATE INDEX "dex_swap_records_status_idx" ON "dex_swap_records"("status"); - --- CreateIndex -CREATE INDEX "dex_swap_records_tx_hash_idx" ON "dex_swap_records"("tx_hash"); - --- CreateIndex -CREATE INDEX "dex_swap_records_created_at_idx" ON "dex_swap_records"("created_at" DESC); - --- CreateIndex -CREATE UNIQUE INDEX "blockchain_address_bindings_account_sequence_key" ON "blockchain_address_bindings"("account_sequence"); - --- CreateIndex -CREATE UNIQUE INDEX "blockchain_address_bindings_kava_address_key" ON "blockchain_address_bindings"("kava_address"); - --- CreateIndex -CREATE INDEX "blockchain_address_bindings_kava_address_idx" ON "blockchain_address_bindings"("kava_address"); - --- CreateIndex -CREATE UNIQUE INDEX "black_hole_contracts_contract_address_key" ON "black_hole_contracts"("contract_address"); - --- CreateIndex -CREATE INDEX "burn_to_black_hole_records_black_hole_id_idx" ON "burn_to_black_hole_records"("black_hole_id"); - --- CreateIndex -CREATE INDEX "burn_to_black_hole_records_source_account_seq_idx" ON "burn_to_black_hole_records"("source_account_seq"); - --- CreateIndex -CREATE INDEX "burn_to_black_hole_records_tx_hash_idx" ON "burn_to_black_hole_records"("tx_hash"); - --- CreateIndex -CREATE INDEX "burn_to_black_hole_records_created_at_idx" ON "burn_to_black_hole_records"("created_at" DESC); - -- CreateIndex CREATE UNIQUE INDEX "fee_configs_fee_type_key" ON "fee_configs"("fee_type"); @@ -607,7 +428,3 @@ ALTER TABLE "pool_account_transactions" ADD CONSTRAINT "pool_account_transaction -- AddForeignKey ALTER TABLE "user_wallet_transactions" ADD CONSTRAINT "user_wallet_transactions_user_wallet_id_fkey" FOREIGN KEY ("user_wallet_id") REFERENCES "user_wallets"("id") ON DELETE RESTRICT ON UPDATE CASCADE; - --- AddForeignKey -ALTER TABLE "burn_to_black_hole_records" ADD CONSTRAINT "burn_to_black_hole_records_black_hole_id_fkey" FOREIGN KEY ("black_hole_id") REFERENCES "black_hole_contracts"("id") ON DELETE RESTRICT ON UPDATE CASCADE; - diff --git a/backend/services/mining-wallet-service/prisma/migrations/20260112180000_add_contribution_balance/migration.sql b/backend/services/mining-wallet-service/prisma/migrations/20260112180000_add_contribution_balance/migration.sql deleted file mode 100644 index 3ffea7c6..00000000 --- a/backend/services/mining-wallet-service/prisma/migrations/20260112180000_add_contribution_balance/migration.sql +++ /dev/null @@ -1,2 +0,0 @@ --- AlterTable: 添加贡献值余额字段到系统账户 -ALTER TABLE "system_accounts" ADD COLUMN "contribution_balance" DECIMAL(30,8) NOT NULL DEFAULT 0; diff --git a/backend/services/mining-wallet-service/prisma/migrations/20260112220000_remove_blockchain_tables/migration.sql b/backend/services/mining-wallet-service/prisma/migrations/20260112220000_remove_blockchain_tables/migration.sql deleted file mode 100644 index ae3a216b..00000000 --- a/backend/services/mining-wallet-service/prisma/migrations/20260112220000_remove_blockchain_tables/migration.sql +++ /dev/null @@ -1,27 +0,0 @@ --- Remove KAVA blockchain related tables and enums --- These features are being removed from mining-wallet-service - --- Drop tables in correct order (respecting foreign key constraints) -DROP TABLE IF EXISTS "burn_to_black_hole_records"; -DROP TABLE IF EXISTS "black_hole_contracts"; -DROP TABLE IF EXISTS "blockchain_address_bindings"; -DROP TABLE IF EXISTS "dex_swap_records"; -DROP TABLE IF EXISTS "deposit_records"; -DROP TABLE IF EXISTS "withdraw_requests"; - --- Remove WithdrawStatus enum (check if used elsewhere first) --- Note: PostgreSQL doesn't support DROP TYPE IF EXISTS in older versions --- So we use a DO block to handle the case safely -DO $$ -BEGIN - DROP TYPE IF EXISTS "WithdrawStatus"; -EXCEPTION - WHEN OTHERS THEN NULL; -END $$; - --- Update SystemAccountType enum to remove HOT_WALLET and COLD_WALLET --- This requires recreating the enum, but existing data may use these values --- For safety, we'll just leave the enum as is if there's data - --- Remove BLOCKCHAIN from CounterpartyType enum --- Same consideration - leave as is if data exists diff --git a/backend/services/mining-wallet-service/prisma/migrations/migration_lock.toml b/backend/services/mining-wallet-service/prisma/migrations/migration_lock.toml new file mode 100644 index 00000000..99e4f200 --- /dev/null +++ b/backend/services/mining-wallet-service/prisma/migrations/migration_lock.toml @@ -0,0 +1,3 @@ +# Please do not edit this file manually +# It should be added in your version-control system (i.e. Git) +provider = "postgresql" diff --git a/backend/services/trading-service/prisma/migrations/20260111000000_init/migration.sql b/backend/services/trading-service/prisma/migrations/0001_init/migration.sql similarity index 97% rename from backend/services/trading-service/prisma/migrations/20260111000000_init/migration.sql rename to backend/services/trading-service/prisma/migrations/0001_init/migration.sql index 1d15f8aa..a97cedde 100644 --- a/backend/services/trading-service/prisma/migrations/20260111000000_init/migration.sql +++ b/backend/services/trading-service/prisma/migrations/0001_init/migration.sql @@ -1,3 +1,8 @@ +-- ============================================================================ +-- trading-service 初始化 migration +-- 合并自: 20260111000000_init (只有一个,无需合并) +-- ============================================================================ + -- CreateEnum CREATE TYPE "OutboxStatus" AS ENUM ('PENDING', 'PUBLISHED', 'FAILED'); @@ -109,7 +114,7 @@ CREATE TABLE "circulation_pool_transactions" ( CONSTRAINT "circulation_pool_transactions_pkey" PRIMARY KEY ("id") ); --- CreateTable +-- CreateTable (deprecated, kept for compatibility) CREATE TABLE "pool_transactions" ( "id" TEXT NOT NULL, "type" TEXT NOT NULL, @@ -309,4 +314,3 @@ ALTER TABLE "trading_transactions" ADD CONSTRAINT "trading_transactions_accountS -- AddForeignKey ALTER TABLE "circulation_pool_transactions" ADD CONSTRAINT "circulation_pool_transactions_pool_id_fkey" FOREIGN KEY ("pool_id") REFERENCES "circulation_pools"("id") ON DELETE RESTRICT ON UPDATE CASCADE; - diff --git a/backend/services/trading-service/prisma/migrations/migration_lock.toml b/backend/services/trading-service/prisma/migrations/migration_lock.toml new file mode 100644 index 00000000..99e4f200 --- /dev/null +++ b/backend/services/trading-service/prisma/migrations/migration_lock.toml @@ -0,0 +1,3 @@ +# Please do not edit this file manually +# It should be added in your version-control system (i.e. Git) +provider = "postgresql"