From ecc9479719209274b7f0eff9128815ad30486773 Mon Sep 17 00:00:00 2001 From: Igor Chubin Date: Fri, 23 Dec 2022 14:45:59 +0100 Subject: [PATCH] Skip existing entries in db when converting --- internal/geo/location/convert.go | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/internal/geo/location/convert.go b/internal/geo/location/convert.go index 11343f6..7219cb3 100644 --- a/internal/geo/location/convert.go +++ b/internal/geo/location/convert.go @@ -1,6 +1,8 @@ package location import ( + "database/sql" + "errors" "fmt" "log" "os" @@ -14,7 +16,7 @@ import ( // ConvertCache converts files-based cache into the DB-based cache. // 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 { var ( dbfile = c.config.Geo.LocationCacheDB @@ -60,12 +62,28 @@ func (c *Cache) ConvertCache(reset bool) error { continue } + // Skip too long location names. + if len(loc.Name) > 25 { + continue + } + // Skip duplicates. if known[loc.Name] { log.Println("skipping", loc.Name) 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 // Skip some invalid names.