diff pkg/imports/bn.go @ 1572:056a86b24be2

Made bottleneck primary key an int. Attention: This may break something!
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 12 Dec 2018 19:21:02 +0100
parents cf0ca4a9812a
children c503d2fa9776
line wrap: on
line diff
--- a/pkg/imports/bn.go	Wed Dec 12 17:21:11 2018 +0100
+++ b/pkg/imports/bn.go	Wed Dec 12 19:21:02 2018 +0100
@@ -32,7 +32,12 @@
 
 const BNJobKind JobKind = "bn"
 
-const insertSQL = `INSERT INTO waterway.bottlenecks (
+const (
+	hasBottleneckSQL = `
+SELECT true FROM waterway.bottlenecks WHERE bottleneck_id = $1`
+
+	insertSQL = `
+INSERT INTO waterway.bottlenecks (
   bottleneck_id,
   fk_g_fid,
   objnam,
@@ -60,7 +65,9 @@
   $11,
   $12,
   $13
-) ON CONFLICT (bottleneck_id) DO NOTHING`
+)
+RETURNING id`
+)
 
 type bnJobCreator struct{}
 
@@ -156,9 +163,35 @@
 	}
 	defer tx.Rollback()
 
+	hasStmt, err := tx.PrepareContext(ctx, hasBottleneckSQL)
+	if err != nil {
+		return nil, err
+	}
+	defer hasStmt.Close()
+	insertStmt, err := tx.PrepareContext(ctx, insertSQL)
+	if err != nil {
+		return nil, err
+	}
+	defer insertStmt.Close()
+
+	var nids []int64
+
 	start := time.Now()
-	for i := range bns {
-		bn := bns[i]
+
+nextBN:
+	for _, bn := range bns {
+
+		var found bool
+		err := hasStmt.QueryRowContext(ctx, bn.Bottleneck_id).Scan(&found)
+		switch {
+		case err == sql.ErrNoRows:
+			// This is good.
+		case err != nil:
+			return nil, err
+		case found:
+			continue nextBN
+		}
+
 		rb, lb := splitRBLB(bn.Rb_lb)
 
 		var limiting, country string
@@ -171,7 +204,10 @@
 			country = string(*bn.Responsible_country)
 		}
 
-		tx.Exec(insertSQL,
+		var nid int64
+
+		err = insertStmt.QueryRowContext(
+			ctx,
 			bn.Bottleneck_id,
 			bn.Fk_g_fid,
 			bn.OBJNAM,
@@ -184,24 +220,31 @@
 			limiting,
 			bn.Date_Info,
 			bn.Source,
-		)
-		feedback.Info("Insert \"%s\" into database", bn.OBJNAM)
-		//TODO: Track for potential later removal? Bottlenecks have an string PK, track wants int64.
-		// if err = track(ctx, tx, importID, "waterway.bottlenecks", bn.Bottleneck_id); err != nil {
-		// 	return nil, err
-		// }
+		).Scan(&nid)
+		if err != nil {
+			return nil, err
+		}
+		nids = append(nids, nid)
+		feedback.Info("Inserted '%s'into database", bn.OBJNAM)
+		if err := track(ctx, tx, importID, "waterway.bottlenecks", nid); err != nil {
+			return nil, err
+		}
+	}
+	if len(nids) == 0 {
+		feedback.Error("No new bottlenecks found")
+		return nil, errors.New("No new bottlenecks found")
 	}
 
-	feedback.Info("Storing %d bottlenecks took %s", len(bns), time.Since(start))
+	feedback.Info("Storing %d bottlenecks took %s", len(nids), time.Since(start))
 	if err = tx.Commit(); err == nil {
 		feedback.Info("Import of bottlenecks was successful")
 	}
 
-	// TODO: needs to be filled.
+	// TODO: needs to be filled more useful.
 	summary := struct {
-		BottleneckCount int `json:"bottleneckCount"`
+		Bottlenecks []int64 `json:"bottlenecks"`
 	}{
-		BottleneckCount: len(bns),
+		Bottlenecks: nids,
 	}
 	return &summary, err
 }