29 lines
750 B
Go
29 lines
750 B
Go
package store
|
|
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
func migrateUserProfileColumns(db *sql.DB, dialect string) error {
|
|
cols := []string{"avatar_url", "state"}
|
|
for _, col := range cols {
|
|
var stmt string
|
|
switch dialect {
|
|
case dialectPostgres:
|
|
stmt = fmt.Sprintf(`ALTER TABLE users ADD COLUMN IF NOT EXISTS %s TEXT NOT NULL DEFAULT ''`, col)
|
|
default:
|
|
stmt = fmt.Sprintf(`ALTER TABLE users ADD COLUMN %s TEXT NOT NULL DEFAULT ''`, col)
|
|
}
|
|
if _, err := db.Exec(stmt); err != nil {
|
|
// SQLite errors when the column already exists.
|
|
if dialect == dialectSQLite && strings.Contains(strings.ToLower(err.Error()), "duplicate column") {
|
|
continue
|
|
}
|
|
return fmt.Errorf("add column %s: %w", col, err)
|
|
}
|
|
}
|
|
return nil
|
|
}
|