Repository navigation
Expand file tree
/
Copy pathchatfiles.go
More file actions
59 lines (52 loc) · 1.49 KB
/
Copy pathchatfiles.go
File metadata and controls
59 lines (52 loc) · 1.49 KB
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
package database
import (
"context"
"golang.org/x/xerrors"
)
type (
DeleteOldChatFilesParams = GetOldUnlinkedChatFileIDsParams
LinkChatFilesParams = LinkChatFilesAfterLockParams
)
func (q *sqlQuerier) LinkChatFiles(ctx context.Context, arg LinkChatFilesParams) (int32, error) {
var rejected int32
err := q.InTx(func(tx Store) error {
if _, err := tx.LockChatByID(ctx, arg.ChatID); err != nil {
return xerrors.Errorf("lock chat: %w", err)
}
var err error
rejected, err = tx.LinkChatFilesAfterLock(ctx, arg)
if err != nil {
return xerrors.Errorf("link chat files after lock: %w", err)
}
return nil
}, DefaultTXOptions().WithID("link_chat_files"))
if err != nil {
return 0, err
}
return rejected, nil
}
func (q *sqlQuerier) DeleteOldChatFiles(ctx context.Context, arg DeleteOldChatFilesParams) (int64, error) {
// Recheck candidates because links may commit during row-lock waits.
var deleted int64
err := q.InTx(func(tx Store) error {
ids, err := tx.GetOldUnlinkedChatFileIDs(ctx, arg)
if err != nil {
return xerrors.Errorf("get old unlinked chat files: %w", err)
}
if len(ids) == 0 {
return nil
}
deleted, err = tx.DeleteUnlinkedChatFilesByIDs(ctx, DeleteUnlinkedChatFilesByIDsParams{
IDs: ids,
BeforeTime: arg.BeforeTime,
})
if err != nil {
return xerrors.Errorf("delete old unlinked chat files: %w", err)
}
return nil
}, DefaultTXOptions().WithID("delete_old_chat_files"))
if err != nil {
return 0, err
}
return deleted, nil
}