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.
|
// GetForbiddenUsernameList will return the blocked usernames as a comma separated string.
|
||||||
func GetForbiddenUsernameList() []string {
|
func GetForbiddenUsernameList() []string {
|
||||||
usernameString, err := _datastore.GetString(blockedUsernamesKey)
|
usernames, err := _datastore.GetStringSlice(blockedUsernamesKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return config.DefaultForbiddenUsernames
|
return config.DefaultForbiddenUsernames
|
||||||
}
|
}
|
||||||
|
|
||||||
if usernameString == "" {
|
if len(usernames) == 0 {
|
||||||
return config.DefaultForbiddenUsernames
|
return config.DefaultForbiddenUsernames
|
||||||
}
|
}
|
||||||
|
|
||||||
blocklist := strings.Split(usernameString, ",")
|
return usernames
|
||||||
|
|
||||||
return blocklist
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetForbiddenUsernameList set the username blocklist as a comma separated string.
|
// SetForbiddenUsernameList set the username blocklist as a comma separated string.
|
||||||
func SetForbiddenUsernameList(usernames []string) error {
|
func SetForbiddenUsernameList(usernames []string) error {
|
||||||
usernameListString := strings.Join(usernames, ",")
|
return _datastore.SetStringSlice(blockedUsernamesKey, usernames)
|
||||||
return _datastore.SetString(blockedUsernamesKey, usernameListString)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetSuggestedUsernamesList will return the suggested usernames as a comma separated string.
|
// 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).
|
// 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 {
|
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{}
|
return []string{}
|
||||||
}
|
}
|
||||||
|
|
||||||
suggestionList := strings.Split(usernameString, ",")
|
return usernames
|
||||||
|
|
||||||
return suggestionList
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetSuggestedUsernamesList sets the username suggestion list as a comma separated string.
|
// SetSuggestedUsernamesList sets the username suggestion list.
|
||||||
func SetSuggestedUsernamesList(usernames []string) error {
|
func SetSuggestedUsernamesList(usernames []string) error {
|
||||||
usernameListString := strings.Join(usernames, ",")
|
return _datastore.SetStringSlice(suggestedUsernamesKey, usernames)
|
||||||
return _datastore.SetString(suggestedUsernamesKey, usernameListString)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetServerInitTime will return when the server was first setup.
|
// GetServerInitTime will return when the server was first setup.
|
||||||
|
@ -12,6 +12,13 @@ type ConfigEntry struct {
|
|||||||
Value interface{}
|
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) {
|
func (c *ConfigEntry) getString() (string, error) {
|
||||||
decoder := c.getDecoder()
|
decoder := c.getDecoder()
|
||||||
var result string
|
var result string
|
||||||
|
@ -108,7 +108,7 @@ func SetupPersistence(file string) error {
|
|||||||
|
|
||||||
// is database schema outdated?
|
// is database schema outdated?
|
||||||
if version < schemaVersion {
|
if version < schemaVersion {
|
||||||
if err := migrateDatabase(db, version, schemaVersion); err != nil {
|
if err := migrateDatabaseSchema(db, version, schemaVersion); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -126,33 +126,3 @@ func SetupPersistence(file string) error {
|
|||||||
|
|
||||||
return nil
|
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 (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"fmt"
|
||||||
|
"path/filepath"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/owncast/owncast/config"
|
||||||
"github.com/owncast/owncast/utils"
|
"github.com/owncast/owncast/utils"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"github.com/teris-io/shortid"
|
"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
|
// nolint:cyclop
|
||||||
func migrateToSchema5(db *sql.DB) {
|
func migrateToSchema5(db *sql.DB) {
|
||||||
// Create the access tokens table.
|
// Create the access tokens table.
|
||||||
|
@ -147,6 +147,8 @@ func (ds *Datastore) Setup() {
|
|||||||
if hasSetInitDate, _ := GetServerInitTime(); hasSetInitDate == nil || !hasSetInitDate.Valid {
|
if hasSetInitDate, _ := GetServerInitTime(); hasSetInitDate == nil || !hasSetInitDate.Valid {
|
||||||
_ = SetServerInitTime(time.Now())
|
_ = SetServerInitTime(time.Now())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
migrateDatastoreValues(_datastore)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reset will delete all config entries in the datastore and start over.
|
// Reset will delete all config entries in the datastore and start over.
|
||||||
|
@ -1,5 +1,20 @@
|
|||||||
package data
|
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.
|
// GetString will return the string value for a key.
|
||||||
func (ds *Datastore) GetString(key string) (string, error) {
|
func (ds *Datastore) GetString(key string) (string, error) {
|
||||||
configEntry, err := ds.Get(key)
|
configEntry, err := ds.Get(key)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user