Files

33 lines
1.2 KiB
SQL
Raw Permalink Normal View History

-- +goose Up
-- Stripe customer / subscription linkage + webhook idempotency (company-scoped).
ALTER TABLE companies
ADD COLUMN IF NOT EXISTS stripe_customer_id TEXT;
CREATE UNIQUE INDEX IF NOT EXISTS companies_stripe_customer_id_uidx
ON companies (stripe_customer_id)
WHERE stripe_customer_id IS NOT NULL AND stripe_customer_id <> '';
ALTER TABLE company_plans
ADD COLUMN IF NOT EXISTS stripe_subscription_id TEXT,
ADD COLUMN IF NOT EXISTS stripe_price_id TEXT;
CREATE INDEX IF NOT EXISTS company_plans_stripe_subscription_id_idx
ON company_plans (stripe_subscription_id)
WHERE stripe_subscription_id IS NOT NULL AND stripe_subscription_id <> '';
CREATE TABLE IF NOT EXISTS stripe_webhook_events (
event_id TEXT PRIMARY KEY,
event_type TEXT NOT NULL,
company_id UUID REFERENCES companies(id) ON DELETE SET NULL,
processed_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
-- +goose Down
DROP TABLE IF EXISTS stripe_webhook_events;
DROP INDEX IF EXISTS company_plans_stripe_subscription_id_idx;
ALTER TABLE company_plans DROP COLUMN IF EXISTS stripe_price_id;
ALTER TABLE company_plans DROP COLUMN IF EXISTS stripe_subscription_id;
DROP INDEX IF EXISTS companies_stripe_customer_id_uidx;
ALTER TABLE companies DROP COLUMN IF EXISTS stripe_customer_id;