Files
Aether/crates/aether-data/schema/generated/mysql/baseline/005_wallet_billing.sql
fawney19 fce7e959e5 Add multi-database data layer
Introduce aether-data-schema and driver-specific schema generation for Postgres, MySQL, and SQLite.

Split data backends, lifecycle, repositories, and gateway runtime integration across database drivers.

Verified with cargo fmt --all --check, cargo clippy --workspace --all-targets -- -D warnings, and cargo test --workspace.
2026-05-05 18:27:36 +08:00

196 lines
7.3 KiB
SQL

-- 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 wallets (
`id` VARCHAR(64) NOT NULL,
`user_id` VARCHAR(64),
`api_key_id` VARCHAR(64),
`balance` DOUBLE NOT NULL DEFAULT 0,
`gift_balance` DOUBLE NOT NULL DEFAULT 0,
`limit_mode` VARCHAR(64) NOT NULL DEFAULT 'finite',
`currency` VARCHAR(16) NOT NULL DEFAULT 'USD',
`status` VARCHAR(64) NOT NULL DEFAULT 'active',
`total_recharged` DOUBLE NOT NULL DEFAULT 0,
`total_consumed` DOUBLE NOT NULL DEFAULT 0,
`total_refunded` DOUBLE NOT NULL DEFAULT 0,
`total_adjusted` DOUBLE NOT NULL DEFAULT 0,
`created_at` BIGINT NOT NULL,
`updated_at` BIGINT NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY wallets_user_id_key (`user_id`),
UNIQUE KEY wallets_api_key_id_key (`api_key_id`),
KEY wallets_api_key_id_idx (`api_key_id`),
KEY wallets_user_id_idx (`user_id`)
);
CREATE TABLE IF NOT EXISTS wallet_transactions (
`id` VARCHAR(64) NOT NULL,
`wallet_id` VARCHAR(64) NOT NULL,
`category` VARCHAR(64) NOT NULL,
`reason_code` VARCHAR(64) NOT NULL,
`amount` DOUBLE NOT NULL,
`balance_before` DOUBLE NOT NULL,
`balance_after` DOUBLE NOT NULL,
`recharge_balance_before` DOUBLE NOT NULL,
`recharge_balance_after` DOUBLE NOT NULL,
`gift_balance_before` DOUBLE NOT NULL,
`gift_balance_after` DOUBLE NOT NULL,
`link_type` VARCHAR(64),
`link_id` VARCHAR(128),
`operator_id` VARCHAR(64),
`description` LONGTEXT,
`created_at` BIGINT NOT NULL,
PRIMARY KEY (`id`),
KEY idx_wallet_tx_wallet_created (`wallet_id`, `created_at`),
KEY idx_wallet_tx_category_created (`category`, `created_at`),
KEY idx_wallet_tx_reason_created (`reason_code`, `created_at`),
KEY idx_wallet_tx_link (`link_type`, `link_id`),
KEY ix_wallet_transactions_operator_id (`operator_id`)
);
CREATE TABLE IF NOT EXISTS wallet_daily_usage_ledgers (
`id` VARCHAR(64) NOT NULL,
`wallet_id` VARCHAR(64) NOT NULL,
`billing_date` VARCHAR(16) NOT NULL,
`billing_timezone` VARCHAR(64) NOT NULL,
`total_cost_usd` DOUBLE NOT NULL DEFAULT 0,
`total_requests` BIGINT NOT NULL DEFAULT 0,
`input_tokens` BIGINT NOT NULL DEFAULT 0,
`output_tokens` BIGINT NOT NULL DEFAULT 0,
`cache_creation_tokens` BIGINT NOT NULL DEFAULT 0,
`cache_read_tokens` BIGINT NOT NULL DEFAULT 0,
`first_finalized_at` BIGINT,
`last_finalized_at` BIGINT,
`aggregated_at` BIGINT NOT NULL,
`created_at` BIGINT NOT NULL,
`updated_at` BIGINT NOT NULL,
PRIMARY KEY (`id`),
KEY idx_wallet_daily_usage_wallet_date (`wallet_id`, `billing_timezone`, `billing_date`)
);
CREATE TABLE IF NOT EXISTS payment_orders (
`id` VARCHAR(64) NOT NULL,
`order_no` VARCHAR(128) NOT NULL,
`wallet_id` VARCHAR(64) NOT NULL,
`user_id` VARCHAR(64),
`amount_usd` DOUBLE NOT NULL,
`pay_amount` DOUBLE,
`pay_currency` VARCHAR(16),
`exchange_rate` DOUBLE,
`refunded_amount_usd` DOUBLE NOT NULL DEFAULT 0,
`refundable_amount_usd` DOUBLE NOT NULL DEFAULT 0,
`payment_method` VARCHAR(64) NOT NULL,
`gateway_order_id` VARCHAR(128),
`gateway_response` JSON,
`status` VARCHAR(64) NOT NULL DEFAULT 'pending',
`created_at` BIGINT NOT NULL,
`paid_at` BIGINT,
`credited_at` BIGINT,
`expires_at` BIGINT,
PRIMARY KEY (`id`),
UNIQUE KEY uq_payment_orders_order_no (`order_no`),
KEY idx_payment_orders_wallet_created (`wallet_id`, `created_at`),
KEY idx_payment_orders_user_created (`user_id`, `created_at`),
KEY idx_payment_orders_status (`status`),
KEY idx_payment_orders_gateway_order_id (`gateway_order_id`)
);
CREATE TABLE IF NOT EXISTS payment_callbacks (
`id` VARCHAR(64) NOT NULL,
`payment_order_id` VARCHAR(64),
`payment_method` VARCHAR(64) NOT NULL,
`callback_key` VARCHAR(128) NOT NULL,
`order_no` VARCHAR(128),
`gateway_order_id` VARCHAR(128),
`payload_hash` VARCHAR(128),
`signature_valid` TINYINT(1) NOT NULL DEFAULT 0,
`status` VARCHAR(64) NOT NULL DEFAULT 'received',
`payload` JSON,
`error_message` LONGTEXT,
`created_at` BIGINT NOT NULL,
`processed_at` BIGINT,
PRIMARY KEY (`id`),
UNIQUE KEY uq_payment_callbacks_callback_key (`callback_key`),
KEY idx_payment_callbacks_order (`order_no`),
KEY idx_payment_callbacks_gateway_order (`gateway_order_id`),
KEY idx_payment_callbacks_created (`created_at`),
KEY ix_payment_callbacks_payment_order_id (`payment_order_id`)
);
CREATE TABLE IF NOT EXISTS refund_requests (
`id` VARCHAR(64) NOT NULL,
`refund_no` VARCHAR(128) NOT NULL,
`wallet_id` VARCHAR(64) NOT NULL,
`user_id` VARCHAR(64),
`payment_order_id` VARCHAR(64),
`source_type` VARCHAR(64) NOT NULL,
`source_id` VARCHAR(128),
`refund_mode` VARCHAR(64) NOT NULL,
`amount_usd` DOUBLE NOT NULL,
`status` VARCHAR(64) NOT NULL DEFAULT 'pending_approval',
`reason` LONGTEXT,
`requested_by` VARCHAR(64),
`approved_by` VARCHAR(64),
`processed_by` VARCHAR(64),
`gateway_refund_id` VARCHAR(128),
`payout_method` VARCHAR(64),
`payout_reference` VARCHAR(255),
`payout_proof` JSON,
`failure_reason` LONGTEXT,
`idempotency_key` VARCHAR(128),
`created_at` BIGINT NOT NULL,
`updated_at` BIGINT NOT NULL,
`processed_at` BIGINT,
`completed_at` BIGINT,
PRIMARY KEY (`id`),
UNIQUE KEY uq_refund_requests_refund_no (`refund_no`),
UNIQUE KEY uq_refund_requests_idempotency_key (`idempotency_key`),
KEY idx_refund_wallet_created (`wallet_id`, `created_at`),
KEY idx_refund_user_created (`user_id`, `created_at`),
KEY idx_refund_status (`status`),
KEY ix_refund_requests_payment_order_id (`payment_order_id`),
KEY ix_refund_requests_requested_by (`requested_by`),
KEY ix_refund_requests_approved_by (`approved_by`),
KEY ix_refund_requests_processed_by (`processed_by`)
);
CREATE TABLE IF NOT EXISTS redeem_code_batches (
`id` VARCHAR(64) NOT NULL,
`name` VARCHAR(255) NOT NULL,
`amount_usd` DOUBLE NOT NULL,
`currency` VARCHAR(16) NOT NULL DEFAULT 'USD',
`balance_bucket` VARCHAR(64) NOT NULL DEFAULT 'gift',
`total_count` INT NOT NULL,
`status` VARCHAR(64) NOT NULL DEFAULT 'active',
`description` LONGTEXT,
`created_by` VARCHAR(64),
`expires_at` BIGINT,
`created_at` BIGINT NOT NULL,
`updated_at` BIGINT NOT NULL,
PRIMARY KEY (`id`),
KEY idx_redeem_code_batches_status (`status`, `created_at`)
);
CREATE TABLE IF NOT EXISTS redeem_codes (
`id` VARCHAR(64) NOT NULL,
`batch_id` VARCHAR(64) NOT NULL,
`code_hash` VARCHAR(128) NOT NULL,
`code_prefix` VARCHAR(16) NOT NULL,
`code_suffix` VARCHAR(16) NOT NULL,
`status` VARCHAR(64) NOT NULL DEFAULT 'active',
`redeemed_by_user_id` VARCHAR(64),
`redeemed_wallet_id` VARCHAR(64),
`redeemed_payment_order_id` VARCHAR(64),
`redeemed_at` BIGINT,
`disabled_by` VARCHAR(64),
`created_at` BIGINT NOT NULL,
`updated_at` BIGINT NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY uq_redeem_codes_code_hash (`code_hash`),
KEY idx_redeem_codes_batch_created (`batch_id`, `created_at`),
KEY idx_redeem_codes_status (`status`, `updated_at`),
KEY idx_redeem_codes_redeemed_user (`redeemed_by_user_id`, `redeemed_at`),
KEY idx_redeem_codes_redeemed_order (`redeemed_payment_order_id`)
);