-- CreateTable CREATE TABLE "planting_orders" ( "order_id" BIGSERIAL NOT NULL, "order_no" VARCHAR(50) NOT NULL, "user_id" BIGINT NOT NULL, "tree_count" INTEGER NOT NULL, "total_amount" DECIMAL(20,8) NOT NULL, "selected_province" VARCHAR(10), "selected_city" VARCHAR(10), "province_city_selected_at" TIMESTAMP(3), "province_city_confirmed_at" TIMESTAMP(3), "status" VARCHAR(30) NOT NULL DEFAULT 'CREATED', "pool_injection_batch_id" BIGINT, "pool_injection_scheduled_time" TIMESTAMP(3), "pool_injection_actual_time" TIMESTAMP(3), "pool_injection_tx_hash" VARCHAR(100), "mining_enabled_at" TIMESTAMP(3), "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, "paid_at" TIMESTAMP(3), "fund_allocated_at" TIMESTAMP(3), "updated_at" TIMESTAMP(3) NOT NULL, CONSTRAINT "planting_orders_pkey" PRIMARY KEY ("order_id") ); -- CreateTable CREATE TABLE "fund_allocations" ( "allocation_id" BIGSERIAL NOT NULL, "order_id" BIGINT NOT NULL, "target_type" VARCHAR(50) NOT NULL, "amount" DECIMAL(20,8) NOT NULL, "target_account_id" VARCHAR(100), "metadata" JSONB, "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, CONSTRAINT "fund_allocations_pkey" PRIMARY KEY ("allocation_id") ); -- CreateTable CREATE TABLE "planting_positions" ( "position_id" BIGSERIAL NOT NULL, "user_id" BIGINT NOT NULL, "total_tree_count" INTEGER NOT NULL DEFAULT 0, "effective_tree_count" INTEGER NOT NULL DEFAULT 0, "pending_tree_count" INTEGER NOT NULL DEFAULT 0, "first_mining_start_at" TIMESTAMP(3), "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, "updated_at" TIMESTAMP(3) NOT NULL, CONSTRAINT "planting_positions_pkey" PRIMARY KEY ("position_id") ); -- CreateTable CREATE TABLE "position_province_city_distribution" ( "distribution_id" BIGSERIAL NOT NULL, "user_id" BIGINT NOT NULL, "province_code" VARCHAR(10), "city_code" VARCHAR(10), "tree_count" INTEGER NOT NULL DEFAULT 0, "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, "updated_at" TIMESTAMP(3) NOT NULL, CONSTRAINT "position_province_city_distribution_pkey" PRIMARY KEY ("distribution_id") ); -- CreateTable CREATE TABLE "pool_injection_batches" ( "batch_id" BIGSERIAL NOT NULL, "batch_no" VARCHAR(50) NOT NULL, "start_date" DATE NOT NULL, "end_date" DATE NOT NULL, "order_count" INTEGER NOT NULL DEFAULT 0, "total_amount" DECIMAL(20,8) NOT NULL DEFAULT 0, "status" VARCHAR(20) NOT NULL DEFAULT 'PENDING', "scheduled_injection_time" TIMESTAMP(3), "actual_injection_time" TIMESTAMP(3), "injection_tx_hash" VARCHAR(100), "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, "updated_at" TIMESTAMP(3) NOT NULL, CONSTRAINT "pool_injection_batches_pkey" PRIMARY KEY ("batch_id") ); -- CreateTable CREATE TABLE "planting_events" ( "event_id" BIGSERIAL NOT NULL, "event_type" VARCHAR(50) NOT NULL, "aggregate_id" VARCHAR(100) NOT NULL, "aggregate_type" VARCHAR(50) NOT NULL, "event_data" JSONB NOT NULL, "user_id" BIGINT, "occurred_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, "version" INTEGER NOT NULL DEFAULT 1, CONSTRAINT "planting_events_pkey" PRIMARY KEY ("event_id") ); -- CreateIndex CREATE UNIQUE INDEX "planting_orders_order_no_key" ON "planting_orders"("order_no"); -- CreateIndex CREATE INDEX "planting_orders_user_id_idx" ON "planting_orders"("user_id"); -- CreateIndex CREATE INDEX "planting_orders_order_no_idx" ON "planting_orders"("order_no"); -- CreateIndex CREATE INDEX "planting_orders_status_idx" ON "planting_orders"("status"); -- CreateIndex CREATE INDEX "planting_orders_pool_injection_batch_id_idx" ON "planting_orders"("pool_injection_batch_id"); -- CreateIndex CREATE INDEX "planting_orders_selected_province_selected_city_idx" ON "planting_orders"("selected_province", "selected_city"); -- CreateIndex CREATE INDEX "planting_orders_created_at_idx" ON "planting_orders"("created_at"); -- CreateIndex CREATE INDEX "planting_orders_paid_at_idx" ON "planting_orders"("paid_at"); -- CreateIndex CREATE INDEX "fund_allocations_order_id_idx" ON "fund_allocations"("order_id"); -- CreateIndex CREATE INDEX "fund_allocations_target_type_target_account_id_idx" ON "fund_allocations"("target_type", "target_account_id"); -- CreateIndex CREATE INDEX "fund_allocations_created_at_idx" ON "fund_allocations"("created_at"); -- CreateIndex CREATE UNIQUE INDEX "planting_positions_user_id_key" ON "planting_positions"("user_id"); -- CreateIndex CREATE INDEX "planting_positions_user_id_idx" ON "planting_positions"("user_id"); -- CreateIndex CREATE INDEX "planting_positions_total_tree_count_idx" ON "planting_positions"("total_tree_count"); -- CreateIndex CREATE UNIQUE INDEX "position_province_city_distribution_user_id_province_code_c_key" ON "position_province_city_distribution"("user_id", "province_code", "city_code"); -- CreateIndex CREATE INDEX "position_province_city_distribution_user_id_idx" ON "position_province_city_distribution"("user_id"); -- CreateIndex CREATE INDEX "position_province_city_distribution_province_code_idx" ON "position_province_city_distribution"("province_code"); -- CreateIndex CREATE INDEX "position_province_city_distribution_city_code_idx" ON "position_province_city_distribution"("city_code"); -- CreateIndex CREATE UNIQUE INDEX "pool_injection_batches_batch_no_key" ON "pool_injection_batches"("batch_no"); -- CreateIndex CREATE INDEX "pool_injection_batches_batch_no_idx" ON "pool_injection_batches"("batch_no"); -- CreateIndex CREATE INDEX "pool_injection_batches_start_date_end_date_idx" ON "pool_injection_batches"("start_date", "end_date"); -- CreateIndex CREATE INDEX "pool_injection_batches_status_idx" ON "pool_injection_batches"("status"); -- CreateIndex CREATE INDEX "pool_injection_batches_scheduled_injection_time_idx" ON "pool_injection_batches"("scheduled_injection_time"); -- CreateIndex CREATE INDEX "planting_events_aggregate_type_aggregate_id_idx" ON "planting_events"("aggregate_type", "aggregate_id"); -- CreateIndex CREATE INDEX "planting_events_event_type_idx" ON "planting_events"("event_type"); -- CreateIndex CREATE INDEX "planting_events_user_id_idx" ON "planting_events"("user_id"); -- CreateIndex CREATE INDEX "planting_events_occurred_at_idx" ON "planting_events"("occurred_at"); -- AddForeignKey ALTER TABLE "fund_allocations" ADD CONSTRAINT "fund_allocations_order_id_fkey" FOREIGN KEY ("order_id") REFERENCES "planting_orders"("order_id") ON DELETE RESTRICT ON UPDATE CASCADE; -- AddForeignKey ALTER TABLE "position_province_city_distribution" ADD CONSTRAINT "position_province_city_distribution_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "planting_positions"("user_id") ON DELETE RESTRICT ON UPDATE CASCADE; -- AddForeignKey ALTER TABLE "planting_orders" ADD CONSTRAINT "planting_orders_pool_injection_batch_id_fkey" FOREIGN KEY ("pool_injection_batch_id") REFERENCES "pool_injection_batches"("batch_id") ON DELETE SET NULL ON UPDATE CASCADE;