Skip to content

Commit 83f909e

Browse files
committed
feat: Added migrations for removing duplication of tags
1 parent d72665a commit 83f909e

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
exports.up = async function (knex) {
2+
await knex.raw(`
3+
WITH ranked AS (
4+
SELECT
5+
id,
6+
row_number() OVER (
7+
PARTITION BY event_pubkey, event_kind, jsonb_build_array(COALESCE(event_deduplication->>0, ''))
8+
ORDER BY event_created_at DESC, event_id ASC
9+
) AS row_rank
10+
FROM events
11+
WHERE event_kind >= 30000
12+
AND event_kind < 40000
13+
)
14+
DELETE FROM events AS e
15+
USING ranked AS r
16+
WHERE e.id = r.id
17+
AND r.row_rank > 1;
18+
`)
19+
20+
await knex.raw(`
21+
UPDATE events
22+
SET event_deduplication = jsonb_build_array(COALESCE(event_deduplication->>0, ''))
23+
WHERE event_kind >= 30000
24+
AND event_kind < 40000
25+
AND event_deduplication IS DISTINCT FROM jsonb_build_array(COALESCE(event_deduplication->>0, ''));
26+
`)
27+
}
28+
29+
exports.down = async function () {
30+
// Irreversible data migration.
31+
}

0 commit comments

Comments
 (0)