mirror of
https://github.com/tiennm99/tiennm99bot.git
synced 2026-10-19 16:16:22 +00:00
refactor: remove completed migration logic
This commit is contained in:
1 parent
3049af2800
commit
2fb10ff632
8 files changed
+11
-355
No files matched your search
@@ -24,9 +24,6 @@ const (
|
||||
// no-op.
|
||||
func InitStore(ctx context.Context, lolColl storage.Collection) error {
|
||||
if mongoColl, ok := storage.MongoCollection(lolColl); ok {
|
||||
if err := backfillMatchCacheFetchedAt(ctx, mongoColl); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := ensureMatchCacheTTLIndex(ctx, mongoColl); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -34,26 +31,6 @@ func InitStore(ctx context.Context, lolColl storage.Collection) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func backfillMatchCacheFetchedAt(ctx context.Context, coll *mongo.Collection) error {
|
||||
filter := bson.D{
|
||||
{Key: "_id", Value: matchCacheIDRange()},
|
||||
{Key: "fetchedAt", Value: bson.D{{Key: "$exists", Value: false}}},
|
||||
{Key: "ts", Value: bson.D{
|
||||
{Key: "$type", Value: "number"},
|
||||
{Key: "$gt", Value: int64(0)},
|
||||
}},
|
||||
}
|
||||
update := mongo.Pipeline{
|
||||
bson.D{{Key: "$set", Value: bson.D{
|
||||
{Key: "fetchedAt", Value: bson.D{{Key: "$toDate", Value: "$ts"}}},
|
||||
}}},
|
||||
}
|
||||
if _, err := coll.UpdateMany(ctx, filter, update); err != nil {
|
||||
return fmt.Errorf("lol match cache fetchedAt backfill: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func ensureMatchCacheTTLIndex(ctx context.Context, coll *mongo.Collection) error {
|
||||
model := mongo.IndexModel{
|
||||
Keys: bson.D{{Key: "fetchedAt", Value: 1}},
|
||||
|
||||
@@ -41,48 +41,10 @@ func TestInitStore_MongoCreatesMatchCacheTTLIndex(t *testing.T) {
|
||||
t.Fatal("lol collection is not Mongo-backed")
|
||||
}
|
||||
|
||||
fetchedAt := time.Date(2026, 5, 9, 5, 0, 0, 0, time.UTC)
|
||||
if _, err := rawLolColl.InsertMany(ctx, []any{
|
||||
bson.M{
|
||||
"_id": "matches:2026-05-09T00:00:00Z:2026-05-10T00:00:00Z",
|
||||
"version": int64(1),
|
||||
"updatedAt": time.Now().UTC(),
|
||||
"ts": fetchedAt.UnixMilli(),
|
||||
"events": bson.A{},
|
||||
},
|
||||
bson.M{
|
||||
"_id": "subscribers",
|
||||
"version": int64(1),
|
||||
"updatedAt": time.Now().UTC(),
|
||||
"ts": fetchedAt.UnixMilli(),
|
||||
"subscribers": bson.A{},
|
||||
},
|
||||
}); err != nil {
|
||||
t.Fatalf("seed legacy docs: %v", err)
|
||||
}
|
||||
|
||||
if err := InitStore(ctx, lolColl); err != nil {
|
||||
t.Fatalf("InitStore: %v", err)
|
||||
}
|
||||
|
||||
var matchDoc struct {
|
||||
FetchedAt time.Time `bson:"fetchedAt"`
|
||||
}
|
||||
if err := rawLolColl.FindOne(ctx, bson.M{"_id": "matches:2026-05-09T00:00:00Z:2026-05-10T00:00:00Z"}).Decode(&matchDoc); err != nil {
|
||||
t.Fatalf("load backfilled match doc: %v", err)
|
||||
}
|
||||
if !matchDoc.FetchedAt.Equal(fetchedAt) {
|
||||
t.Fatalf("backfilled fetchedAt = %s, want %s", matchDoc.FetchedAt, fetchedAt)
|
||||
}
|
||||
|
||||
var subscriberDoc bson.M
|
||||
if err := rawLolColl.FindOne(ctx, bson.M{"_id": "subscribers"}).Decode(&subscriberDoc); err != nil {
|
||||
t.Fatalf("load subscriber doc: %v", err)
|
||||
}
|
||||
if _, ok := subscriberDoc["fetchedAt"]; ok {
|
||||
t.Fatalf("subscriber doc was backfilled with fetchedAt: %#v", subscriberDoc)
|
||||
}
|
||||
|
||||
cur, err := rawLolColl.Indexes().List(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("list indexes: %v", err)
|
||||
|
||||
@@ -2,7 +2,6 @@ package lol
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
@@ -36,15 +35,6 @@ type SubscriberStore = storage.DocStore[subscribersDoc]
|
||||
|
||||
// listSubscribers returns the current subscriber list, or an empty slice
|
||||
// if none have ever subscribed.
|
||||
//
|
||||
// Decoder accepts two shapes for forward-compatibility with rows written
|
||||
// before topic-aware subscriptions existed:
|
||||
// - current: [{"chat_id":123,"thread_id":7}, ...]
|
||||
// - legacy: [123, 456, ...] (decoded with ThreadID = 0)
|
||||
//
|
||||
// The decoder handles legacy rows indefinitely; any successful add/remove
|
||||
// rewrites the slot in the new shape, but a no-op duplicate add leaves the
|
||||
// legacy bytes in place — that's fine, the fallback path stays correct.
|
||||
func listSubscribers(ctx context.Context, store SubscriberStore) ([]Subscriber, error) {
|
||||
doc, _, err := store.Get(ctx, subscribersKey)
|
||||
switch {
|
||||
@@ -59,25 +49,6 @@ func listSubscribers(ctx context.Context, store SubscriberStore) ([]Subscriber,
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// listSubscribersLegacy decodes a raw JSON byte slice that may be either the
|
||||
// current [{chat_id,thread_id}] shape or the legacy [int64] shape. Used by
|
||||
// the legacy-migration path when a key was written by an older version.
|
||||
func listSubscribersLegacy(raw []byte) ([]Subscriber, error) {
|
||||
var subs []Subscriber
|
||||
if err := json.Unmarshal(raw, &subs); err == nil {
|
||||
return subs, nil
|
||||
}
|
||||
var legacy []int64
|
||||
if err := json.Unmarshal(raw, &legacy); err != nil {
|
||||
return nil, fmt.Errorf("lol listSubscribers decode: %w", err)
|
||||
}
|
||||
out := make([]Subscriber, len(legacy))
|
||||
for i, id := range legacy {
|
||||
out[i] = Subscriber{ChatID: id}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// addSubscriber appends (chatID, threadID) if that exact pair is absent.
|
||||
// Returns true on first-add, false when already subscribed (idempotent).
|
||||
//
|
||||
|
||||
@@ -2,7 +2,6 @@ package lol
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/tiennm99/miti99bot/internal/storage"
|
||||
@@ -130,10 +129,7 @@ func TestSubscribers_RemoveAllForChat(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestSubscribers_LegacyDecode locks in backward-compat reads of rows
|
||||
// written before topic-aware subscriptions (raw []int64 JSON). The legacy
|
||||
// helper decodes both shapes independently of the store.
|
||||
func TestSubscribers_LegacyDecode(t *testing.T) {
|
||||
func TestSubscribers_CurrentShapeRoundTrip(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
store := newSubscriberStore()
|
||||
|
||||
@@ -157,32 +153,7 @@ func TestSubscribers_LegacyDecode(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// Verify the legacy JSON helper handles the old []int64 shape.
|
||||
legacyRaw := []byte(`[11,22,33]`)
|
||||
legacySubs, err := listSubscribersLegacy(legacyRaw)
|
||||
if err != nil {
|
||||
t.Fatalf("listSubscribersLegacy: %v", err)
|
||||
}
|
||||
if len(legacySubs) != 3 {
|
||||
t.Fatalf("legacy decode length = %d, want 3", len(legacySubs))
|
||||
}
|
||||
for i, s := range legacySubs {
|
||||
if s != want[i] {
|
||||
t.Errorf("legacy[%d]: got %v, want %v", i, s, want[i])
|
||||
}
|
||||
}
|
||||
|
||||
// Verify the legacy helper also handles the current [{chat_id,...}] shape.
|
||||
currentRaw, _ := json.Marshal(currentSubs)
|
||||
currentDecoded, err := listSubscribersLegacy(currentRaw)
|
||||
if err != nil {
|
||||
t.Fatalf("listSubscribersLegacy (current shape): %v", err)
|
||||
}
|
||||
if len(currentDecoded) != 3 {
|
||||
t.Fatalf("current shape via legacy helper = %d, want 3", len(currentDecoded))
|
||||
}
|
||||
|
||||
// Next mutation rewrites in the new shape — verify subscribers wrap correctly.
|
||||
// Next mutation verifies subscribers wrap correctly.
|
||||
if _, err := addSubscriber(ctx, store, 44, 7); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user