Add user color database migration. Closes #1984

This commit is contained in:
Gabe Kangas 2022-10-17 21:44:12 -07:00
parent 8689061498
commit 106e144cba
No known key found for this signature in database
GPG Key ID: 9A56337728BC81EA
2 changed files with 47 additions and 1 deletions

View File

@ -17,7 +17,7 @@ import (
)
const (
schemaVersion = 6
schemaVersion = 7
)
var (

View File

@ -31,6 +31,8 @@ func migrateDatabaseSchema(db *sql.DB, from, to int) error {
migrateToSchema5(db)
case 5:
migrateToSchema6(db)
case 6:
migrateToSchema7(db)
default:
log.Fatalln("missing database migration step")
}
@ -44,6 +46,50 @@ func migrateDatabaseSchema(db *sql.DB, from, to int) error {
return nil
}
func migrateToSchema7(db *sql.DB) {
log.Println("Migrating users. This may take time if you have lots of users...")
var ids []string
rows, err := db.Query(`SELECT id FROM users`)
if err != nil {
log.Errorln("error migrating access tokens to schema v5", err)
return
}
if rows.Err() != nil {
log.Errorln("error migrating users to schema v7", rows.Err())
return
}
for rows.Next() {
var id string
if err := rows.Scan(&id); err != nil {
log.Error("There is a problem reading the database when migrating users.", err)
return
}
ids = append(ids, id)
}
defer rows.Close()
tx, _ := db.Begin()
stmt, _ := tx.Prepare("update users set display_color=? WHERE id=?")
defer stmt.Close()
for _, id := range ids {
displayColor := utils.GenerateRandomDisplayColor(config.MaxUserColor)
if _, err := stmt.Exec(displayColor, id); err != nil {
log.Panic(err)
return
}
}
if err := tx.Commit(); err != nil {
log.Panicln(err)
}
}
func migrateToSchema6(db *sql.DB) {
// Fix chat messages table schema. Since chat is ephemeral we can drop
// the table and recreate it.