-- 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;