refactor: remove completed one-time startup migrations

Production data is migrated and every marker is recorded in the system
collection, so the stock dividend-history, sticker legacy-pack and stats
stock_dividend migrations no longer do anything. Stats keeps its startup
index creation and still hides retired stock_dividend rows.
This commit is contained in:
tiennm99 committed 2026-10-08 10:35:21 +07:00
1 parent 5344111787
commit 5e96490254
13 files changed
+31 -1030

No files matched your search

+1 -57
View File
@@ -42,7 +42,6 @@ import (
"github.com/tiennm99/tiennm99bot/internal/modules/wordle"
"github.com/tiennm99/tiennm99bot/internal/server"
"github.com/tiennm99/tiennm99bot/internal/storage"
"github.com/tiennm99/tiennm99bot/internal/systemstate"
"github.com/tiennm99/tiennm99bot/internal/telegram"
)
@@ -117,10 +116,6 @@ func factories() map[string]modules.Factory {
// container; 10s leaves headroom without hiding a wedged cluster.
const mongodbInitTimeout = 10 * time.Second
// stockMigrationTimeout bounds the one-time scan of persisted stock
// portfolios without tying it to the shorter MongoDB connection timeout.
const stockMigrationTimeout = 2 * time.Minute
func main() {
rootCtx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()
@@ -145,21 +140,12 @@ func main() {
}
defer closeProvider()
if err := initStatsStore(rootCtx, provider); err != nil {
if err := stats.InitStore(rootCtx, provider.Collection("stats")); err != nil {
log.Fatal("stats storage init failed", "err", err)
}
if err := lol.InitStore(rootCtx, provider.Collection(lol.CollectionName)); err != nil {
log.Fatal("lol storage init failed", "err", err)
}
if err := initStickerStore(rootCtx, provider); err != nil {
log.Fatal("sticker storage init failed", "err", err)
}
migrationCtx, cancelMigration := context.WithTimeout(rootCtx, stockMigrationTimeout)
if err := initStockStore(migrationCtx, provider); err != nil {
cancelMigration()
log.Fatal("stock storage init failed", "err", err)
}
cancelMigration()
b, err := telegram.NewBot(cfg.TelegramBotToken)
if err != nil {
@@ -261,48 +247,6 @@ func main() {
metrics.Flush()
}
func initStockStore(ctx context.Context, provider storage.Provider) error {
return initStockStoreWith(ctx, provider, stock.InitStore)
}
func initStatsStore(ctx context.Context, provider storage.Provider) error {
return initStatsStoreWith(ctx, provider, stats.InitStore)
}
type statsStoreInitializer func(context.Context, storage.Collection, storage.Collection) error
func initStatsStoreWith(ctx context.Context, provider storage.Provider, init statsStoreInitializer) error {
return init(
ctx,
provider.Collection("stats"),
provider.Collection(systemstate.CollectionName),
)
}
func initStickerStore(ctx context.Context, provider storage.Provider) error {
return initStickerStoreWith(ctx, provider, sticker.InitStore)
}
type stickerStoreInitializer func(context.Context, storage.Collection, storage.Collection) error
func initStickerStoreWith(ctx context.Context, provider storage.Provider, init stickerStoreInitializer) error {
return init(
ctx,
provider.Collection(sticker.CollectionName),
provider.Collection(systemstate.CollectionName),
)
}
type stockStoreInitializer func(context.Context, storage.Collection, storage.Collection) error
func initStockStoreWith(ctx context.Context, provider storage.Provider, init stockStoreInitializer) error {
return init(
ctx,
provider.Collection(stock.CollectionName),
provider.Collection(systemstate.CollectionName),
)
}
// buildProvider picks the storage backend. Selection order:
// 1. Explicit KV_PROVIDER env (memory|mongodb) wins.
// 2. Auto-detect: MONGO_URL set → mongodb; otherwise memory.
-51
View File
@@ -1,17 +1,13 @@
package main
import (
"context"
"errors"
"os"
"path/filepath"
"strings"
"testing"
"github.com/tiennm99/tiennm99bot/internal/modules"
"github.com/tiennm99/tiennm99bot/internal/modules/stock"
"github.com/tiennm99/tiennm99bot/internal/storage"
"github.com/tiennm99/tiennm99bot/internal/systemstate"
)
func TestResolveCommitSHA(t *testing.T) {
@@ -36,53 +32,6 @@ func TestResolveCommitSHA(t *testing.T) {
}
}
func TestInitStockStoreRunsStartupMigration(t *testing.T) {
ctx := context.Background()
provider := storage.NewMemoryProvider()
if err := initStockStore(ctx, provider); err != nil {
t.Fatalf("initStockStore: %v", err)
}
marker, exists, err := systemstate.New(provider.Collection(systemstate.CollectionName)).Get(ctx, "migration:stock-dividend-history-v1")
if err != nil || !exists || marker.Status != "completed" {
t.Fatalf("marker=%+v exists=%v err=%v", marker, exists, err)
}
if _, _, err := storage.Typed[stock.Portfolio](provider.Collection(stock.CollectionName)).Get(ctx, "user:1"); !errors.Is(err, storage.ErrNotFound) {
t.Fatalf("unexpected portfolio lookup error: %v", err)
}
}
func TestInitStatsStoreRunsStartupMigration(t *testing.T) {
ctx := context.Background()
provider := storage.NewMemoryProvider()
if err := initStatsStore(ctx, provider); err != nil {
t.Fatalf("initStatsStore: %v", err)
}
marker, exists, err := systemstate.New(provider.Collection(systemstate.CollectionName)).Get(ctx, "migration:stats-delete-stock-dividend-v1")
if err != nil || !exists || marker.Status != "completed" {
t.Fatalf("marker=%+v exists=%v err=%v", marker, exists, err)
}
}
func TestInitStockStorePropagatesMigrationError(t *testing.T) {
want := errors.New("migration failed")
err := initStockStoreWith(context.Background(), storage.NewMemoryProvider(), func(context.Context, storage.Collection, storage.Collection) error {
return want
})
if !errors.Is(err, want) {
t.Fatalf("initStockStoreWith error=%v, want %v", err, want)
}
}
func TestInitStatsStorePropagatesMigrationError(t *testing.T) {
want := errors.New("migration failed")
err := initStatsStoreWith(context.Background(), storage.NewMemoryProvider(), func(context.Context, storage.Collection, storage.Collection) error {
return want
})
if !errors.Is(err, want) {
t.Fatalf("initStatsStoreWith error=%v, want %v", err, want)
}
}
func TestShortCommitSHA(t *testing.T) {
if got := shortCommitSHA(" 0123456789abcdef "); got != "0123456" {
t.Errorf("full revision: got %q, want %q", got, "0123456")