-- ============================================================================ -- mining-service 初始化 migration -- 合并自: 0001_init, 0002_minute_to_second, 0003_add_system_accounts_and_pending_mining, -- 20250120000001_add_region_to_system_mining_accounts -- ============================================================================ -- CreateEnum CREATE TYPE "PoolAccountType" AS ENUM ('SHARE_POOL', 'BLACK_HOLE_POOL', 'CIRCULATION_POOL'); -- CreateEnum CREATE TYPE "PoolTransactionType" AS ENUM ('MINING_DISTRIBUTE', 'FEE_COLLECT', 'INITIAL_INJECT', 'BURN', 'USER_TRANSFER_IN', 'USER_TRANSFER_OUT', 'TRADE_BUY', 'TRADE_SELL', 'POOL_TRANSFER', 'ADJUSTMENT'); -- CreateEnum CREATE TYPE "OutboxStatus" AS ENUM ('PENDING', 'PUBLISHED', 'FAILED'); -- CreateTable CREATE TABLE "mining_configs" ( "id" TEXT NOT NULL, "totalShares" DECIMAL(30,8) NOT NULL, "distributionPool" DECIMAL(30,8) NOT NULL, "remainingDistribution" DECIMAL(30,8) NOT NULL, "halvingPeriodYears" INTEGER NOT NULL DEFAULT 2, "currentEra" INTEGER NOT NULL DEFAULT 1, "eraStartDate" TIMESTAMP(3) NOT NULL, "secondDistribution" DECIMAL(30,18) NOT NULL, "network_total_contribution" DECIMAL(30, 8) NOT NULL DEFAULT 0, "total_tree_count" INTEGER NOT NULL DEFAULT 0, "contribution_per_tree" DECIMAL(20, 10) NOT NULL DEFAULT 22617, "network_last_synced_at" TIMESTAMP(3), "isActive" BOOLEAN NOT NULL DEFAULT false, "activatedAt" TIMESTAMP(3), "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, "updatedAt" TIMESTAMP(3) NOT NULL, CONSTRAINT "mining_configs_pkey" PRIMARY KEY ("id") ); -- CreateTable CREATE TABLE "mining_eras" ( "id" TEXT NOT NULL, "eraNumber" INTEGER NOT NULL, "startDate" TIMESTAMP(3) NOT NULL, "endDate" TIMESTAMP(3), "initialDistribution" DECIMAL(30,8) NOT NULL, "totalDistributed" DECIMAL(30,8) NOT NULL DEFAULT 0, "secondDistribution" DECIMAL(30,18) NOT NULL, "isActive" BOOLEAN NOT NULL DEFAULT true, "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, CONSTRAINT "mining_eras_pkey" PRIMARY KEY ("id") ); -- CreateTable CREATE TABLE "mining_accounts" ( "id" TEXT NOT NULL, "accountSequence" TEXT NOT NULL, "totalMined" DECIMAL(30,8) NOT NULL DEFAULT 0, "availableBalance" DECIMAL(30,8) NOT NULL DEFAULT 0, "frozenBalance" DECIMAL(30,8) NOT NULL DEFAULT 0, "totalContribution" DECIMAL(30,8) NOT NULL DEFAULT 0, "lastSyncedAt" TIMESTAMP(3), "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, "updatedAt" TIMESTAMP(3) NOT NULL, CONSTRAINT "mining_accounts_pkey" PRIMARY KEY ("id") ); -- CreateTable CREATE TABLE "mining_records" ( "id" TEXT NOT NULL, "accountSequence" TEXT NOT NULL, "miningMinute" TIMESTAMP(3) NOT NULL, "contributionRatio" DECIMAL(30,18) NOT NULL, "totalContribution" DECIMAL(30,8) NOT NULL, "secondDistribution" DECIMAL(30,18) NOT NULL, "minedAmount" DECIMAL(30,18) NOT NULL, "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, CONSTRAINT "mining_records_pkey" PRIMARY KEY ("id") ); -- CreateTable CREATE TABLE "mining_transactions" ( "id" TEXT NOT NULL, "accountSequence" TEXT NOT NULL, "type" TEXT NOT NULL, "amount" DECIMAL(30,8) NOT NULL, "balanceBefore" DECIMAL(30,8) NOT NULL, "balanceAfter" DECIMAL(30,8) NOT NULL, "referenceId" TEXT, "referenceType" TEXT, "counterparty_type" TEXT, "counterparty_account_seq" TEXT, "counterparty_user_id" TEXT, "memo" TEXT, "description" TEXT, "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, CONSTRAINT "mining_transactions_pkey" PRIMARY KEY ("id") ); -- CreateTable: 系统挖矿账户 CREATE TABLE "system_mining_accounts" ( "id" TEXT NOT NULL, "account_type" TEXT NOT NULL, "region_code" TEXT, "name" TEXT NOT NULL, "totalMined" DECIMAL(30, 8) NOT NULL DEFAULT 0, "availableBalance" DECIMAL(30, 8) NOT NULL DEFAULT 0, "totalContribution" DECIMAL(30, 8) NOT NULL DEFAULT 0, "last_synced_at" TIMESTAMP(3), "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, "updated_at" TIMESTAMP(3) NOT NULL, CONSTRAINT "system_mining_accounts_pkey" PRIMARY KEY ("id") ); -- CreateTable: 系统账户挖矿记录 CREATE TABLE "system_mining_records" ( "id" TEXT NOT NULL, "system_account_id" TEXT NOT NULL, "mining_minute" TIMESTAMP(3) NOT NULL, "contribution_ratio" DECIMAL(30, 18) NOT NULL, "total_contribution" DECIMAL(30, 8) NOT NULL, "second_distribution" DECIMAL(30, 18) NOT NULL, "mined_amount" DECIMAL(30, 18) NOT NULL, "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, CONSTRAINT "system_mining_records_pkey" PRIMARY KEY ("id") ); -- CreateTable: 系统账户交易流水 CREATE TABLE "system_mining_transactions" ( "id" TEXT NOT NULL, "system_account_id" TEXT NOT NULL, "type" TEXT NOT NULL, "amount" DECIMAL(30, 8) NOT NULL, "balance_before" DECIMAL(30, 8) NOT NULL, "balance_after" DECIMAL(30, 8) NOT NULL, "reference_id" TEXT, "reference_type" TEXT, "memo" TEXT, "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, CONSTRAINT "system_mining_transactions_pkey" PRIMARY KEY ("id") ); -- CreateTable: 待解锁算力挖矿 CREATE TABLE "pending_contribution_mining" ( "id" BIGSERIAL NOT NULL, "source_adoption_id" BIGINT NOT NULL, "source_account_sequence" VARCHAR(20) NOT NULL, "would_be_account_sequence" VARCHAR(20), "contribution_type" VARCHAR(30) NOT NULL, "amount" DECIMAL(30, 10) NOT NULL, "reason" VARCHAR(200), "effective_date" DATE NOT NULL, "expire_date" DATE NOT NULL, "is_expired" BOOLEAN NOT NULL DEFAULT false, "last_synced_at" TIMESTAMP(3), "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, CONSTRAINT "pending_contribution_mining_pkey" PRIMARY KEY ("id") ); -- CreateTable: 待解锁算力挖矿记录 CREATE TABLE "pending_mining_records" ( "id" BIGSERIAL NOT NULL, "pending_contribution_id" BIGINT NOT NULL, "mining_minute" TIMESTAMP(3) NOT NULL, "source_adoption_id" BIGINT NOT NULL, "source_account_sequence" VARCHAR(20) NOT NULL, "would_be_account_sequence" VARCHAR(20), "contribution_type" VARCHAR(30) NOT NULL, "contribution_amount" DECIMAL(30, 10) NOT NULL, "network_total_contribution" DECIMAL(30, 10) NOT NULL, "contribution_ratio" DECIMAL(30, 18) NOT NULL, "second_distribution" DECIMAL(30, 18) NOT NULL, "mined_amount" DECIMAL(30, 18) NOT NULL, "allocated_to" VARCHAR(20) NOT NULL DEFAULT 'HEADQUARTERS', "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, CONSTRAINT "pending_mining_records_pkey" PRIMARY KEY ("id") ); -- CreateTable CREATE TABLE "mining_reward_allocations" ( "id" BIGSERIAL NOT NULL, "mining_date" DATE NOT NULL, "contribution_record_id" BIGINT NOT NULL, "source_adoption_id" BIGINT NOT NULL, "source_account_sequence" VARCHAR(20) NOT NULL, "owner_account_sequence" VARCHAR(20) NOT NULL, "contribution_type" VARCHAR(30) NOT NULL, "contribution_amount" DECIMAL(30,10) NOT NULL, "network_total_contribution" DECIMAL(30,10) NOT NULL, "contribution_ratio" DECIMAL(30,18) NOT NULL, "daily_mining_pool" DECIMAL(30,10) NOT NULL, "reward_amount" DECIMAL(30,10) NOT NULL, "allocation_status" VARCHAR(20) NOT NULL, "is_unlocked" BOOLEAN NOT NULL, "allocated_to_account_sequence" VARCHAR(20), "allocated_to_system_account" VARCHAR(20), "unlocked_reason" VARCHAR(200), "owner_has_adopted" BOOLEAN NOT NULL, "owner_direct_referral_count" INTEGER NOT NULL, "owner_unlocked_level_depth" INTEGER NOT NULL, "owner_unlocked_bonus_tiers" INTEGER NOT NULL, "remark" VARCHAR(500), "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, CONSTRAINT "mining_reward_allocations_pkey" PRIMARY KEY ("id") ); -- CreateTable CREATE TABLE "daily_mining_reward_summaries" ( "id" BIGSERIAL NOT NULL, "mining_date" DATE NOT NULL, "account_sequence" VARCHAR(20) NOT NULL, "unlocked_reward" DECIMAL(30,10) NOT NULL DEFAULT 0, "pending_reward_to_hq" DECIMAL(30,10) NOT NULL DEFAULT 0, "personal_reward" DECIMAL(30,10) NOT NULL DEFAULT 0, "level_reward" DECIMAL(30,10) NOT NULL DEFAULT 0, "bonus_reward" DECIMAL(30,10) NOT NULL DEFAULT 0, "pending_level_to_hq" DECIMAL(30,10) NOT NULL DEFAULT 0, "pending_bonus_to_hq" DECIMAL(30,10) NOT NULL DEFAULT 0, "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, CONSTRAINT "daily_mining_reward_summaries_pkey" PRIMARY KEY ("id") ); -- CreateTable CREATE TABLE "headquarters_pending_rewards" ( "id" BIGSERIAL NOT NULL, "mining_date" DATE NOT NULL, "would_be_account_sequence" VARCHAR(20) NOT NULL, "source_adoption_id" BIGINT NOT NULL, "source_account_sequence" VARCHAR(20) NOT NULL, "contribution_record_id" BIGINT NOT NULL, "contribution_type" VARCHAR(30) NOT NULL, "contribution_amount" DECIMAL(30,10) NOT NULL, "reward_amount" DECIMAL(30,10) NOT NULL, "reason" VARCHAR(200) NOT NULL, "owner_has_adopted" BOOLEAN NOT NULL, "owner_direct_referral_count" INTEGER NOT NULL, "required_condition" VARCHAR(100) NOT NULL, "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, CONSTRAINT "headquarters_pending_rewards_pkey" PRIMARY KEY ("id") ); -- CreateTable CREATE TABLE "minute_mining_stats" ( "id" TEXT NOT NULL, "minute" TIMESTAMP(3) NOT NULL, "totalContribution" DECIMAL(30,8) NOT NULL, "totalDistributed" DECIMAL(30,18) NOT NULL, "participantCount" INTEGER NOT NULL, "burnAmount" DECIMAL(30,8) NOT NULL DEFAULT 0, "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, CONSTRAINT "minute_mining_stats_pkey" PRIMARY KEY ("id") ); -- CreateTable CREATE TABLE "daily_mining_stats" ( "id" TEXT NOT NULL, "date" DATE NOT NULL, "totalContribution" DECIMAL(30,8) NOT NULL, "totalDistributed" DECIMAL(30,8) NOT NULL, "totalBurned" DECIMAL(30,8) NOT NULL, "participantCount" INTEGER NOT NULL, "avgContributionRate" DECIMAL(10,8) NOT NULL, "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, CONSTRAINT "daily_mining_stats_pkey" PRIMARY KEY ("id") ); -- CreateTable CREATE TABLE "black_holes" ( "id" TEXT NOT NULL, "totalBurned" DECIMAL(30,8) NOT NULL DEFAULT 0, "targetBurn" DECIMAL(30,8) NOT NULL, "remainingBurn" DECIMAL(30,8) NOT NULL, "lastBurnMinute" TIMESTAMP(3), "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, "updatedAt" TIMESTAMP(3) NOT NULL, CONSTRAINT "black_holes_pkey" PRIMARY KEY ("id") ); -- CreateTable CREATE TABLE "burn_records" ( "id" TEXT NOT NULL, "blackHoleId" TEXT NOT NULL, "burnMinute" TIMESTAMP(3) NOT NULL, "burnAmount" DECIMAL(30,18) NOT NULL, "remainingTarget" DECIMAL(30,8) NOT NULL, "source_type" TEXT, "source_account_seq" TEXT, "source_user_id" TEXT, "memo" TEXT, "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, CONSTRAINT "burn_records_pkey" PRIMARY KEY ("id") ); -- CreateTable CREATE TABLE "price_snapshots" ( "id" TEXT NOT NULL, "snapshotTime" TIMESTAMP(3) NOT NULL, "price" DECIMAL(30,18) NOT NULL, "sharePool" DECIMAL(30,8) NOT NULL, "blackHoleAmount" DECIMAL(30,8) NOT NULL, "circulationPool" DECIMAL(30,8) NOT NULL, "effectiveDenominator" DECIMAL(30,8) NOT NULL, "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, CONSTRAINT "price_snapshots_pkey" PRIMARY KEY ("id") ); -- CreateTable CREATE TABLE "pool_accounts" ( "id" TEXT NOT NULL, "pool_type" "PoolAccountType" NOT NULL, "name" TEXT NOT NULL, "balance" DECIMAL(30,8) NOT NULL DEFAULT 0, "totalInflow" DECIMAL(30,8) NOT NULL DEFAULT 0, "totalOutflow" DECIMAL(30,8) NOT NULL DEFAULT 0, "is_active" BOOLEAN NOT NULL DEFAULT true, "description" TEXT, "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, "updated_at" TIMESTAMP(3) NOT NULL, CONSTRAINT "pool_accounts_pkey" PRIMARY KEY ("id") ); -- CreateTable CREATE TABLE "pool_transactions" ( "id" TEXT NOT NULL, "pool_account_id" TEXT NOT NULL, "pool_type" "PoolAccountType" NOT NULL, "transaction_type" "PoolTransactionType" NOT NULL, "amount" DECIMAL(30,8) NOT NULL, "balance_before" DECIMAL(30,8) NOT NULL, "balance_after" DECIMAL(30,8) NOT NULL, "counterparty_type" TEXT, "counterparty_account_seq" TEXT, "counterparty_user_id" TEXT, "counterparty_pool_type" "PoolAccountType", "reference_id" TEXT, "reference_type" TEXT, "tx_hash" TEXT, "memo" TEXT, "metadata" JSONB, "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, CONSTRAINT "pool_transactions_pkey" PRIMARY KEY ("id") ); -- CreateTable CREATE TABLE "outbox_events" ( "id" TEXT NOT NULL, "aggregate_type" TEXT NOT NULL, "aggregate_id" TEXT NOT NULL, "event_type" TEXT NOT NULL, "payload" JSONB NOT NULL, "topic" TEXT NOT NULL DEFAULT 'mining.events', "key" TEXT, "status" "OutboxStatus" NOT NULL DEFAULT 'PENDING', "retry_count" INTEGER NOT NULL DEFAULT 0, "max_retries" INTEGER NOT NULL DEFAULT 5, "last_error" TEXT, "published_at" TIMESTAMP(3), "next_retry_at" TIMESTAMP(3), "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, CONSTRAINT "outbox_events_pkey" PRIMARY KEY ("id") ); -- CreateIndex CREATE UNIQUE INDEX "mining_eras_eraNumber_key" ON "mining_eras"("eraNumber"); -- CreateIndex CREATE UNIQUE INDEX "mining_accounts_accountSequence_key" ON "mining_accounts"("accountSequence"); -- CreateIndex CREATE INDEX "mining_accounts_totalContribution_idx" ON "mining_accounts"("totalContribution" DESC); -- CreateIndex CREATE INDEX "mining_records_miningMinute_idx" ON "mining_records"("miningMinute"); -- CreateIndex CREATE UNIQUE INDEX "mining_records_accountSequence_miningMinute_key" ON "mining_records"("accountSequence", "miningMinute"); -- CreateIndex CREATE INDEX "mining_transactions_accountSequence_createdAt_idx" ON "mining_transactions"("accountSequence", "createdAt" DESC); -- CreateIndex CREATE INDEX "mining_transactions_type_idx" ON "mining_transactions"("type"); -- CreateIndex CREATE INDEX "mining_transactions_counterparty_account_seq_idx" ON "mining_transactions"("counterparty_account_seq"); -- CreateIndex CREATE INDEX "mining_transactions_counterparty_user_id_idx" ON "mining_transactions"("counterparty_user_id"); -- CreateIndex: system_mining_accounts CREATE UNIQUE INDEX "system_mining_accounts_account_type_region_code_key" ON "system_mining_accounts"("account_type", "region_code"); CREATE INDEX "system_mining_accounts_totalContribution_idx" ON "system_mining_accounts"("totalContribution" DESC); CREATE INDEX "system_mining_accounts_account_type_idx" ON "system_mining_accounts"("account_type"); CREATE INDEX "system_mining_accounts_region_code_idx" ON "system_mining_accounts"("region_code"); -- CreateIndex: system_mining_records CREATE UNIQUE INDEX "system_mining_records_system_account_id_mining_minute_key" ON "system_mining_records"("system_account_id", "mining_minute"); CREATE INDEX "system_mining_records_mining_minute_idx" ON "system_mining_records"("mining_minute"); -- CreateIndex: system_mining_transactions CREATE INDEX "system_mining_transactions_system_account_id_created_at_idx" ON "system_mining_transactions"("system_account_id", "created_at" DESC); -- CreateIndex: pending_contribution_mining CREATE UNIQUE INDEX "pending_contribution_mining_source_adoption_id_would_be_acco_key" ON "pending_contribution_mining"("source_adoption_id", "would_be_account_sequence", "contribution_type"); CREATE INDEX "pending_contribution_mining_would_be_account_sequence_idx" ON "pending_contribution_mining"("would_be_account_sequence"); CREATE INDEX "pending_contribution_mining_contribution_type_idx" ON "pending_contribution_mining"("contribution_type"); CREATE INDEX "pending_contribution_mining_is_expired_idx" ON "pending_contribution_mining"("is_expired"); -- CreateIndex: pending_mining_records CREATE UNIQUE INDEX "pending_mining_records_pending_contribution_id_mining_minute_key" ON "pending_mining_records"("pending_contribution_id", "mining_minute"); CREATE INDEX "pending_mining_records_mining_minute_idx" ON "pending_mining_records"("mining_minute"); CREATE INDEX "pending_mining_records_source_account_sequence_idx" ON "pending_mining_records"("source_account_sequence"); CREATE INDEX "pending_mining_records_would_be_account_sequence_idx" ON "pending_mining_records"("would_be_account_sequence"); -- CreateIndex CREATE INDEX "mining_reward_allocations_mining_date_idx" ON "mining_reward_allocations"("mining_date"); -- CreateIndex CREATE INDEX "mining_reward_allocations_owner_account_sequence_mining_dat_idx" ON "mining_reward_allocations"("owner_account_sequence", "mining_date"); -- CreateIndex CREATE INDEX "mining_reward_allocations_source_account_sequence_idx" ON "mining_reward_allocations"("source_account_sequence"); -- CreateIndex CREATE INDEX "mining_reward_allocations_source_adoption_id_idx" ON "mining_reward_allocations"("source_adoption_id"); -- CreateIndex CREATE INDEX "mining_reward_allocations_allocation_status_idx" ON "mining_reward_allocations"("allocation_status"); -- CreateIndex CREATE INDEX "mining_reward_allocations_contribution_record_id_idx" ON "mining_reward_allocations"("contribution_record_id"); -- CreateIndex CREATE INDEX "daily_mining_reward_summaries_mining_date_idx" ON "daily_mining_reward_summaries"("mining_date"); -- CreateIndex CREATE INDEX "daily_mining_reward_summaries_account_sequence_idx" ON "daily_mining_reward_summaries"("account_sequence"); -- CreateIndex CREATE UNIQUE INDEX "daily_mining_reward_summaries_mining_date_account_sequence_key" ON "daily_mining_reward_summaries"("mining_date", "account_sequence"); -- CreateIndex CREATE INDEX "headquarters_pending_rewards_mining_date_idx" ON "headquarters_pending_rewards"("mining_date"); -- CreateIndex CREATE INDEX "headquarters_pending_rewards_would_be_account_sequence_idx" ON "headquarters_pending_rewards"("would_be_account_sequence"); -- CreateIndex CREATE INDEX "headquarters_pending_rewards_source_adoption_id_idx" ON "headquarters_pending_rewards"("source_adoption_id"); -- CreateIndex CREATE UNIQUE INDEX "minute_mining_stats_minute_key" ON "minute_mining_stats"("minute"); -- CreateIndex CREATE INDEX "minute_mining_stats_minute_idx" ON "minute_mining_stats"("minute" DESC); -- CreateIndex CREATE UNIQUE INDEX "daily_mining_stats_date_key" ON "daily_mining_stats"("date"); -- CreateIndex CREATE INDEX "burn_records_burnMinute_idx" ON "burn_records"("burnMinute"); -- CreateIndex CREATE INDEX "burn_records_source_account_seq_idx" ON "burn_records"("source_account_seq"); -- CreateIndex CREATE UNIQUE INDEX "burn_records_blackHoleId_burnMinute_key" ON "burn_records"("blackHoleId", "burnMinute"); -- CreateIndex CREATE UNIQUE INDEX "price_snapshots_snapshotTime_key" ON "price_snapshots"("snapshotTime"); -- CreateIndex CREATE INDEX "price_snapshots_snapshotTime_idx" ON "price_snapshots"("snapshotTime" DESC); -- CreateIndex CREATE UNIQUE INDEX "pool_accounts_pool_type_key" ON "pool_accounts"("pool_type"); -- CreateIndex CREATE INDEX "pool_accounts_pool_type_idx" ON "pool_accounts"("pool_type"); -- CreateIndex CREATE INDEX "pool_transactions_pool_account_id_created_at_idx" ON "pool_transactions"("pool_account_id", "created_at" DESC); -- CreateIndex CREATE INDEX "pool_transactions_pool_type_transaction_type_idx" ON "pool_transactions"("pool_type", "transaction_type"); -- CreateIndex CREATE INDEX "pool_transactions_counterparty_account_seq_idx" ON "pool_transactions"("counterparty_account_seq"); -- CreateIndex CREATE INDEX "pool_transactions_counterparty_user_id_idx" ON "pool_transactions"("counterparty_user_id"); -- CreateIndex CREATE INDEX "pool_transactions_reference_id_idx" ON "pool_transactions"("reference_id"); -- CreateIndex CREATE INDEX "pool_transactions_created_at_idx" ON "pool_transactions"("created_at" DESC); -- CreateIndex CREATE INDEX "outbox_events_status_idx" ON "outbox_events"("status"); -- CreateIndex CREATE INDEX "outbox_events_next_retry_at_idx" ON "outbox_events"("next_retry_at"); -- CreateIndex CREATE INDEX "outbox_events_created_at_idx" ON "outbox_events"("created_at"); -- AddForeignKey ALTER TABLE "mining_records" ADD CONSTRAINT "mining_records_accountSequence_fkey" FOREIGN KEY ("accountSequence") REFERENCES "mining_accounts"("accountSequence") ON DELETE RESTRICT ON UPDATE CASCADE; -- AddForeignKey ALTER TABLE "mining_transactions" ADD CONSTRAINT "mining_transactions_accountSequence_fkey" FOREIGN KEY ("accountSequence") REFERENCES "mining_accounts"("accountSequence") ON DELETE RESTRICT ON UPDATE CASCADE; -- AddForeignKey: system_mining_records ALTER TABLE "system_mining_records" ADD CONSTRAINT "system_mining_records_system_account_id_fkey" FOREIGN KEY ("system_account_id") REFERENCES "system_mining_accounts"("id") ON DELETE RESTRICT ON UPDATE CASCADE; -- AddForeignKey: system_mining_transactions ALTER TABLE "system_mining_transactions" ADD CONSTRAINT "system_mining_transactions_system_account_id_fkey" FOREIGN KEY ("system_account_id") REFERENCES "system_mining_accounts"("id") ON DELETE RESTRICT ON UPDATE CASCADE; -- AddForeignKey: pending_mining_records ALTER TABLE "pending_mining_records" ADD CONSTRAINT "pending_mining_records_pending_contribution_id_fkey" FOREIGN KEY ("pending_contribution_id") REFERENCES "pending_contribution_mining"("id") ON DELETE RESTRICT ON UPDATE CASCADE; -- AddForeignKey ALTER TABLE "burn_records" ADD CONSTRAINT "burn_records_blackHoleId_fkey" FOREIGN KEY ("blackHoleId") REFERENCES "black_holes"("id") ON DELETE RESTRICT ON UPDATE CASCADE; -- AddForeignKey ALTER TABLE "pool_transactions" ADD CONSTRAINT "pool_transactions_pool_account_id_fkey" FOREIGN KEY ("pool_account_id") REFERENCES "pool_accounts"("id") ON DELETE RESTRICT ON UPDATE CASCADE; -- 初始化系统账户 (无 regionCode 的汇总账户) INSERT INTO "system_mining_accounts" ("id", "account_type", "region_code", "name", "totalMined", "availableBalance", "totalContribution", "updated_at") VALUES (gen_random_uuid(), 'OPERATION', NULL, '运营账户', 0, 0, 0, NOW()), (gen_random_uuid(), 'HEADQUARTERS', NULL, '总部账户', 0, 0, 0, NOW()) ON CONFLICT ("account_type", "region_code") DO NOTHING;