fix(backend): backfill legacy inx columns during sqlite migration

This commit is contained in:
sagit
2026-02-10 03:08:52 +00:00
parent d515d610b9
commit 32763358ea
+31 -22
View File
@@ -1262,35 +1262,44 @@ func migrateSchema(db *sql.DB) error {
return errors.New("nil db") return errors.New("nil db")
} }
// Add allowed_domains column if it doesn't exist (for existing tables) ensureColumn := func(table, col, typ string) {
var dummy interface{} var dummy interface{}
err := db.QueryRow("SELECT allowed_domains FROM peer_share LIMIT 1").Scan(&dummy) err := db.QueryRow(fmt.Sprintf("SELECT %s FROM %s LIMIT 1", col, table)).Scan(&dummy)
if err != nil { if err == nil || errors.Is(err, sql.ErrNoRows) {
return
}
if strings.Contains(err.Error(), "no such column") { if strings.Contains(err.Error(), "no such column") {
_, err = db.Exec("ALTER TABLE peer_share ADD COLUMN allowed_domains TEXT DEFAULT ''") if _, alterErr := db.Exec(fmt.Sprintf("ALTER TABLE %s ADD COLUMN %s %s", table, col, typ)); alterErr != nil {
if err != nil { log.Printf("failed to add column %s to %s: %v", col, table, alterErr)
log.Printf("failed to add column allowed_domains to peer_share: %v", err)
} }
} }
} }
columns := map[string]string{ columnsByTable := map[string]map[string]string{
"is_remote": "INTEGER DEFAULT 0", "peer_share": {
"remote_url": "TEXT", "allowed_domains": "TEXT DEFAULT ''",
"remote_token": "TEXT", },
"remote_config": "TEXT", "node": {
"inx": "INTEGER NOT NULL DEFAULT 0",
"is_remote": "INTEGER DEFAULT 0",
"remote_url": "TEXT",
"remote_token": "TEXT",
"remote_config": "TEXT",
},
"tunnel": {
"inx": "INTEGER NOT NULL DEFAULT 0",
},
"forward": {
"inx": "INTEGER NOT NULL DEFAULT 0",
},
"chain_tunnel": {
"inx": "INTEGER",
},
} }
for col, typ := range columns { for table, columns := range columnsByTable {
var dummy interface{} for col, typ := range columns {
err := db.QueryRow(fmt.Sprintf("SELECT %s FROM node LIMIT 1", col)).Scan(&dummy) ensureColumn(table, col, typ)
if err != nil {
if strings.Contains(err.Error(), "no such column") {
_, err = db.Exec(fmt.Sprintf("ALTER TABLE node ADD COLUMN %s %s", col, typ))
if err != nil {
log.Printf("failed to add column %s: %v", col, err)
}
}
} }
} }
return nil return nil