diff --git a/internal/cache/sqlite/sqlite.go b/internal/cache/sqlite/sqlite.go index 860ac0e..ff29082 100644 --- a/internal/cache/sqlite/sqlite.go +++ b/internal/cache/sqlite/sqlite.go @@ -2,6 +2,7 @@ package sqlite import ( "fmt" + "strings" "github.com/davidallendj/magellan/internal/util" magellan "github.com/davidallendj/magellan/pkg" @@ -113,13 +114,22 @@ func DeleteRemoteAssets(path string, assets ...magellan.RemoteAsset) error { } tx = db.MustBegin() for _, asset := range assets { + // skip if neither host nor port are specified if asset.Host == "" && asset.Port <= 0 { continue } - sql := fmt.Sprintf(`DELETE FROM %s WHERE port=:port;`, TABLE_NAME) - if asset.Host != "" { - sql += "AND host=:host" + sql := fmt.Sprintf(`DELETE FROM %s`, TABLE_NAME) + where := []string{} + if asset.Port > 0 { + where = append(where, "port=:port") } + if asset.Host != "" { + where = append(where, "host=:host") + } + if len(where) <= 0 { + continue + } + sql += fmt.Sprintf(" WHERE %s;", strings.Join(where, " AND ")) _, err := tx.NamedExec(sql, &asset) if err != nil { fmt.Printf("failed to execute DELETE transaction: %v\n", err)