nxtgauge-backend-rust/crates/db/migrations/20260703210000_ai_credits_wallet.down.sql
Ashwin Kumar Sivakumar da9be7f6a3
Some checks failed
build-and-release / build (cron) (push) Successful in 1m10s
build-and-release / build (developers) (push) Successful in 1m40s
build-and-release / build (catering-services) (push) Successful in 1m58s
build-and-release / build (employees) (push) Successful in 2m19s
build-and-release / build (companies) (push) Successful in 2m36s
build-and-release / build (customers) (push) Successful in 2m45s
build-and-release / build (gateway) (push) Successful in 1m11s
build-and-release / build (jobs) (push) Successful in 40s
build-and-release / build (fitness-trainers) (push) Successful in 2m28s
build-and-release / build (graphic-designers) (push) Successful in 2m14s
build-and-release / build (payments) (push) Successful in 1m48s
build-and-release / build (job-seekers) (push) Successful in 2m57s
build-and-release / build (makeup-artists) (push) Successful in 2m49s
build-and-release / build (photographers) (push) Successful in 2m43s
backend-integration-tests / ai-credits (push) Failing after 31s
build-and-release / build (social-media-managers) (push) Successful in 2m58s
build-and-release / build (tutors) (push) Successful in 2m47s
build-and-release / build (ugc-content-creators) (push) Successful in 2m39s
build-and-release / build (users) (push) Has been cancelled
build-and-release / build (video-editors) (push) Has been cancelled
feat(db,ci): fix missing ai_credits tables, wire integration tests to CI
Re-enables and corrects 20260703210000_ai_credits_wallet, which had been
disabled (.up.sql.skip) in commit 0163349 because 'relation already
exists' aborted the original CREATE TABLE script on its very first
table (ai_plans) - silently leaving ai_credit_ledger and
ai_reservation_holds never created, despite the wallet system being
actively wired into apps/users/handlers/ai_credits.rs, apps/payments,
apps/cron with real subscription rows already in prod.

Rewrote the migration to be idempotent: ADD COLUMN IF NOT EXISTS to
bring ai_plans/user_ai_subscriptions/ai_feature_costs/ai_usage_logs up
to the full column set crates/db/src/models/ai_credits.rs expects
(safe zero/default backfills, no data loss - verified against the 3
existing subscription rows), and CREATE TABLE IF NOT EXISTS for the 2
genuinely-missing tables. down.sql updated to only reverse what this
corrected version actually adds, not drop tables that predate it.

Applied directly to the live database (verified schema + existing rows
intact afterward).

Also wires crates/db/tests/ai_credits.rs + ai_credits_reaper.rs to
Forgejo Actions CI (docs/LIVE_SERVER_RUNBOOK.md step 6):
- New TEST_DATABASE_URL repo secret, pointing at a dedicated
  nxtgauge_test database (created fresh, schema mirrored from live prod
  via pg_dump --schema-only - never runs against the real nxtgauge DB).
- .forgejo/workflows/test.yaml: runs on push to main/high-performance,
  skips when crates/db/ isn't touched, cargo test -p db --test
  ai_credits --test ai_credits_reaper -- --test-threads=1.

Also commits docs/openapi.wallet-holds.json (hand-written minimal
OpenAPI spec used for schemathesis fuzzing in step 4) and gitignores
the local .schemathesis/ cache directory from that run.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-13 17:31:22 +05:30

48 lines
2.5 KiB
PL/PgSQL

BEGIN;
DROP INDEX IF EXISTS idx_ai_reservation_holds_status_expires;
DROP INDEX IF EXISTS idx_ai_reservation_holds_wallet_id;
DROP INDEX IF EXISTS idx_ai_credit_ledger_reference;
DROP INDEX IF EXISTS idx_ai_credit_ledger_wallet_id;
DROP TABLE IF EXISTS ai_reservation_holds;
DROP TABLE IF EXISTS ai_credit_ledger;
-- NOTE: ai_plans, user_ai_subscriptions, ai_feature_costs, and ai_usage_logs
-- are NOT dropped here (unlike the original version of this file) -- they
-- predate this migration (created by an earlier, separate schema pass) and
-- hold real production subscription data. This down-migration only reverses
-- what the corresponding up.sql actually added: the 2 new tables/indexes and
-- the columns/constraint below.
ALTER TABLE user_ai_subscriptions DROP CONSTRAINT IF EXISTS chk_ai_wallet_nonnegative_v2;
ALTER TABLE ai_plans DROP COLUMN IF EXISTS daily_credit_limit;
ALTER TABLE ai_plans DROP COLUMN IF EXISTS is_trial;
ALTER TABLE ai_plans DROP COLUMN IF EXISTS trial_days;
ALTER TABLE user_ai_subscriptions DROP COLUMN IF EXISTS bonus_credits_total;
ALTER TABLE user_ai_subscriptions DROP COLUMN IF EXISTS bonus_credits_used;
ALTER TABLE user_ai_subscriptions DROP COLUMN IF EXISTS reserved_credits;
ALTER TABLE user_ai_subscriptions DROP COLUMN IF EXISTS locked_credits;
ALTER TABLE user_ai_subscriptions DROP COLUMN IF EXISTS lifetime_purchased_credits;
ALTER TABLE user_ai_subscriptions DROP COLUMN IF EXISTS lifetime_used_credits;
ALTER TABLE user_ai_subscriptions DROP COLUMN IF EXISTS daily_credits_used;
ALTER TABLE user_ai_subscriptions DROP COLUMN IF EXISTS daily_usage_date;
ALTER TABLE user_ai_subscriptions DROP COLUMN IF EXISTS purchased_credits_expire_at;
ALTER TABLE user_ai_subscriptions DROP COLUMN IF EXISTS billing_cycle;
ALTER TABLE user_ai_subscriptions DROP COLUMN IF EXISTS auto_renew;
ALTER TABLE ai_feature_costs DROP COLUMN IF EXISTS min_plan_code;
ALTER TABLE ai_feature_costs DROP COLUMN IF EXISTS priority;
ALTER TABLE ai_feature_costs DROP COLUMN IF EXISTS timeout_ms;
ALTER TABLE ai_feature_costs DROP COLUMN IF EXISTS rate_limit_per_minute;
ALTER TABLE ai_feature_costs DROP COLUMN IF EXISTS retry_policy;
ALTER TABLE ai_feature_costs DROP COLUMN IF EXISTS fallback_model;
ALTER TABLE ai_usage_logs DROP COLUMN IF EXISTS cached_tokens;
ALTER TABLE ai_usage_logs DROP COLUMN IF EXISTS provider_reported_cost;
ALTER TABLE ai_usage_logs DROP COLUMN IF EXISTS internal_cost;
ALTER TABLE ai_usage_logs DROP COLUMN IF EXISTS credit_unit_price_at_time;
COMMIT;