Fixed removing from cache with --with-* flags

This commit is contained in:
David Allen 2024-09-19 11:21:08 -06:00
parent 4cf854313a
commit 3f12b093f9
Signed by: towk
GPG key ID: 793B2924A49B3A3F
2 changed files with 40 additions and 8 deletions

View file

@ -2,6 +2,7 @@ package sqlite
import (
"fmt"
"strings"
magellan "github.com/OpenCHAMI/magellan/internal"
"github.com/OpenCHAMI/magellan/internal/util"
@ -60,22 +61,36 @@ func InsertScannedAssets(path string, assets ...magellan.RemoteAsset) error {
}
func DeleteScannedAssets(path string, assets ...magellan.RemoteAsset) error {
var (
db *sqlx.DB
tx *sqlx.Tx
err error
)
if assets == nil {
return fmt.Errorf("no assets found")
}
db, err := sqlx.Open("sqlite3", path)
db, err = sqlx.Open("sqlite3", path)
if err != nil {
return fmt.Errorf("failed to open database: %v", err)
}
tx := db.MustBegin()
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)