Migrate forbidden and suggested usernames list to string slice. Closes #1873
This commit is contained in:
parent
a29d6450cb
commit
b2b791b365
@ -630,44 +630,39 @@ func FindHighestVideoQualityIndex(qualities []models.StreamOutputVariant) int {
|
||||
|
||||
// GetForbiddenUsernameList will return the blocked usernames as a comma separated string.
|
||||
func GetForbiddenUsernameList() []string {
|
||||
usernameString, err := _datastore.GetString(blockedUsernamesKey)
|
||||
usernames, err := _datastore.GetStringSlice(blockedUsernamesKey)
|
||||
if err != nil {
|
||||
return config.DefaultForbiddenUsernames
|
||||
}
|
||||
|
||||
if usernameString == "" {
|
||||
if len(usernames) == 0 {
|
||||
return config.DefaultForbiddenUsernames
|
||||
}
|
||||
|
||||
blocklist := strings.Split(usernameString, ",")
|
||||
|
||||
return blocklist
|
||||
return usernames
|
||||
}
|
||||
|
||||
// SetForbiddenUsernameList set the username blocklist as a comma separated string.
|
||||
func SetForbiddenUsernameList(usernames []string) error {
|
||||
usernameListString := strings.Join(usernames, ",")
|
||||
return _datastore.SetString(blockedUsernamesKey, usernameListString)
|
||||
return _datastore.SetStringSlice(blockedUsernamesKey, usernames)
|
||||
}
|
||||
|
||||
// GetSuggestedUsernamesList will return the suggested usernames as a comma separated string.
|
||||
// If the number of suggested usernames is smaller than 10, the number pool is not used (see code in the CreateAnonymousUser function).
|
||||
// GetSuggestedUsernamesList will return the suggested usernames.
|
||||
// If the number of suggested usernames is smaller than 10, the number pool is
|
||||
// not used (see code in the CreateAnonymousUser function).
|
||||
func GetSuggestedUsernamesList() []string {
|
||||
usernameString, err := _datastore.GetString(suggestedUsernamesKey)
|
||||
usernames, err := _datastore.GetStringSlice(suggestedUsernamesKey)
|
||||
|
||||
if err != nil || usernameString == "" {
|
||||
if err != nil || len(usernames) == 0 {
|
||||
return []string{}
|
||||
}
|
||||
|
||||
suggestionList := strings.Split(usernameString, ",")
|
||||
|
||||
return suggestionList
|
||||
return usernames
|
||||
}
|
||||
|
||||
// SetSuggestedUsernamesList sets the username suggestion list as a comma separated string.
|
||||
// SetSuggestedUsernamesList sets the username suggestion list.
|
||||
func SetSuggestedUsernamesList(usernames []string) error {
|
||||
usernameListString := strings.Join(usernames, ",")
|
||||
return _datastore.SetString(suggestedUsernamesKey, usernameListString)
|
||||
return _datastore.SetStringSlice(suggestedUsernamesKey, usernames)
|
||||
}
|
||||
|
||||
// GetServerInitTime will return when the server was first setup.
|
||||
|
@ -12,6 +12,13 @@ type ConfigEntry struct {
|
||||
Value interface{}
|
||||
}
|
||||
|
||||
func (c *ConfigEntry) getStringSlice() ([]string, error) {
|
||||
decoder := c.getDecoder()
|
||||
var result []string
|
||||
err := decoder.Decode(&result)
|
||||
return result, err
|
||||
}
|
||||
|
||||
func (c *ConfigEntry) getString() (string, error) {
|
||||
decoder := c.getDecoder()
|
||||
var result string
|
||||
|
@ -108,7 +108,7 @@ func SetupPersistence(file string) error {
|
||||
|
||||
// is database schema outdated?
|
||||
if version < schemaVersion {
|
||||
if err := migrateDatabase(db, version, schemaVersion); err != nil {
|
||||
if err := migrateDatabaseSchema(db, version, schemaVersion); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@ -126,33 +126,3 @@ func SetupPersistence(file string) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func migrateDatabase(db *sql.DB, from, to int) error {
|
||||
log.Printf("Migrating database from version %d to %d", from, to)
|
||||
dbBackupFile := filepath.Join(config.BackupDirectory, fmt.Sprintf("owncast-v%d.bak", from))
|
||||
utils.Backup(db, dbBackupFile)
|
||||
for v := from; v < to; v++ {
|
||||
log.Tracef("Migration step from %d to %d\n", v, v+1)
|
||||
switch v {
|
||||
case 0:
|
||||
migrateToSchema1(db)
|
||||
case 1:
|
||||
migrateToSchema2(db)
|
||||
case 2:
|
||||
migrateToSchema3(db)
|
||||
case 3:
|
||||
migrateToSchema4(db)
|
||||
case 4:
|
||||
migrateToSchema5(db)
|
||||
default:
|
||||
log.Fatalln("missing database migration step")
|
||||
}
|
||||
}
|
||||
|
||||
_, err := db.Exec("UPDATE config SET value = ? WHERE key = ?", to, "version")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
49
core/data/datastoreMigrations.go
Normal file
49
core/data/datastoreMigrations.go
Normal file
@ -0,0 +1,49 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
const (
|
||||
datastoreValuesVersion = 1
|
||||
datastoreValueVersionKey = "DATA_STORE_VERSION"
|
||||
)
|
||||
|
||||
func migrateDatastoreValues(datastore *Datastore) {
|
||||
currentVersion, _ := _datastore.GetNumber(datastoreValueVersionKey)
|
||||
|
||||
for v := currentVersion; v < datastoreValuesVersion; v++ {
|
||||
log.Tracef("Migration datastore values from %d to %d\n", int(v), int(v+1))
|
||||
switch v {
|
||||
case 0:
|
||||
migrateToDatastoreValues1(datastore)
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
@ -2,13 +2,46 @@ package data
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/owncast/owncast/config"
|
||||
"github.com/owncast/owncast/utils"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/teris-io/shortid"
|
||||
)
|
||||
|
||||
func migrateDatabaseSchema(db *sql.DB, from, to int) error {
|
||||
log.Printf("Migrating database from version %d to %d", from, to)
|
||||
dbBackupFile := filepath.Join(config.BackupDirectory, fmt.Sprintf("owncast-v%d.bak", from))
|
||||
utils.Backup(db, dbBackupFile)
|
||||
for v := from; v < to; v++ {
|
||||
log.Tracef("Migration step from %d to %d\n", v, v+1)
|
||||
switch v {
|
||||
case 0:
|
||||
migrateToSchema1(db)
|
||||
case 1:
|
||||
migrateToSchema2(db)
|
||||
case 2:
|
||||
migrateToSchema3(db)
|
||||
case 3:
|
||||
migrateToSchema4(db)
|
||||
case 4:
|
||||
migrateToSchema5(db)
|
||||
default:
|
||||
log.Fatalln("missing database migration step")
|
||||
}
|
||||
}
|
||||
|
||||
_, err := db.Exec("UPDATE config SET value = ? WHERE key = ?", to, "version")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// nolint:cyclop
|
||||
func migrateToSchema5(db *sql.DB) {
|
||||
// Create the access tokens table.
|
||||
|
@ -147,6 +147,8 @@ func (ds *Datastore) Setup() {
|
||||
if hasSetInitDate, _ := GetServerInitTime(); hasSetInitDate == nil || !hasSetInitDate.Valid {
|
||||
_ = SetServerInitTime(time.Now())
|
||||
}
|
||||
|
||||
migrateDatastoreValues(_datastore)
|
||||
}
|
||||
|
||||
// Reset will delete all config entries in the datastore and start over.
|
||||
|
@ -1,5 +1,20 @@
|
||||
package data
|
||||
|
||||
// GetStringSlice will return the string slice value for a key.
|
||||
func (ds *Datastore) GetStringSlice(key string) ([]string, error) {
|
||||
configEntry, err := ds.Get(key)
|
||||
if err != nil {
|
||||
return []string{}, err
|
||||
}
|
||||
return configEntry.getStringSlice()
|
||||
}
|
||||
|
||||
// SetStringSlice will set the string slice value for a key.
|
||||
func (ds *Datastore) SetStringSlice(key string, value []string) error {
|
||||
configEntry := ConfigEntry{key, value}
|
||||
return ds.Save(configEntry)
|
||||
}
|
||||
|
||||
// GetString will return the string value for a key.
|
||||
func (ds *Datastore) GetString(key string) (string, error) {
|
||||
configEntry, err := ds.Get(key)
|
||||
|
Loading…
x
Reference in New Issue
Block a user