Skip to content

Commit 1abd76e

Browse files
committed
fix(sync): tolerate duplicate attachment snapshots
1 parent 4c5630d commit 1abd76e

3 files changed

Lines changed: 100 additions & 1 deletion

File tree

internal/store/sqlc/queries.sql

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,25 @@ insert into message_attachments(
384384
attachment_id, message_id, guild_id, channel_id, author_id, filename,
385385
content_type, size, url, proxy_url, text_content, media_path, content_sha256,
386386
content_size, fetched_at, fetch_status, fetch_error, updated_at
387-
) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);
387+
) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
388+
on conflict(attachment_id) do update set
389+
message_id = excluded.message_id,
390+
guild_id = excluded.guild_id,
391+
channel_id = excluded.channel_id,
392+
author_id = excluded.author_id,
393+
filename = excluded.filename,
394+
content_type = excluded.content_type,
395+
size = excluded.size,
396+
url = excluded.url,
397+
proxy_url = excluded.proxy_url,
398+
text_content = excluded.text_content,
399+
media_path = excluded.media_path,
400+
content_sha256 = excluded.content_sha256,
401+
content_size = excluded.content_size,
402+
fetched_at = excluded.fetched_at,
403+
fetch_status = excluded.fetch_status,
404+
fetch_error = excluded.fetch_error,
405+
updated_at = excluded.updated_at;
388406

389407
-- name: DeleteMentionEventsByMessage :exec
390408
delete from mention_events

internal/store/store_write_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,69 @@ func TestUpsertMessagesBatch(t *testing.T) {
6969
require.Equal(t, "2", rows[0][0])
7070
}
7171

72+
func TestUpsertMessagesRefreshesDuplicateAttachmentID(t *testing.T) {
73+
t.Parallel()
74+
75+
ctx := context.Background()
76+
s, err := Open(ctx, filepath.Join(t.TempDir(), "discrawl.db"))
77+
require.NoError(t, err)
78+
defer func() { _ = s.Close() }()
79+
80+
now := time.Now().UTC().Format(time.RFC3339Nano)
81+
first := MessageMutation{
82+
Record: MessageRecord{
83+
ID: "m1",
84+
GuildID: "g1",
85+
ChannelID: "c1",
86+
MessageType: 0,
87+
CreatedAt: now,
88+
Content: "first",
89+
NormalizedContent: "first",
90+
HasAttachments: true,
91+
RawJSON: `{"id":"m1"}`,
92+
},
93+
Attachments: []AttachmentRecord{{
94+
AttachmentID: "a1",
95+
MessageID: "m1",
96+
GuildID: "g1",
97+
ChannelID: "c1",
98+
Filename: "first.txt",
99+
TextContent: "cached text",
100+
MediaPath: "attachments/a1.txt",
101+
FetchStatus: "done",
102+
}},
103+
}
104+
second := MessageMutation{
105+
Record: MessageRecord{
106+
ID: "m2",
107+
GuildID: "g1",
108+
ChannelID: "c2",
109+
MessageType: 0,
110+
CreatedAt: now,
111+
Content: "second",
112+
NormalizedContent: "second",
113+
HasAttachments: true,
114+
RawJSON: `{"id":"m2"}`,
115+
},
116+
Attachments: []AttachmentRecord{{
117+
AttachmentID: "a1",
118+
MessageID: "m2",
119+
GuildID: "g1",
120+
ChannelID: "c2",
121+
Filename: "second.txt",
122+
TextContent: "fresh text",
123+
FetchStatus: "done",
124+
}},
125+
}
126+
127+
require.NoError(t, s.UpsertMessages(ctx, []MessageMutation{first}))
128+
require.NoError(t, s.UpsertMessages(ctx, []MessageMutation{second}))
129+
130+
_, rows, err := s.ReadOnlyQuery(ctx, "select attachment_id, message_id, channel_id, filename, text_content, media_path from message_attachments")
131+
require.NoError(t, err)
132+
require.Equal(t, [][]string{{"a1", "m2", "c2", "second.txt", "fresh text", ""}}, rows)
133+
}
134+
72135
func TestUpsertMessagesNormalizesTimestampStrings(t *testing.T) {
73136
t.Parallel()
74137

internal/store/storedb/queries.sql.go

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)