diff --git a/acceleration/internal/store/channel_threads.go b/acceleration/internal/store/channel_threads.go index 5e6da4ea4..93ebfe625 100644 --- a/acceleration/internal/store/channel_threads.go +++ b/acceleration/internal/store/channel_threads.go @@ -99,11 +99,48 @@ func (s *Store) ReleaseChannelThreadTurn(ctx context.Context, channelID, holder return stack.Wrap(err) } +// channelThreadMessage is one claimed message of a thread channel (ClaimChannelThreadMessage). +type channelThreadMessage struct { + bun.BaseModel `bun:"table:channel_thread_messages,alias:ctmsg"` + + ChannelID string `bun:"channel_id,pk"` + Kind string `bun:"kind,pk"` + MessageID string `bun:"message_id,pk"` + CreatedAt time.Time `bun:"created_at,notnull"` +} + +// channelThreadPerson is the one person a thread channel's chat is with +// (SetChannelThreadChatPerson). +type channelThreadPerson struct { + bun.BaseModel `bun:"table:channel_thread_people,alias:ctp"` + + ChannelID string `bun:"channel_id,pk"` + ChatPerson string `bun:"chat_person,notnull"` +} + +// channelThreadMember is a person a thread channel's chat reaches (AddChannelThreadMember). +type channelThreadMember struct { + bun.BaseModel `bun:"table:channel_thread_members,alias:ctm"` + + ChannelID string `bun:"channel_id,pk"` + Member string `bun:"member,pk"` +} + +// channelThreadStarter is the person who started a thread channel's chat +// (SetChannelThreadStarter). +type channelThreadStarter struct { + bun.BaseModel `bun:"table:channel_thread_starters,alias:cts"` + + ChannelID string `bun:"channel_id,pk"` + Starter string `bun:"starter,notnull"` +} + // ReleaseChannelThreadMessage forgets a claim, so the step that took it can take it again: // a reply whose send failed is sent on a later attempt. func (s *Store) ReleaseChannelThreadMessage(ctx context.Context, channelID, kind, messageID string) error { - _, err := s.db.NewRaw("DELETE FROM channel_thread_messages WHERE channel_id = ? AND kind = ? AND message_id = ?", - channelID, kind, messageID).Exec(ctx) + _, err := s.db.NewDelete().Model((*channelThreadMessage)(nil)). + Where("channel_id = ? AND kind = ? AND message_id = ?", channelID, kind, messageID). + Exec(ctx) return stack.Wrap(err) } @@ -143,10 +180,12 @@ func (s *Store) SetChannelThreadChatPerson(ctx context.Context, channelID, perso } var err error if person == "" { - _, err = s.db.NewRaw("DELETE FROM channel_thread_people WHERE channel_id = ?", channelID).Exec(ctx) + _, err = s.db.NewDelete().Model((*channelThreadPerson)(nil)).Where("channel_id = ?", channelID).Exec(ctx) } else { - _, err = s.db.NewRaw("INSERT INTO channel_thread_people (channel_id, chat_person) VALUES (?, ?) "+ - "ON CONFLICT (channel_id) DO UPDATE SET chat_person = EXCLUDED.chat_person", channelID, person).Exec(ctx) + _, err = s.db.NewInsert().Model(&channelThreadPerson{ChannelID: channelID, ChatPerson: person}). + On("CONFLICT (channel_id) DO UPDATE"). + Set("chat_person = EXCLUDED.chat_person"). + Exec(ctx) } if err != nil { return stack.Wrap(fmt.Errorf("store: set channel thread chat person: %w", err)) @@ -161,8 +200,9 @@ func (s *Store) AddChannelThreadMember(ctx context.Context, channelID, member st if channelID == "" || member == "" { return stack.Wrap(errors.New("store: a channel id and a member are required")) } - _, err := s.db.NewRaw("INSERT INTO channel_thread_members (channel_id, member) VALUES (?, ?) ON CONFLICT DO NOTHING", - channelID, member).Exec(ctx) + _, err := s.db.NewInsert().Model(&channelThreadMember{ChannelID: channelID, Member: member}). + On("CONFLICT DO NOTHING"). + Exec(ctx) if err != nil { return stack.Wrap(fmt.Errorf("store: add channel thread member: %w", err)) } @@ -177,8 +217,9 @@ func (s *Store) SetChannelThreadStarter(ctx context.Context, channelID, starter if channelID == "" || starter == "" { return stack.Wrap(errors.New("store: a channel id and a starter are required")) } - _, err := s.db.NewRaw("INSERT INTO channel_thread_starters (channel_id, starter) VALUES (?, ?) ON CONFLICT DO NOTHING", - channelID, starter).Exec(ctx) + _, err := s.db.NewInsert().Model(&channelThreadStarter{ChannelID: channelID, Starter: starter}). + On("CONFLICT DO NOTHING"). + Exec(ctx) if err != nil { return stack.Wrap(fmt.Errorf("store: set channel thread starter: %w", err)) } @@ -189,7 +230,10 @@ func (s *Store) SetChannelThreadStarter(ctx context.Context, channelID, starter // (SetChannelThreadStarter), or "" when none is recorded. func (s *Store) ChannelThreadStarter(ctx context.Context, channelID string) (string, error) { var starter string - err := s.db.NewRaw("SELECT starter FROM channel_thread_starters WHERE channel_id = ?", channelID).Scan(ctx, &starter) + err := s.db.NewSelect().Model((*channelThreadStarter)(nil)). + Column("starter"). + Where("channel_id = ?", channelID). + Scan(ctx, &starter) if errors.Is(err, sql.ErrNoRows) { return "", nil } @@ -252,14 +296,17 @@ func (s *Store) ChannelThread(ctx context.Context, channelID string) (ChannelThr // waitingMessage is one row of channel_thread_waiting (20261011220000_channel_thread_waiting.sql). type waitingMessage struct { - ConnectorID string `bun:"connector_id"` - ProviderUnitID string `bun:"provider_unit_id"` - ThreadKey string `bun:"thread_key"` - ProviderMessageID string `bun:"provider_message_id"` - AuthorID string `bun:"author_id"` - Text string `bun:"text"` - Raw []byte `bun:"raw"` - CreatedAt time.Time `bun:"created_at"` + bun.BaseModel `bun:"table:channel_thread_waiting,alias:ctw"` + + CustomerID string `bun:"customer_id,pk"` + ConnectorID string `bun:"connector_id,pk"` + ProviderUnitID string `bun:"provider_unit_id,pk"` + ThreadKey string `bun:"thread_key,pk"` + ProviderMessageID string `bun:"provider_message_id,pk"` + AuthorID string `bun:"author_id,notnull"` + Text string `bun:"text,notnull"` + Raw []byte `bun:"raw,notnull"` + CreatedAt time.Time `bun:"created_at,notnull"` } // WaitChannelThreadMessage keeps a customer's message on an external thread no thread channel @@ -271,14 +318,15 @@ func (s *Store) WaitChannelThreadMessage(ctx context.Context, customerID string, return false, stack.Wrap(errors.New("store: a customer, a connector, a thread key and a message id are required")) } now := time.Now().UTC() - if _, err := s.db.NewRaw("DELETE FROM channel_thread_waiting WHERE created_at < ?", now.Add(-channelWaitingKeep)).Exec(ctx); err != nil { + _, err := s.db.NewDelete().Model((*waitingMessage)(nil)).Where("created_at < ?", now.Add(-channelWaitingKeep)).Exec(ctx) + if err != nil { return false, stack.Wrap(fmt.Errorf("store: wait channel thread message: %w", err)) } - result, err := s.db.NewRaw( - "INSERT INTO channel_thread_waiting (customer_id, connector_id, provider_unit_id, thread_key, provider_message_id, author_id, text, raw, created_at) "+ - "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) ON CONFLICT DO NOTHING", - customerID, message.ConnectorID, message.ProviderUnitID, message.ThreadKey, message.ProviderMessageID, - message.AuthorID, message.Text, message.Raw, now).Exec(ctx) + result, err := s.db.NewInsert().Model(&waitingMessage{ + CustomerID: customerID, ConnectorID: message.ConnectorID, ProviderUnitID: message.ProviderUnitID, + ThreadKey: message.ThreadKey, ProviderMessageID: message.ProviderMessageID, + AuthorID: message.AuthorID, Text: message.Text, Raw: message.Raw, CreatedAt: now, + }).On("CONFLICT DO NOTHING").Exec(ctx) if err != nil { return false, stack.Wrap(fmt.Errorf("store: wait channel thread message: %w", err)) } @@ -292,9 +340,11 @@ func (s *Store) WaitChannelThreadMessage(ctx context.Context, customerID string, // UnwaitChannelThreadMessage takes one waiting message back. False is one that is not waiting: // the message that linked its thread took it first, or it was never kept. func (s *Store) UnwaitChannelThreadMessage(ctx context.Context, customerID string, message core.InboundMessage) (bool, error) { - result, err := s.db.NewRaw( - "DELETE FROM channel_thread_waiting WHERE customer_id = ? AND connector_id = ? AND provider_unit_id = ? AND thread_key = ? AND provider_message_id = ?", - customerID, message.ConnectorID, message.ProviderUnitID, message.ThreadKey, message.ProviderMessageID).Exec(ctx) + result, err := s.db.NewDelete().Model((*waitingMessage)(nil)). + Where("customer_id = ? AND connector_id = ? AND provider_unit_id = ? AND thread_key = ?", + customerID, message.ConnectorID, message.ProviderUnitID, message.ThreadKey). + Where("provider_message_id = ?", message.ProviderMessageID). + Exec(ctx) if err != nil { return false, stack.Wrap(fmt.Errorf("store: unwait channel thread message: %w", err)) } @@ -311,10 +361,12 @@ func (s *Store) UnwaitChannelThreadMessage(ctx context.Context, customerID strin // (UnwaitChannelThreadMessage) gets it, or this does. func takeWaitingChannelThreadMessages(ctx context.Context, db bun.IDB, customerID, connectorID, providerUnitID, threadKey string) ([]core.InboundMessage, error) { var rows []waitingMessage - err := db.NewRaw( - "DELETE FROM channel_thread_waiting WHERE customer_id = ? AND connector_id = ? AND provider_unit_id = ? AND thread_key = ? AND created_at >= ? "+ - "RETURNING connector_id, provider_unit_id, thread_key, provider_message_id, author_id, text, raw, created_at", - customerID, connectorID, providerUnitID, threadKey, time.Now().UTC().Add(-channelWaitingKeep)).Scan(ctx, &rows) + err := db.NewDelete().Model((*waitingMessage)(nil)). + Where("customer_id = ? AND connector_id = ? AND provider_unit_id = ? AND thread_key = ?", + customerID, connectorID, providerUnitID, threadKey). + Where("created_at >= ?", time.Now().UTC().Add(-channelWaitingKeep)). + Returning("connector_id, provider_unit_id, thread_key, provider_message_id, author_id, text, raw, created_at"). + Scan(ctx, &rows) if err != nil { return nil, stack.Wrap(fmt.Errorf("store: take waiting channel thread messages: %w", err)) } @@ -355,15 +407,15 @@ func claimChannelThreadMessage(ctx context.Context, db bun.IDB, channelID, kind, return false, stack.Wrap(errors.New("store: a channel, a kind and a message id are required")) } now := time.Now().UTC() - if _, err := db.NewRaw( - "DELETE FROM channel_thread_messages WHERE channel_id = ? AND created_at < ?", - channelID, now.Add(-channelMessageKeep)).Exec(ctx); err != nil { + _, err := db.NewDelete().Model((*channelThreadMessage)(nil)). + Where("channel_id = ? AND created_at < ?", channelID, now.Add(-channelMessageKeep)). + Exec(ctx) + if err != nil { return false, stack.Wrap(fmt.Errorf("store: claim channel thread message: %w", err)) } - result, err := db.NewRaw( - "INSERT INTO channel_thread_messages (channel_id, kind, message_id, created_at) "+ - "VALUES (?, ?, ?, ?) ON CONFLICT DO NOTHING", - channelID, kind, messageID, now).Exec(ctx) + result, err := db.NewInsert().Model(&channelThreadMessage{ChannelID: channelID, Kind: kind, MessageID: messageID, CreatedAt: now}). + On("CONFLICT DO NOTHING"). + Exec(ctx) if err != nil { return false, stack.Wrap(fmt.Errorf("store: claim channel thread message: %w", err)) } diff --git a/acceleration/internal/store/channel_threads_test.go b/acceleration/internal/store/channel_threads_test.go index 57a3bf66b..a3e317ecc 100644 --- a/acceleration/internal/store/channel_threads_test.go +++ b/acceleration/internal/store/channel_threads_test.go @@ -75,6 +75,21 @@ func (s *StoreSuite) TestAnotherThreadGetsAnotherChannel() { s.True(created) } +func (s *StoreSuite) TestTheFirstStarterOfAThreadIsTheOneKept() { + _, err := s.store.LinkChannelThread(s.ctx, thread("thread-one", "C0000CHAN:1")) + s.Require().NoError(err) + starter, err := s.store.ChannelThreadStarter(s.ctx, "thread-one") + s.Require().NoError(err) + s.Empty(starter, "nobody started it yet") + + s.Require().NoError(s.store.SetChannelThreadStarter(s.ctx, "thread-one", "+15550100")) + s.Require().NoError(s.store.SetChannelThreadStarter(s.ctx, "thread-one", "+15550199")) + + starter, err = s.store.ChannelThreadStarter(s.ctx, "thread-one") + s.Require().NoError(err) + s.Equal("+15550100", starter) +} + // One Slack workspace can install two customers' apps, so the same thread is two threads. func (s *StoreSuite) TestTheSameThreadOfAnotherCustomerIsAThreadOfItsOwn() { _, err := s.store.LinkChannelThread(s.ctx, thread("thread-one", "C0000CHAN:1")) diff --git a/acceleration/internal/store/channels.go b/acceleration/internal/store/channels.go index 8d113fde9..ae578e4ed 100644 --- a/acceleration/internal/store/channels.go +++ b/acceleration/internal/store/channels.go @@ -7,6 +7,8 @@ import ( "fmt" "time" + "github.com/uptrace/bun" + "github.com/GetStream/Vision-Agents/acceleration/internal/stack" ) @@ -288,13 +290,21 @@ func (s *Store) ClaimChannelLink(ctx context.Context, customerID, code string) ( // ErrUnknownChannelLink is a code nobody was given, one already spent, or one past its time. var ErrUnknownChannelLink = errors.New("store: there is no such code") +// channelDelivery is one message a line was delivered (ClaimChannelMessage). +type channelDelivery struct { + bun.BaseModel `bun:"table:channel_deliveries,alias:cdel"` + + AccountID string `bun:"account_id,pk"` + MessageID string `bun:"message_id,pk"` + ReceivedAt time.Time `bun:"received_at,notnull"` + FromAddress string `bun:"from_address,notnull"` +} + // ClaimChannelMessage records a message as delivered, and who sent it. False is one // already taken, which a provider retrying a slow delivery sends. func (s *Store) ClaimChannelMessage(ctx context.Context, accountID, messageID, from string) (bool, error) { - result, err := s.db.NewRaw( - "INSERT INTO channel_deliveries (account_id, message_id, received_at, from_address) "+ - "VALUES (?, ?, ?, ?) ON CONFLICT DO NOTHING", - accountID, messageID, time.Now().UTC(), from).Exec(ctx) + delivery := channelDelivery{AccountID: accountID, MessageID: messageID, ReceivedAt: time.Now().UTC(), FromAddress: from} + result, err := s.db.NewInsert().Model(&delivery).On("CONFLICT DO NOTHING").Exec(ctx) if err != nil { return false, fmt.Errorf("store: claim channel message: %w", err) } @@ -309,10 +319,12 @@ func (s *Store) ClaimChannelMessage(ctx context.Context, accountID, messageID, f // they never have. WhatsApp's window for writing first is counted from it. func (s *Store) LastChannelMessageAt(ctx context.Context, accountID, from string) (time.Time, error) { var received time.Time - err := s.db.NewRaw( - "SELECT received_at FROM channel_deliveries "+ - "WHERE account_id = ? AND from_address = ? ORDER BY received_at DESC LIMIT 1", - accountID, from).Scan(ctx, &received) + err := s.db.NewSelect().Model((*channelDelivery)(nil)). + Column("received_at"). + Where("account_id = ? AND from_address = ?", accountID, from). + OrderExpr("received_at DESC"). + Limit(1). + Scan(ctx, &received) if errors.Is(err, sql.ErrNoRows) { return time.Time{}, nil } diff --git a/acceleration/internal/store/chat_channel_turns.go b/acceleration/internal/store/chat_channel_turns.go index f9baf7d42..a5672495c 100644 --- a/acceleration/internal/store/chat_channel_turns.go +++ b/acceleration/internal/store/chat_channel_turns.go @@ -6,9 +6,21 @@ import ( "fmt" "time" + "github.com/uptrace/bun" + "github.com/GetStream/Vision-Agents/acceleration/internal/stack" ) +// chatChannelTurn is the lease on a channel's running turn (TakeChatChannelTurn). +type chatChannelTurn struct { + bun.BaseModel `bun:"table:chat_channel_turns,alias:cht"` + + StreamAppPK int64 `bun:"stream_app_pk,pk"` + CID string `bun:"cid,pk"` + TurnHolder string `bun:"turn_holder,notnull"` + TurnUntil time.Time `bun:"turn_until,notnull"` +} + // TakeChatChannelTurn leases the one running turn of the channel cid of the Stream app app, a // channel of a type an agent config answers in, to holder until until, when no turn holds it // or the one that did ran out. False is a turn another holder has, as TakeChannelThreadTurn @@ -17,12 +29,12 @@ func (s *Store) TakeChatChannelTurn(ctx context.Context, app int64, cid, holder if cid == "" || holder == "" { return false, stack.Wrap(errors.New("store: a channel and a holder are required")) } - result, err := s.db.NewRaw( - "INSERT INTO chat_channel_turns (stream_app_pk, cid, turn_holder, turn_until) VALUES (?, ?, ?, ?) "+ - "ON CONFLICT (stream_app_pk, cid) DO UPDATE "+ - "SET turn_holder = EXCLUDED.turn_holder, turn_until = EXCLUDED.turn_until "+ - "WHERE chat_channel_turns.turn_until < now() OR chat_channel_turns.turn_holder = EXCLUDED.turn_holder", - app, cid, holder, until.UTC()).Exec(ctx) + result, err := s.db.NewInsert().Model(&chatChannelTurn{StreamAppPK: app, CID: cid, TurnHolder: holder, TurnUntil: until.UTC()}). + On("CONFLICT (stream_app_pk, cid) DO UPDATE"). + Set("turn_holder = EXCLUDED.turn_holder"). + Set("turn_until = EXCLUDED.turn_until"). + Where("cht.turn_until < now() OR cht.turn_holder = EXCLUDED.turn_holder"). + Exec(ctx) if err != nil { return false, stack.Wrap(fmt.Errorf("store: take chat channel turn: %w", err)) } @@ -36,7 +48,8 @@ func (s *Store) TakeChatChannelTurn(ctx context.Context, app int64, cid, holder // ReleaseChatChannelTurn ends holder's lease on a channel's turn, so the next turn need not // wait for it to run out. A lease another holder took since is left alone. func (s *Store) ReleaseChatChannelTurn(ctx context.Context, app int64, cid, holder string) error { - _, err := s.db.NewRaw("DELETE FROM chat_channel_turns WHERE stream_app_pk = ? AND cid = ? AND turn_holder = ?", - app, cid, holder).Exec(ctx) + _, err := s.db.NewDelete().Model((*chatChannelTurn)(nil)). + Where("stream_app_pk = ? AND cid = ? AND turn_holder = ?", app, cid, holder). + Exec(ctx) return stack.Wrap(err) } diff --git a/acceleration/internal/store/connection_events.go b/acceleration/internal/store/connection_events.go index 56d2963af..097cfe8dc 100644 --- a/acceleration/internal/store/connection_events.go +++ b/acceleration/internal/store/connection_events.go @@ -204,7 +204,7 @@ func (s *Store) claimConnectionEventSubscriptions(ctx context.Context, query str // found is false when none is ever due: there are no subscriptions, or none expires. func (s *Store) NextConnectionEventSubscriptionAt(ctx context.Context) (next time.Time, found bool, err error) { var at sql.NullTime - if err := s.db.NewRaw("SELECT min(next_attempt_at) FROM connection_event_subscriptions").Scan(ctx, &at); err != nil { + if err := s.db.NewSelect().Model((*ConnectionEventSubscription)(nil)).ColumnExpr("min(next_attempt_at)").Scan(ctx, &at); err != nil { return time.Time{}, false, stack.Wrap(fmt.Errorf("store: next connection event subscription: %w", err)) } return at.Time, at.Valid, nil @@ -245,12 +245,20 @@ func (s *Store) DeleteConnectionEventSubscriptions(ctx context.Context, customer return nil } +// connectionEventDelivery is one event a subscription got (ClaimConnectionEvent). +type connectionEventDelivery struct { + bun.BaseModel `bun:"table:connection_event_deliveries,alias:cevd"` + + SubscriptionID string `bun:"subscription_id,pk"` + EventID string `bun:"event_id,pk"` + ReceivedAt time.Time `bun:"received_at,notnull"` +} + // ClaimConnectionEvent records that a subscription's event arrived, and reports whether this // is the first time: a retried delivery of the same event id is not. func (s *Store) ClaimConnectionEvent(ctx context.Context, subscriptionID, eventID string) (bool, error) { - result, err := s.db.NewRaw( - "INSERT INTO connection_event_deliveries (subscription_id, event_id, received_at) VALUES (?, ?, ?) ON CONFLICT DO NOTHING", - subscriptionID, eventID, time.Now().UTC()).Exec(ctx) + delivery := connectionEventDelivery{SubscriptionID: subscriptionID, EventID: eventID, ReceivedAt: time.Now().UTC()} + result, err := s.db.NewInsert().Model(&delivery).On("CONFLICT DO NOTHING").Exec(ctx) if err != nil { return false, stack.Wrap(fmt.Errorf("store: claim connection event: %w", err)) } diff --git a/acceleration/internal/store/grants.go b/acceleration/internal/store/grants.go index 03e5ee4de..d676d2a83 100644 --- a/acceleration/internal/store/grants.go +++ b/acceleration/internal/store/grants.go @@ -452,6 +452,24 @@ DELETE FROM limit_grant_uses WHERE (grant_id, usage_id) IN ( } } +// grantUse is one row counted toward a grant (limit_grant_uses). +type grantUse struct { + bun.BaseModel `bun:"table:limit_grant_uses,alias:lgu"` + + GrantID int64 `bun:"grant_id,pk"` + UsageID uuid.UUID `bun:"usage_id,pk,type:uuid"` + Micros int64 `bun:"micros,notnull"` +} + +// grantReconcile is where a shard's reconcile of grant uses got (limit_grant_reconciles). +type grantReconcile struct { + bun.BaseModel `bun:"table:limit_grant_reconciles,alias:lgr"` + + Shard string `bun:"shard,pk"` + StartedAt time.Time `bun:"started_at,notnull"` + UpdatedAt time.Time `bun:"updated_at,notnull"` +} + // GrantUsesCounted is, of the rows named by usage id, how many grants each counts toward. func (s *Store) GrantUsesCounted(ctx context.Context, usageIDs []uuid.UUID) (map[uuid.UUID]int, error) { counted := map[uuid.UUID]int{} @@ -466,8 +484,12 @@ func (s *Store) GrantUsesCounted(ctx context.Context, usageIDs []uuid.UUID) (map UsageID uuid.UUID `bun:"usage_id"` Grants int `bun:"grants"` } - err := s.db.NewRaw("SELECT usage_id, count(*) AS grants FROM limit_grant_uses WHERE usage_id = ANY(?::uuid[]) GROUP BY usage_id", - pgdialect.Array(ids)).Scan(ctx, &rows) + err := s.db.NewSelect().Model((*grantUse)(nil)). + Column("usage_id"). + ColumnExpr("count(*) AS grants"). + Where("usage_id = ANY(?::uuid[])", pgdialect.Array(ids)). + Group("usage_id"). + Scan(ctx, &rows) if err != nil { return nil, stack.Wrap(fmt.Errorf("store: grant uses counted: %w", err)) } @@ -541,7 +563,7 @@ const reconcileGrantsQuery = "SELECT id, started_at, usage_id, cost_micros, limi // reconcileShard is ReconcileGrantUses for one shard, read up to now. func (s *Store) reconcileShard(ctx context.Context, shard bun.IDB, name string, now time.Time) (int, error) { var mark time.Time - err := s.db.NewRaw("SELECT started_at FROM limit_grant_reconciles WHERE shard = ?", name).Scan(ctx, &mark) + err := s.db.NewSelect().Model((*grantReconcile)(nil)).Column("started_at").Where("shard = ?", name).Scan(ctx, &mark) if err != nil && !errors.Is(err, sql.ErrNoRows) { return 0, fmt.Errorf("shard %s: %w", name, err) } @@ -582,9 +604,13 @@ func (s *Store) reconcileShard(ctx context.Context, shard bun.IDB, name string, reached = last.StartedAt } } - if _, err := s.db.NewRaw("INSERT INTO limit_grant_reconciles (shard, started_at) VALUES (?, ?) "+ - "ON CONFLICT (shard) DO UPDATE SET started_at = GREATEST(limit_grant_reconciles.started_at, EXCLUDED.started_at), updated_at = now()", - name, reached).Exec(ctx); err != nil { + _, err = s.db.NewInsert().Model(&grantReconcile{Shard: name, StartedAt: reached}). + Column("shard", "started_at"). + On("CONFLICT (shard) DO UPDATE"). + Set("started_at = GREATEST(lgr.started_at, EXCLUDED.started_at)"). + Set("updated_at = now()"). + Exec(ctx) + if err != nil { return counted, fmt.Errorf("shard %s: keep where it got: %w", name, err) } if len(rows) < grantReconcilePage { diff --git a/acceleration/internal/store/grants_test.go b/acceleration/internal/store/grants_test.go index b6c4e50ad..989b576ca 100644 --- a/acceleration/internal/store/grants_test.go +++ b/acceleration/internal/store/grants_test.go @@ -273,6 +273,22 @@ func (s *StoreSuite) TestARowWrittenTwiceCountsOnceTowardItsGrant() { s.EqualValues(350, s.used(grant.ID), "a row sent twice in one batch counts once") } +func (s *StoreSuite) TestGrantUsesCountedIsHowManyGrantsEachRowCountsToward() { + a, b := s.grant(LimitGrant{AmountMicros: 10_000}), s.grant(LimitGrant{AmountMicros: 10_000}) + both, one, other, unasked := uuid.Must(uuid.NewV7()), uuid.Must(uuid.NewV7()), uuid.Must(uuid.NewV7()), uuid.Must(uuid.NewV7()) + _, err := s.store.AddGrantUses(s.ctx, []GrantUse{ + {GrantID: a.ID, UsageID: both, Micros: 10}, + {GrantID: b.ID, UsageID: both, Micros: 10}, + {GrantID: a.ID, UsageID: one, Micros: 10}, + {GrantID: b.ID, UsageID: unasked, Micros: 10}, + }) + s.Require().NoError(err) + + counted, err := s.store.GrantUsesCounted(s.ctx, []uuid.UUID{both, one, other}) + s.Require().NoError(err) + s.Equal(map[uuid.UUID]int{both: 2, one: 1}, counted, "a row counted toward nothing, or not asked about, is left out") +} + func (s *StoreSuite) TestAUsedUpGrantIsNotLiveAndLeavesRoomForAnother() { for range MaxLiveGrants - 1 { s.grant(LimitGrant{OrganizationID: "org-used"}) diff --git a/acceleration/internal/store/hooks.go b/acceleration/internal/store/hooks.go index aa0df7735..b73f578d0 100644 --- a/acceleration/internal/store/hooks.go +++ b/acceleration/internal/store/hooks.go @@ -155,13 +155,22 @@ func (s *Store) FirstDelivery(ctx context.Context, key string) (bool, error) { return recordDelivery(ctx, s.db, key) } +// hookDelivery is one hook delivery's key, as FirstDelivery records it. +type hookDelivery struct { + bun.BaseModel `bun:"table:hook_deliveries,alias:hd"` + + Key string `bun:"key,pk"` + SeenAt time.Time `bun:"seen_at,notnull"` +} + // forgetDeliveries deletes the deliveries older than a day, at most once every // deliveriesPrunedEvery. func (s *Store) forgetDeliveries(ctx context.Context) error { now := time.Now().UTC() if last := s.deliveriesPruned.Load(); now.UnixNano()-last >= int64(deliveriesPrunedEvery) && s.deliveriesPruned.CompareAndSwap(last, now.UnixNano()) { - if _, err := s.db.ExecContext(ctx, "DELETE FROM hook_deliveries WHERE seen_at < ?", now.Add(-deliveryKeptFor)); err != nil { + _, err := s.db.NewDelete().Model((*hookDelivery)(nil)).Where("seen_at < ?", now.Add(-deliveryKeptFor)).Exec(ctx) + if err != nil { return fmt.Errorf("store: forget hook deliveries: %w", err) } } @@ -170,8 +179,10 @@ func (s *Store) forgetDeliveries(ctx context.Context) error { // recordDelivery is FirstDelivery's record on db, the store's or a transaction's. func recordDelivery(ctx context.Context, db bun.IDB, key string) (bool, error) { - result, err := db.NewRaw( - "INSERT INTO hook_deliveries (key, seen_at) VALUES (?, now()) ON CONFLICT (key) DO NOTHING", key).Exec(ctx) + result, err := db.NewInsert().Model(&hookDelivery{Key: key}). + Column("key"). + On("CONFLICT (key) DO NOTHING"). + Exec(ctx) if err != nil { return false, fmt.Errorf("store: record hook delivery: %w", err) } diff --git a/acceleration/internal/store/movecursor.go b/acceleration/internal/store/movecursor.go index 53e6a75c6..9b65cf3f2 100644 --- a/acceleration/internal/store/movecursor.go +++ b/acceleration/internal/store/movecursor.go @@ -6,6 +6,8 @@ import ( "errors" "fmt" "time" + + "github.com/uptrace/bun" ) // MoveCursor is where a copy of an app from another deployment stands here. @@ -17,31 +19,39 @@ type MoveCursor struct { ReadAt time.Time } +// dataMoveCursor is MoveCursor as data_move_cursors holds it. +type dataMoveCursor struct { + bun.BaseModel `bun:"table:data_move_cursors,alias:dmc"` + + CustomerID string `bun:"customer_id,pk"` + Source string `bun:"source,pk"` + Cursor *int64 `bun:"cursor"` + ReadAt time.Time `bun:"read_at,notnull"` +} + // MoveCursor answers where the copy of a customer from a source stands here, nil when none was // ever started. func (s *Store) MoveCursor(ctx context.Context, customerID, source string) (*MoveCursor, error) { - var saved MoveCursor - err := s.db.QueryRowContext(ctx, "SELECT cursor, read_at FROM data_move_cursors WHERE customer_id = ? AND source = ?", - customerID, source).Scan(&saved.Cursor, &saved.ReadAt) + var saved dataMoveCursor + err := s.db.NewSelect().Model(&saved).Where("customer_id = ? AND source = ?", customerID, source).Scan(ctx) if errors.Is(err, sql.ErrNoRows) { return nil, nil } if err != nil { return nil, fmt.Errorf("store: move cursor: %w", err) } - return &saved, nil + return &MoveCursor{Cursor: saved.Cursor, ReadAt: saved.ReadAt}, nil } // SaveMoveCursor records where the copy of a customer from a source stands here: the cursor the // source was read up to at readAt, or nil for a copy that has started and not finished. A cursor // never moves back: of two runs at once, the one that read less cannot undo the other. func (s *Store) SaveMoveCursor(ctx context.Context, customerID, source string, cursor *int64, readAt time.Time) error { - _, err := s.db.ExecContext(ctx, ` -INSERT INTO data_move_cursors (customer_id, source, cursor, read_at) VALUES (?, ?, ?, ?) -ON CONFLICT (customer_id, source) DO UPDATE SET - cursor = CASE WHEN EXCLUDED.cursor IS NULL THEN NULL ELSE GREATEST(data_move_cursors.cursor, EXCLUDED.cursor) END, - read_at = CASE WHEN EXCLUDED.cursor IS NULL THEN EXCLUDED.read_at ELSE GREATEST(data_move_cursors.read_at, EXCLUDED.read_at) END`, - customerID, source, cursor, readAt) + _, err := s.db.NewInsert().Model(&dataMoveCursor{CustomerID: customerID, Source: source, Cursor: cursor, ReadAt: readAt}). + On("CONFLICT (customer_id, source) DO UPDATE"). + Set("cursor = CASE WHEN EXCLUDED.cursor IS NULL THEN NULL ELSE GREATEST(dmc.cursor, EXCLUDED.cursor) END"). + Set("read_at = CASE WHEN EXCLUDED.cursor IS NULL THEN EXCLUDED.read_at ELSE GREATEST(dmc.read_at, EXCLUDED.read_at) END"). + Exec(ctx) if err != nil { return fmt.Errorf("store: save move cursor: %w", err) }