Files
events-hub/backend/prisma/migrations/20261105080000_ai_per_customer/migration.sql
T
joshuaandClaude Opus 5.5 59e6b9f03c Hub 2.0 step 6: AI cap and markup per church
The cap and markup move from the licence to the customer (migrated), use
is counted over all the church's installs under one lock per church, and
the licence token carries the church's cap. Settings → AI shows product.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-05 08:55:43 +02:00

20 lines
856 B
SQL

-- Hub 2.0: the AI cap and markup are the church's (counted over all its installs), moved from the
-- licence. Each church takes the cap of its open licence with the highest cap, and that licence's
-- markup override (Hub 1.7 had one Events licence per church, so this is that licence's).
ALTER TABLE "Customer" ADD COLUMN "aiMarkupPercent" INTEGER,
ADD COLUMN "aiMonthlyCapCents" INTEGER NOT NULL DEFAULT 0;
UPDATE "Customer" c
SET "aiMonthlyCapCents" = l."aiMonthlyCapCents", "aiMarkupPercent" = l."aiMarkupPercent"
FROM (
SELECT DISTINCT ON ("customerId") "customerId", "aiMonthlyCapCents", "aiMarkupPercent"
FROM "Licence"
WHERE status <> 'revoked'
ORDER BY "customerId", "aiMonthlyCapCents" DESC, "createdAt" ASC
) l
WHERE c.id = l."customerId";
ALTER TABLE "Licence" DROP COLUMN "aiMarkupPercent",
DROP COLUMN "aiMonthlyCapCents";