~ashkeel/strimertul

bba0bce1ec9da633222977cbe821830cd421a403 — Ash Keel 7 months ago 2b7f332
fix: prevent migration from breaking new installs
1 files changed, 16 insertions(+), 7 deletions(-)

M migrations/v3_v4.go
M migrations/v3_v4.go => migrations/v3_v4.go +16 -7
@@ 2,6 2,8 @@ package migrations

import (
	"encoding/json"
	"errors"
	"fmt"
	"log/slog"

	"git.sr.ht/~ashkeel/strimertul/twitch/eventsub"


@@ 23,7 25,7 @@ func migrateToV4(db database.Database, logger *slog.Logger) error {
		"twitch/bot-custom-commands":       chat.CustomCommandsKey,
	} {
		if err := renameKey(db, oldKey, newKey, true); err != nil {
			return err
			return fmt.Errorf("failed to rename key '%s' to '%s': %w", oldKey, newKey, err)
		}
	}



@@ 37,7 39,7 @@ func migrateToV4(db database.Database, logger *slog.Logger) error {
		"twitch/ev/eventsub-event",
	} {
		if err := db.RemoveKey(key); err != nil {
			return err
			return fmt.Errorf("failed to remove key '%s': %w", key, err)
		}
	}



@@ 46,12 48,16 @@ func migrateToV4(db database.Database, logger *slog.Logger) error {
		CommandCooldown int `json:"command_cooldown"`
	}
	if err := db.GetJSON("twitch/bot-config", &botConfig); err != nil {
		return err
		if errors.Is(err, database.ErrEmptyKey) {
			botConfig.CommandCooldown = 0
		} else {
			return fmt.Errorf("failed to get bot config: %w", err)
		}
	}
	if err := db.PutJSON(chat.ConfigKey, chat.Config{
		CommandCooldown: botConfig.CommandCooldown,
	}); err != nil {
		return err
		return fmt.Errorf("failed to put chat config to new key: %w", err)
	}

	// Migrate eventsub history to their new keys


@@ 61,7 67,10 @@ func migrateToV4(db database.Database, logger *slog.Logger) error {
	}
	var eventsubHistory []eventSubNotification
	if err := db.GetJSON("twitch/eventsub-history", &eventsubHistory); err != nil {
		return err
		if !errors.Is(err, database.ErrEmptyKey) {
			return fmt.Errorf("failed to get eventsub history: %w", err)
		}
		eventsubHistory = []eventSubNotification{}
	}
	eventsubHistoryMap := make(map[string][]eventSubNotification)
	for _, notification := range eventsubHistory {


@@ 70,12 79,12 @@ func migrateToV4(db database.Database, logger *slog.Logger) error {
	}
	for key, notifications := range eventsubHistoryMap {
		if err := db.PutJSON(key, notifications); err != nil {
			return err
			return fmt.Errorf("failed to put eventsub history to new key '%s': %w", key, err)
		}
	}
	// Clear old eventsub history key
	if err := db.RemoveKey("twitch/eventsub-history"); err != nil {
		return err
		return fmt.Errorf("failed to remove key 'twitch/eventsub-history': %w", err)
	}

	return nil