1-- Teardown claims used to bump updated_at past the link's revoke or publish-only write, so a
2-- failed deprovision now reads as an unfinished relink. Give those rows the updated_at the
3-- current write rule would have left. Touches only withdrawn links' live tunnels; idempotent.
4UPDATE "relay_managed_endpoint_allocations" AS a
5SET "updated_at" = l."updated_at"
6FROM "relay_environment_links" AS l
7WHERE l."user_id" = a."user_id"
8 AND l."environment_id" = a."environment_id"
9 AND a."tunnel_id" IS NOT NULL
10 AND a."tunnel_released_at" IS NULL
11 AND (l."revoked_at" IS NOT NULL OR l."managed_tunnels_enabled" = false)
12 AND l."updated_at" < a."updated_at";