Files
tenant-registry/migrations/0019_align_trial_entitlements.up.sql
sharang 3f4dfa0cdc
ci / shared (push) Successful in 14s
ci / test (push) Successful in 25m31s
ci / image (push) Successful in 37s
fix(trial): a product trial ends with the workspace's trial (#77)
2026-10-15 16:28:01 +00:00

26 lines
1.2 KiB
SQL

-- A product trial started from the catalogue ran its own 14 days instead of
-- ending with the workspace's trial, so a workspace whose trial staff had set
-- to a later day lost the product before its trial ended. Product trials of
-- workspaces still on trial end with that trial again; each change is in the
-- audit log. Rows that end later, preview grants and booked products are not
-- touched.
WITH aligned AS (
UPDATE tenant_products tp
SET expires_at = t.trial_ends_at,
enabled = TRUE,
updated_at = NOW()
FROM tenants t
WHERE tp.tenant_id = t.id
AND t.status = 'trial'
AND t.trial_ends_at > NOW()
AND tp.config ->> 'source' = 'trial'
AND NOT tp.preview
AND tp.expires_at IS NOT NULL
AND tp.expires_at < t.trial_ends_at
RETURNING tp.tenant_id, tp.product, t.trial_ends_at
)
INSERT INTO audit_log (tenant_id, actor_name, actor_type, action, target_id, target_type, target_name, metadata)
SELECT tenant_id, 'migration 0019', 'system', 'entitlement.trial_aligned', tenant_id::text, 'tenant', product,
jsonb_build_object('product', product, 'expires_at', trial_ends_at)
FROM aligned;