-
Notifications
You must be signed in to change notification settings - Fork 14
/
housekeeping.go
72 lines (64 loc) · 1.54 KB
/
housekeeping.go
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
63
64
65
66
67
68
69
70
71
72
package main
import (
"fmt"
"gorm.io/gorm"
)
type HouseKeepingCmd struct {
}
func (c *HouseKeepingCmd) Run(ctx *Context) error {
db, err := gorm.Open(ctx.Dialector, &ctx.Config)
if err != nil {
return err
}
return db.Transaction(func(tx *gorm.DB) error {
// delete all ActorAttributes that are not referenced by an Actor
res := tx.Exec(`
DELETE FROM actor_attributes
WHERE actor_id NOT IN (SELECT id FROM actors)
`)
if res.Error != nil {
return res.Error
}
fmt.Println("deleted", res.RowsAffected, "orphaned actor attributes")
// delete all Person and Service actors that have no status
res = tx.Exec(`
DELETE FROM actors
WHERE id IN (
SELECT id FROM actors
WHERE type IN ('Person', 'Service')
AND id NOT IN (
SELECT actor_id FROM statuses
)
)
`)
if res.Error != nil {
return res.Error
}
fmt.Println("deleted", res.RowsAffected, "actors with no statuses")
// delete all conversations that have no statuses
res = tx.Exec(`
DELETE FROM conversations
WHERE id NOT IN (
SELECT conversation_id FROM statuses
)
`)
if res.Error != nil {
return res.Error
}
fmt.Println("deleted", res.RowsAffected, "orphaned conversations")
// delete all mentions that have no status or actor
res = tx.Exec(`
DELETE FROM status_mentions
WHERE status_id NOT IN (
SELECT id FROM statuses
) OR actor_id NOT IN (
SELECT id FROM actors
)
`)
if res.Error != nil {
return res.Error
}
fmt.Println("deleted", res.RowsAffected, "orphaned mentions")
return nil
})
}