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:
parent
e09b839ac0
commit
1a065257d0
32
src/migrations/20250515252526-cr-uniqueness.js
Normal file
32
src/migrations/20250515252526-cr-uniqueness.js
Normal 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,
|
||||
);
|
||||
};
|
Loading…
Reference in New Issue
Block a user