/
merge-all-dupe-names.ts
62 lines (59 loc) · 1.58 KB
/
merge-all-dupe-names.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { mergeGroups } from 'merge-groups'
import { runScript } from 'run-script'
import { SupabaseDirectClient } from 'shared/supabase/init'
const mergeAllDupes = async (pg: SupabaseDirectClient, firestore: any) => {
const dupeNames = await pg.manyOrNone<{
name_fts: string
slug: string
id: string
importance_score: number
rank: number
}>(
`SELECT
name_fts, name, slug, id, importance_score,
ROW_NUMBER() OVER(PARTITION BY name_fts ORDER BY importance_score DESC, total_members DESC) AS rank
FROM groups
WHERE
privacy_status = 'public'
and name_fts in (
select name_fts
from groups
where privacy_status = 'public'
and name_fts != ''
and name not in (
'Anime',
'Animals',
'Animation',
'Avatars',
'Avatar',
'Disney+',
'Disney',
'Curling',
'Curl',
'Musicals',
'Personal',
'Personality',
'Production',
'Productivity',
'Products',
'tests'
)
group by name_fts
having count(*) > 1
)`
)
let top = dupeNames[0]
for (const group of dupeNames) {
if (group.rank == 1) {
top = group
} else if (group.name_fts == top.name_fts) {
console.log('merge', group.slug, top.slug)
await mergeGroups(pg, firestore, group.slug, top.slug)
}
}
}
if (require.main === module) {
runScript(async ({ pg, firestore }) => {
await mergeAllDupes(pg, firestore)
})
}