package migrations

import (
	"errors"

	"git.sr.ht/~ashkeel/strimertul/database"
)

func renameKey(db database.Database, oldKey, newKey string, ignoreMissing bool) error {
	value, err := db.GetKey(oldKey)
	if err != nil {
		if ignoreMissing && errors.Is(err, database.ErrEmptyKey) {
			return nil
		}
		return err
	}

	if err = db.PutKey(newKey, value); err != nil {
		return err
	}

	return db.RemoveKey(oldKey)
}
