rwadurian/backend/services/mining-service/prisma/migrations/0001_init/migration.sql

423 lines
16 KiB
SQL

-- ============================================================================
-- mining-service 初始化 migration
-- 合并自: 20260111000000_init (只有一个,无需合并)
-- ============================================================================
-- 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,
"minuteDistribution" DECIMAL(30,18) NOT NULL,
"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,
"minuteDistribution" 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,
"minuteDistribution" 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 "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
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
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;