Skip to content

Commit

Permalink
Use ctid instead of createdAt to determine which rows are deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
oana-lolea committed Dec 10, 2024
1 parent 2b2b34e commit dd6353c
Showing 1 changed file with 4 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,15 @@
exports.up = async function (knex) {
return knex
.raw(
// Keep only one active walletAddressKey (the most recent one)
// Keep only one active walletAddressKey (the most recent row)
`DELETE FROM "walletAddressKeys" w
WHERE revoked = false
AND w."createdAt" = (
SELECT "createdAt"
AND ctid NOT IT (
SELECT MIN(ctid)
FROM "walletAddressKeys"
WHERE revoked = false
AND kid = w.kid
ORDER BY "createdAt" DESC
LIMIT 1
)
/*
if there are multiple keys that have the most recent "createdAt",
we keep just one of them and delete the rest
*/
AND w.id <> (
SELECT id
FROM "walletAddressKeys"
WHERE revoked = false
AND kid = w.kid
ORDER BY "createdAt" DESC
LIMIT 1 OFFSET 1
);
`
)`
)
.then(() => {
return knex.raw(`
Expand Down

0 comments on commit dd6353c

Please sign in to comment.