-- Generated by aether-data-schema from schema/logical/*.toml. -- Do not edit generated files directly; edit logical schema or explicit overrides instead. CREATE TABLE IF NOT EXISTS public.wallets ( id character varying(64) NOT NULL, user_id character varying(64), api_key_id character varying(64), balance double precision DEFAULT 0 NOT NULL, gift_balance double precision DEFAULT 0 NOT NULL, limit_mode character varying(64) DEFAULT 'finite' NOT NULL, currency character varying(16) DEFAULT 'USD' NOT NULL, status character varying(64) DEFAULT 'active' NOT NULL, total_recharged double precision DEFAULT 0 NOT NULL, total_consumed double precision DEFAULT 0 NOT NULL, total_refunded double precision DEFAULT 0 NOT NULL, total_adjusted double precision DEFAULT 0 NOT NULL, created_at bigint NOT NULL, updated_at bigint NOT NULL ); ALTER TABLE ONLY public.wallets ADD CONSTRAINT wallets_pkey PRIMARY KEY (id); ALTER TABLE ONLY public.wallets ADD CONSTRAINT wallets_user_id_key UNIQUE (user_id); ALTER TABLE ONLY public.wallets ADD CONSTRAINT wallets_api_key_id_key UNIQUE (api_key_id); CREATE INDEX IF NOT EXISTS wallets_api_key_id_idx ON public.wallets USING btree (api_key_id); CREATE INDEX IF NOT EXISTS wallets_user_id_idx ON public.wallets USING btree (user_id); CREATE TABLE IF NOT EXISTS public.wallet_transactions ( id character varying(64) NOT NULL, wallet_id character varying(64) NOT NULL, category character varying(64) NOT NULL, reason_code character varying(64) NOT NULL, amount double precision NOT NULL, balance_before double precision NOT NULL, balance_after double precision NOT NULL, recharge_balance_before double precision NOT NULL, recharge_balance_after double precision NOT NULL, gift_balance_before double precision NOT NULL, gift_balance_after double precision NOT NULL, link_type character varying(64), link_id character varying(128), operator_id character varying(64), description text, created_at bigint NOT NULL ); ALTER TABLE ONLY public.wallet_transactions ADD CONSTRAINT wallet_transactions_pkey PRIMARY KEY (id); CREATE INDEX IF NOT EXISTS idx_wallet_tx_wallet_created ON public.wallet_transactions USING btree (wallet_id, created_at); CREATE INDEX IF NOT EXISTS idx_wallet_tx_category_created ON public.wallet_transactions USING btree (category, created_at); CREATE INDEX IF NOT EXISTS idx_wallet_tx_reason_created ON public.wallet_transactions USING btree (reason_code, created_at); CREATE INDEX IF NOT EXISTS idx_wallet_tx_link ON public.wallet_transactions USING btree (link_type, link_id); CREATE INDEX IF NOT EXISTS ix_wallet_transactions_operator_id ON public.wallet_transactions USING btree (operator_id); CREATE TABLE IF NOT EXISTS public.wallet_daily_usage_ledgers ( id character varying(64) NOT NULL, wallet_id character varying(64) NOT NULL, billing_date character varying(16) NOT NULL, billing_timezone character varying(64) NOT NULL, total_cost_usd double precision DEFAULT 0 NOT NULL, total_requests bigint DEFAULT 0 NOT NULL, input_tokens bigint DEFAULT 0 NOT NULL, output_tokens bigint DEFAULT 0 NOT NULL, cache_creation_tokens bigint DEFAULT 0 NOT NULL, cache_read_tokens bigint DEFAULT 0 NOT NULL, first_finalized_at bigint, last_finalized_at bigint, aggregated_at bigint NOT NULL, created_at bigint NOT NULL, updated_at bigint NOT NULL ); ALTER TABLE ONLY public.wallet_daily_usage_ledgers ADD CONSTRAINT wallet_daily_usage_ledgers_pkey PRIMARY KEY (id); CREATE INDEX IF NOT EXISTS idx_wallet_daily_usage_wallet_date ON public.wallet_daily_usage_ledgers USING btree (wallet_id, billing_timezone, billing_date); CREATE TABLE IF NOT EXISTS public.payment_orders ( id character varying(64) NOT NULL, order_no character varying(128) NOT NULL, wallet_id character varying(64) NOT NULL, user_id character varying(64), amount_usd double precision NOT NULL, pay_amount double precision, pay_currency character varying(16), exchange_rate double precision, refunded_amount_usd double precision DEFAULT 0 NOT NULL, refundable_amount_usd double precision DEFAULT 0 NOT NULL, payment_method character varying(64) NOT NULL, gateway_order_id character varying(128), gateway_response jsonb, status character varying(64) DEFAULT 'pending' NOT NULL, created_at bigint NOT NULL, paid_at bigint, credited_at bigint, expires_at bigint ); ALTER TABLE ONLY public.payment_orders ADD CONSTRAINT payment_orders_pkey PRIMARY KEY (id); ALTER TABLE ONLY public.payment_orders ADD CONSTRAINT uq_payment_orders_order_no UNIQUE (order_no); CREATE INDEX IF NOT EXISTS idx_payment_orders_wallet_created ON public.payment_orders USING btree (wallet_id, created_at); CREATE INDEX IF NOT EXISTS idx_payment_orders_user_created ON public.payment_orders USING btree (user_id, created_at); CREATE INDEX IF NOT EXISTS idx_payment_orders_status ON public.payment_orders USING btree (status); CREATE INDEX IF NOT EXISTS idx_payment_orders_gateway_order_id ON public.payment_orders USING btree (gateway_order_id); CREATE TABLE IF NOT EXISTS public.payment_callbacks ( id character varying(64) NOT NULL, payment_order_id character varying(64), payment_method character varying(64) NOT NULL, callback_key character varying(128) NOT NULL, order_no character varying(128), gateway_order_id character varying(128), payload_hash character varying(128), signature_valid boolean DEFAULT false NOT NULL, status character varying(64) DEFAULT 'received' NOT NULL, payload jsonb, error_message text, created_at bigint NOT NULL, processed_at bigint ); ALTER TABLE ONLY public.payment_callbacks ADD CONSTRAINT payment_callbacks_pkey PRIMARY KEY (id); ALTER TABLE ONLY public.payment_callbacks ADD CONSTRAINT uq_payment_callbacks_callback_key UNIQUE (callback_key); CREATE INDEX IF NOT EXISTS idx_payment_callbacks_order ON public.payment_callbacks USING btree (order_no); CREATE INDEX IF NOT EXISTS idx_payment_callbacks_gateway_order ON public.payment_callbacks USING btree (gateway_order_id); CREATE INDEX IF NOT EXISTS idx_payment_callbacks_created ON public.payment_callbacks USING btree (created_at); CREATE INDEX IF NOT EXISTS ix_payment_callbacks_payment_order_id ON public.payment_callbacks USING btree (payment_order_id); CREATE TABLE IF NOT EXISTS public.refund_requests ( id character varying(64) NOT NULL, refund_no character varying(128) NOT NULL, wallet_id character varying(64) NOT NULL, user_id character varying(64), payment_order_id character varying(64), source_type character varying(64) NOT NULL, source_id character varying(128), refund_mode character varying(64) NOT NULL, amount_usd double precision NOT NULL, status character varying(64) DEFAULT 'pending_approval' NOT NULL, reason text, requested_by character varying(64), approved_by character varying(64), processed_by character varying(64), gateway_refund_id character varying(128), payout_method character varying(64), payout_reference character varying(255), payout_proof jsonb, failure_reason text, idempotency_key character varying(128), created_at bigint NOT NULL, updated_at bigint NOT NULL, processed_at bigint, completed_at bigint ); ALTER TABLE ONLY public.refund_requests ADD CONSTRAINT refund_requests_pkey PRIMARY KEY (id); ALTER TABLE ONLY public.refund_requests ADD CONSTRAINT uq_refund_requests_refund_no UNIQUE (refund_no); ALTER TABLE ONLY public.refund_requests ADD CONSTRAINT uq_refund_requests_idempotency_key UNIQUE (idempotency_key); CREATE INDEX IF NOT EXISTS idx_refund_wallet_created ON public.refund_requests USING btree (wallet_id, created_at); CREATE INDEX IF NOT EXISTS idx_refund_user_created ON public.refund_requests USING btree (user_id, created_at); CREATE INDEX IF NOT EXISTS idx_refund_status ON public.refund_requests USING btree (status); CREATE INDEX IF NOT EXISTS ix_refund_requests_payment_order_id ON public.refund_requests USING btree (payment_order_id); CREATE INDEX IF NOT EXISTS ix_refund_requests_requested_by ON public.refund_requests USING btree (requested_by); CREATE INDEX IF NOT EXISTS ix_refund_requests_approved_by ON public.refund_requests USING btree (approved_by); CREATE INDEX IF NOT EXISTS ix_refund_requests_processed_by ON public.refund_requests USING btree (processed_by); CREATE TABLE IF NOT EXISTS public.redeem_code_batches ( id character varying(64) NOT NULL, name character varying(255) NOT NULL, amount_usd double precision NOT NULL, currency character varying(16) DEFAULT 'USD' NOT NULL, balance_bucket character varying(64) DEFAULT 'gift' NOT NULL, total_count integer NOT NULL, status character varying(64) DEFAULT 'active' NOT NULL, description text, created_by character varying(64), expires_at bigint, created_at bigint NOT NULL, updated_at bigint NOT NULL ); ALTER TABLE ONLY public.redeem_code_batches ADD CONSTRAINT redeem_code_batches_pkey PRIMARY KEY (id); CREATE INDEX IF NOT EXISTS idx_redeem_code_batches_status ON public.redeem_code_batches USING btree (status, created_at); CREATE TABLE IF NOT EXISTS public.redeem_codes ( id character varying(64) NOT NULL, batch_id character varying(64) NOT NULL, code_hash character varying(128) NOT NULL, code_prefix character varying(16) NOT NULL, code_suffix character varying(16) NOT NULL, status character varying(64) DEFAULT 'active' NOT NULL, redeemed_by_user_id character varying(64), redeemed_wallet_id character varying(64), redeemed_payment_order_id character varying(64), redeemed_at bigint, disabled_by character varying(64), created_at bigint NOT NULL, updated_at bigint NOT NULL ); ALTER TABLE ONLY public.redeem_codes ADD CONSTRAINT redeem_codes_pkey PRIMARY KEY (id); ALTER TABLE ONLY public.redeem_codes ADD CONSTRAINT uq_redeem_codes_code_hash UNIQUE (code_hash); CREATE INDEX IF NOT EXISTS idx_redeem_codes_batch_created ON public.redeem_codes USING btree (batch_id, created_at); CREATE INDEX IF NOT EXISTS idx_redeem_codes_status ON public.redeem_codes USING btree (status, updated_at); CREATE INDEX IF NOT EXISTS idx_redeem_codes_redeemed_user ON public.redeem_codes USING btree (redeemed_by_user_id, redeemed_at); CREATE INDEX IF NOT EXISTS idx_redeem_codes_redeemed_order ON public.redeem_codes USING btree (redeemed_payment_order_id);