template-monorepo/cmd/mongo-index/main.go

47 lines
1.3 KiB
Go

// Command mongo-index ensures Gateway notification MongoDB indexes exist.
// Use when docker-entrypoint-initdb.d did not run (existing mongo_data volume).
package main
import (
"context"
"flag"
"fmt"
"os"
"time"
"gateway/internal/config"
notifrepo "gateway/internal/model/notification/repository"
"github.com/zeromicro/go-zero/core/conf"
)
var configFile = flag.String("f", "etc/gateway.dev.yaml", "config file")
func main() {
flag.Parse()
var c config.Config
conf.MustLoad(*configFile, &c)
if c.Mongo.Host == "" {
fmt.Fprintln(os.Stderr, "mongo-index: Mongo.Host is empty in config")
os.Exit(1)
}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
notifRepo := notifrepo.NewNotificationRepository(notifrepo.NotificationRepositoryParam{Conf: &c.Mongo})
dlqRepo := notifrepo.NewNotificationDLQRepository(notifrepo.NotificationDLQRepositoryParam{Conf: &c.Mongo})
if err := notifRepo.Index20260520001UP(ctx); err != nil {
fmt.Fprintf(os.Stderr, "mongo-index: notifications: %v\n", err)
os.Exit(1)
}
if err := dlqRepo.Index20260520001UP(ctx); err != nil {
fmt.Fprintf(os.Stderr, "mongo-index: notification_dlq: %v\n", err)
os.Exit(1)
}
fmt.Println("mongo-index: notifications + notification_dlq indexes OK")
}