Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions internal/store/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ func (s *Store) GetSyncState(ctx context.Context, scope string) (string, error)
return cursor, nil
}

// ChannelHasMessages reports whether any message rows are stored for the
// channel. It is an O(1) existence probe against idx_messages_channel_id and is
// safe to call once per channel per sync.
func (s *Store) ChannelHasMessages(ctx context.Context, channelID string) (bool, error) {
if channelID == "" {
return false, nil
}
return s.q.ChannelHasMessages(ctx, channelID)
}

func (s *Store) ChannelMessageBounds(ctx context.Context, channelID string) (string, string, error) {
row, err := s.q.ChannelMessageBounds(ctx, channelID)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions internal/store/sqlc/queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ on conflict(scope) do update set
delete from sync_state
where scope = ?;

-- name: ChannelHasMessages :one
select exists(select 1 from messages where channel_id = ? limit 1) as has_messages;

-- name: ChannelMessageBounds :one
select cast(coalesce(min(id), '') as text) as oldest_id,
cast(coalesce(max(id), '') as text) as newest_id
Expand Down
11 changes: 11 additions & 0 deletions internal/store/storedb/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions internal/syncer/channel_catalog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,13 @@ func TestSyncSkipsUnchangedThreadsWhenHistoryComplete(t *testing.T) {
}))
require.NoError(t, s.SetSyncState(ctx, channelLatestScope("t1"), "200"))
require.NoError(t, s.SetSyncState(ctx, channelHistoryCompleteScope("t1"), "1"))
// A genuinely complete channel has the messages to show for it. Without a
// stored row the channel is re-crawled to recover the missing history.
require.NoError(t, s.UpsertMessage(ctx, store.MessageRecord{
ID: "200", GuildID: "g1", ChannelID: "t1", ChannelName: "bug-report",
AuthorID: "u1", AuthorName: "User", CreatedAt: time.Now().UTC().Format(time.RFC3339Nano),
Content: "hello", NormalizedContent: "hello", RawJSON: `{}`,
}))

client := &fakeClient{
guilds: []*discordgo.UserGuild{{ID: "g1", Name: "Guild"}},
Expand Down Expand Up @@ -556,6 +563,13 @@ func TestSyncSkipsUnchangedTextChannelsWhenHistoryComplete(t *testing.T) {
}))
require.NoError(t, s.SetSyncState(ctx, channelLatestScope("c1"), "200"))
require.NoError(t, s.SetSyncState(ctx, channelHistoryCompleteScope("c1"), "1"))
// A genuinely complete channel has the messages to show for it. Without a
// stored row the channel is re-crawled to recover the missing history.
require.NoError(t, s.UpsertMessage(ctx, store.MessageRecord{
ID: "200", GuildID: "g1", ChannelID: "c1", ChannelName: "general",
AuthorID: "u1", AuthorName: "User", CreatedAt: time.Now().UTC().Format(time.RFC3339Nano),
Content: "hello", NormalizedContent: "hello", RawJSON: `{}`,
}))

client := &fakeClient{
guilds: []*discordgo.UserGuild{{ID: "g1", Name: "Guild"}},
Expand Down
Loading