Skip existing entries in db when converting

chubin/logging
Igor Chubin 2 years ago
parent 38e2ddd69b
commit ecc9479719

@ -1,6 +1,8 @@
package location package location
import ( import (
"database/sql"
"errors"
"fmt" "fmt"
"log" "log"
"os" "os"
@ -14,7 +16,7 @@ import (
// ConvertCache converts files-based cache into the DB-based cache. // ConvertCache converts files-based cache into the DB-based cache.
// If reset is true, the DB cache is created from scratch. // If reset is true, the DB cache is created from scratch.
// //
//nolint:funlen,cyclop,gocognit //nolint:funlen,cyclop
func (c *Cache) ConvertCache(reset bool) error { func (c *Cache) ConvertCache(reset bool) error {
var ( var (
dbfile = c.config.Geo.LocationCacheDB dbfile = c.config.Geo.LocationCacheDB
@ -60,12 +62,28 @@ func (c *Cache) ConvertCache(reset bool) error {
continue continue
} }
// Skip too long location names.
if len(loc.Name) > 25 {
continue
}
// Skip duplicates. // Skip duplicates.
if known[loc.Name] { if known[loc.Name] {
log.Println("skipping", loc.Name) log.Println("skipping", loc.Name)
continue continue
} }
singleLocation := Location{}
err = db.Select(&singleLocation).
Where("name = ?", loc.Name).
Do()
if !errors.Is(err, sql.ErrNoRows) {
log.Println("found in db:", loc.Name)
continue
}
known[loc.Name] = true known[loc.Name] = true
// Skip some invalid names. // Skip some invalid names.

Loading…
Cancel
Save