2026-05-20 07:01:08 +00:00
|
|
|
// 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"
|
|
|
|
|
)
|
|
|
|
|
|
2026-05-20 07:14:44 +00:00
|
|
|
var configFile = flag.String("f", "etc/gateway.dev.yaml", "config file (local; copy from etc/gateway.dev.example.yaml)")
|
2026-05-20 07:01:08 +00:00
|
|
|
|
|
|
|
|
func main() {
|
2026-05-20 13:03:59 +00:00
|
|
|
if err := run(); err != nil {
|
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func run() error {
|
2026-05-20 07:01:08 +00:00
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
|
|
var c config.Config
|
|
|
|
|
conf.MustLoad(*configFile, &c)
|
|
|
|
|
if c.Mongo.Host == "" {
|
2026-05-20 13:03:59 +00:00
|
|
|
return fmt.Errorf("mongo-index: Mongo.Host is empty in config")
|
2026-05-20 07:01:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 {
|
2026-05-20 13:03:59 +00:00
|
|
|
return fmt.Errorf("mongo-index: notifications: %w", err)
|
2026-05-20 07:01:08 +00:00
|
|
|
}
|
|
|
|
|
if err := dlqRepo.Index20260520001UP(ctx); err != nil {
|
2026-05-20 13:03:59 +00:00
|
|
|
return fmt.Errorf("mongo-index: notification_dlq: %w", err)
|
2026-05-20 07:01:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fmt.Println("mongo-index: notifications + notification_dlq indexes OK")
|
2026-05-20 13:03:59 +00:00
|
|
|
return nil
|
2026-05-20 07:01:08 +00:00
|
|
|
}
|