1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-08-13 13:48:59 +02:00

fix: do not allow creating cr for same environment

This commit is contained in:
sjaanus 2025-05-16 10:51:01 +03:00
parent e09b839ac0
commit 1a065257d0
No known key found for this signature in database
GPG Key ID: 20E007C0248BA7FF

View File

@ -0,0 +1,32 @@
exports.up = (db, callback) => {
db.runSql(
`
WITH ranked AS (
SELECT id,
ROW_NUMBER() OVER (
PARTITION BY created_by, project, environment
ORDER BY created_at DESC
) AS rn
FROM change_requests
WHERE state NOT IN ('Applied', 'Cancelled', 'Rejected', 'Scheduled')
)
UPDATE change_requests
SET state = 'Cancelled'
WHERE id IN (
SELECT id FROM ranked WHERE rn > 1
);
CREATE UNIQUE INDEX unique_pending_request_per_user_project_env
ON change_requests (created_by, project, environment)
WHERE state NOT IN ('Applied', 'Cancelled', 'Rejected', 'Scheduled');
`,
callback,
);
};
exports.down = (db, callback) => {
db.runSql(
` DROP INDEX IF EXISTS unique_pending_request_per_user_project_env;`,
callback,
);
};