2022-04-25 23:10:20 +02:00
|
|
|
package data
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
2022-11-24 01:39:55 +01:00
|
|
|
"github.com/owncast/owncast/models"
|
2022-04-25 23:10:20 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-11-23 07:08:25 +01:00
|
|
|
datastoreValuesVersion = 2
|
2022-04-25 23:10:20 +02:00
|
|
|
datastoreValueVersionKey = "DATA_STORE_VERSION"
|
|
|
|
)
|
|
|
|
|
|
|
|
func migrateDatastoreValues(datastore *Datastore) {
|
|
|
|
currentVersion, _ := _datastore.GetNumber(datastoreValueVersionKey)
|
2022-11-23 07:08:25 +01:00
|
|
|
if currentVersion == 0 {
|
|
|
|
currentVersion = datastoreValuesVersion
|
|
|
|
}
|
2022-04-25 23:10:20 +02:00
|
|
|
|
|
|
|
for v := currentVersion; v < datastoreValuesVersion; v++ {
|
2022-11-23 07:08:25 +01:00
|
|
|
log.Infof("Migration datastore values from %d to %d\n", int(v), int(v+1))
|
2022-04-25 23:10:20 +02:00
|
|
|
switch v {
|
|
|
|
case 0:
|
|
|
|
migrateToDatastoreValues1(datastore)
|
2022-11-23 07:08:25 +01:00
|
|
|
case 1:
|
|
|
|
migrateToDatastoreValues2(datastore)
|
2022-04-25 23:10:20 +02:00
|
|
|
default:
|
|
|
|
log.Fatalln("missing datastore values migration step")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := _datastore.SetNumber(datastoreValueVersionKey, datastoreValuesVersion); err != nil {
|
|
|
|
log.Errorln("error setting datastore value version:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func migrateToDatastoreValues1(datastore *Datastore) {
|
|
|
|
// Migrate the forbidden usernames to be a slice instead of a string.
|
|
|
|
forbiddenUsernamesString, _ := datastore.GetString(blockedUsernamesKey)
|
|
|
|
if forbiddenUsernamesString != "" {
|
|
|
|
forbiddenUsernamesSlice := strings.Split(forbiddenUsernamesString, ",")
|
|
|
|
if err := datastore.SetStringSlice(blockedUsernamesKey, forbiddenUsernamesSlice); err != nil {
|
|
|
|
log.Errorln("error migrating blocked username list:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Migrate the suggested usernames to be a slice instead of a string.
|
|
|
|
suggestedUsernamesString, _ := datastore.GetString(suggestedUsernamesKey)
|
|
|
|
if suggestedUsernamesString != "" {
|
|
|
|
suggestedUsernamesSlice := strings.Split(suggestedUsernamesString, ",")
|
|
|
|
if err := datastore.SetStringSlice(suggestedUsernamesKey, suggestedUsernamesSlice); err != nil {
|
|
|
|
log.Errorln("error migrating suggested username list:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-11-23 07:08:25 +01:00
|
|
|
|
|
|
|
func migrateToDatastoreValues2(datastore *Datastore) {
|
|
|
|
oldAdminPassword, _ := datastore.GetString("stream_key")
|
|
|
|
_ = SetAdminPassword(oldAdminPassword)
|
2022-11-24 01:39:55 +01:00
|
|
|
_ = SetStreamKeys([]models.StreamKey{
|
|
|
|
{Key: oldAdminPassword, Comment: "Default stream key"},
|
|
|
|
})
|
2022-11-23 07:08:25 +01:00
|
|
|
}
|