-- Pro billing behind a provider port: Mercado Pago (AR) or Stripe (the rest). -- Columns are renamed, not dropped, so existing Mercado Pago rows keep their ids. -- CreateEnum CREATE TYPE "BillingProviderId" AS ENUM ('MERCADO_PAGO', 'STRIPE'); -- billing_subscriptions: provider + provider-neutral ids ALTER TABLE "billing_subscriptions" ADD COLUMN "provider" "BillingProviderId" NOT NULL DEFAULT 'MERCADO_PAGO'; ALTER TABLE "billing_subscriptions" ADD COLUMN "checkout_ref" TEXT; ALTER TABLE "billing_subscriptions" RENAME COLUMN "mp_preapproval_id" TO "external_id"; ALTER INDEX "billing_subscriptions_mpPreapprovalId_key" RENAME TO "billing_subscriptions_externalId_key"; CREATE UNIQUE INDEX "billing_subscriptions_checkoutRef_key" ON "billing_subscriptions"("checkout_ref"); -- billing_charges: provider-neutral id and normalized status ALTER TABLE "billing_charges" RENAME COLUMN "mp_authorized_payment_id" TO "external_id"; ALTER INDEX "billing_charges_mpAuthorizedPaymentId_key" RENAME TO "billing_charges_externalId_key"; UPDATE "billing_charges" SET "status" = CASE WHEN "status" = 'approved' THEN 'approved' WHEN "status" IN ('rejected', 'cancelled', 'refunded', 'charged_back') THEN 'rejected' ELSE 'pending' END; -- billing_webhook_events: dedupe per sending account (`mp:` or `stripe`) ALTER TABLE "billing_webhook_events" RENAME COLUMN "country_code" TO "source"; UPDATE "billing_webhook_events" SET "source" = 'mp:' || "source";