diff --git a/apps/backend/prisma/migrations/20250820175830_grant_internal_users_team_admin/migration.sql b/apps/backend/prisma/migrations/20250820175830_grant_internal_users_team_admin/migration.sql new file mode 100644 index 000000000..0a3806d9f --- /dev/null +++ b/apps/backend/prisma/migrations/20250820175830_grant_internal_users_team_admin/migration.sql @@ -0,0 +1,22 @@ +-- Grant team_admin permission to all users in the "internal" project for all teams they belong to +INSERT INTO "TeamMemberDirectPermission" ("id", "tenancyId", "teamMemberId", "permissionDbId", "createdAt", "updatedAt") +SELECT + gen_random_uuid() AS "id", + tm."tenancyId", + tm."teamMemberId", + p."dbId" AS "permissionDbId", + CURRENT_TIMESTAMP AS "createdAt", + CURRENT_TIMESTAMP AS "updatedAt" +FROM "TeamMember" tm +INNER JOIN "ProjectUser" pu ON tm."tenancyId" = pu."tenancyId" AND tm."projectUserId" = pu."projectUserId" +INNER JOIN "Project" proj ON pu."tenancyId" = proj."tenancyId" AND pu."projectId" = proj."id" +INNER JOIN "Permission" p ON p."projectConfigId" = proj."configId" AND p."id" = 'team_admin' AND p."scope" = 'TEAM' +WHERE proj."id" = 'internal' + AND NOT EXISTS ( + -- Don't create duplicate permissions + SELECT 1 + FROM "TeamMemberDirectPermission" tmdp + WHERE tmdp."tenancyId" = tm."tenancyId" + AND tmdp."teamMemberId" = tm."teamMemberId" + AND tmdp."permissionDbId" = p."dbId" + ); \ No newline at end of file