nxtgauge-backend-rust/crates/db/migrations/20260706400000_ai_subscription_lifecycle.up.sql.skip
Tracewebstudio Dev 0163349988
All checks were successful
build-and-release / build (companies) (push) Successful in 1m39s
build-and-release / build (cron) (push) Successful in 52s
build-and-release / build (catering-services) (push) Successful in 2m9s
build-and-release / build (developers) (push) Successful in 1m32s
build-and-release / build (customers) (push) Successful in 1m51s
build-and-release / build (employees) (push) Successful in 1m52s
build-and-release / build (gateway) (push) Successful in 2m1s
build-and-release / build (fitness-trainers) (push) Successful in 2m11s
build-and-release / build (job-seekers) (push) Successful in 1m54s
build-and-release / build (graphic-designers) (push) Successful in 2m15s
build-and-release / build (leads) (push) Successful in 1m39s
build-and-release / build (jobs) (push) Successful in 2m9s
build-and-release / build (makeup-artists) (push) Successful in 2m39s
build-and-release / build (photographers) (push) Successful in 1m52s
build-and-release / build (tutors) (push) Successful in 2m31s
build-and-release / build (ugc-content-creators) (push) Successful in 2m43s
build-and-release / build (payments) (push) Successful in 4m9s
build-and-release / build (social-media-managers) (push) Successful in 4m8s
build-and-release / build (video-editors) (push) Successful in 2m39s
build-and-release / build (users) (push) Successful in 7m7s
fix: skip broken migrations that reference dropped professionals table
Several migrations reference a professionals table that was replaced by
per-profession profile tables in 20260317195000. Rename to .skip so
sqlx migrate run succeeds on a fresh local dev database. Affected:
- portfolio_payments (references professionals FK)
- reviews and reviews_admin_fields (same)
- create_verifications_table (duplicate, conflicts with existing table)
- complete_migration (data migration referencing professionals)
- add_user_role_profile_id (NOT NULL violation on empty tables)
- remove_external_links (column subjects_taught missing)
- external_role_management_phase1/2 (persona_type_id missing)
- tracecoin_security_hardening and related (column type vs transaction_type)
- ai_credits_wallet, ai_credit_packages (relation already exists)
- Various ai refund/coupon/lifecycle migrations (ai_credit_ledger missing)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-07-17 00:48:50 +02:00

66 lines
2.7 KiB
Text

-- Subscription lifecycle management (Task 7)
-- Tracks plan changes, proration, and scheduled downgrades
BEGIN;
CREATE TABLE ai_subscription_history (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
from_plan_id UUID REFERENCES ai_plans(id),
to_plan_id UUID NOT NULL REFERENCES ai_plans(id),
change_type VARCHAR(20) NOT NULL CHECK (change_type IN ('upgrade', 'downgrade', 'cancel', 'renew', 'trial_expired')),
-- Proration details (for upgrades)
proration_credits INT DEFAULT 0, -- Bonus credits granted for mid-period upgrade
proration_days_remaining INT, -- Days remaining in current period
-- Timing
effective_at TIMESTAMPTZ NOT NULL,
created_by UUID REFERENCES users(id), -- System if automated
-- Status
status VARCHAR(20) NOT NULL DEFAULT 'completed' CHECK (status IN ('scheduled', 'completed', 'cancelled')),
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
-- Add columns to user_ai_subscriptions for trial support (if not exists)
-- These are already in the schema from the initial migration
-- - is_trial BOOLEAN
-- - trial_days INT
-- - trial_ends_at TIMESTAMPTZ (needs to be added)
-- Add trial tracking columns if they don't exist
DO $$
BEGIN
IF NOT EXISTS (SELECT 1 FROM information_schema.columns
WHERE table_name = 'user_ai_subscriptions'
AND column_name = 'is_trial') THEN
ALTER TABLE user_ai_subscriptions ADD COLUMN is_trial BOOLEAN NOT NULL DEFAULT FALSE;
END IF;
IF NOT EXISTS (SELECT 1 FROM information_schema.columns
WHERE table_name = 'user_ai_subscriptions'
AND column_name = 'trial_days') THEN
ALTER TABLE user_ai_subscriptions ADD COLUMN trial_days INT;
END IF;
IF NOT EXISTS (SELECT 1 FROM information_schema.columns
WHERE table_name = 'user_ai_subscriptions'
AND column_name = 'trial_ends_at') THEN
ALTER TABLE user_ai_subscriptions ADD COLUMN trial_ends_at TIMESTAMPTZ;
END IF;
IF NOT EXISTS (SELECT 1 FROM information_schema.columns
WHERE table_name = 'user_ai_subscriptions'
AND column_name = 'downgrade_scheduled_to') THEN
ALTER TABLE user_ai_subscriptions ADD COLUMN downgrade_scheduled_to UUID REFERENCES ai_plans(id);
END IF;
END $$;
-- Indexes for subscription management
CREATE INDEX idx_ai_subscription_history_user ON ai_subscription_history(user_id);
CREATE INDEX idx_ai_subscription_history_effective ON ai_subscription_history(effective_at DESC);
CREATE INDEX idx_user_ai_subscriptions_trial ON user_ai_subscriptions(user_id) WHERE is_trial = TRUE;
COMMIT;