72 lines
1.8 KiB
Go
72 lines
1.8 KiB
Go
package panel
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
"time"
|
|
|
|
"github.com/leolionad58/ticketbot/internal/config"
|
|
"github.com/leolionad58/ticketbot/internal/db"
|
|
)
|
|
|
|
// MigrateYAMLPanels copies panel configuration from the YAML config into panel_configs and
|
|
// panel_types tables. It runs only once: if panel_configs is already non-empty it is a no-op.
|
|
func MigrateYAMLPanels(ctx context.Context, repo *db.PanelConfigRepo, cfg *config.Config, guildID string) error {
|
|
empty, err := repo.IsEmpty(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !empty || len(cfg.Panels) == 0 {
|
|
return nil
|
|
}
|
|
|
|
slog.Info("migrating YAML panels to DB", "count", len(cfg.Panels))
|
|
order := 0
|
|
for panelName, panel := range cfg.Panels {
|
|
pc := &db.PanelConfig{
|
|
Name: panelName,
|
|
EmbedTitle: panel.EmbedTitle,
|
|
EmbedColor: panel.EmbedColor,
|
|
GuildID: guildID,
|
|
SortOrder: order,
|
|
}
|
|
if panel.EmbedDescription != "" {
|
|
pc.EmbedDescription.String = panel.EmbedDescription
|
|
pc.EmbedDescription.Valid = true
|
|
}
|
|
if err := repo.Create(ctx, pc); err != nil {
|
|
return err
|
|
}
|
|
|
|
typeOrder := 0
|
|
for typeName, t := range panel.Types {
|
|
pt := &db.PanelType{
|
|
PanelID: pc.ID,
|
|
Name: typeName,
|
|
ButtonLabel: t.ButtonLabel,
|
|
ButtonColor: string(t.ButtonColor),
|
|
ButtonEmoji: t.ButtonEmoji,
|
|
EmbedColor: t.EmbedColor,
|
|
EmbedTitle: t.EmbedTitle,
|
|
EmbedText: t.EmbedText,
|
|
StaffRoleID: t.StaffRole,
|
|
CategoryID: t.Category,
|
|
MaxPerUser: 1,
|
|
ClaimMode: 1,
|
|
CloseRule: "staff_only",
|
|
ModalEnabled: true,
|
|
SortOrder: typeOrder,
|
|
CreatedAt: time.Now().UTC(),
|
|
UpdatedAt: time.Now().UTC(),
|
|
}
|
|
if err := repo.CreateType(ctx, pt); err != nil {
|
|
return err
|
|
}
|
|
typeOrder++
|
|
}
|
|
order++
|
|
}
|
|
slog.Info("YAML panels migrated to DB", "panels", len(cfg.Panels))
|
|
return nil
|
|
}
|