changeset 2999:b3c3c5b5b7c1

Bottleneck import: Import riverbed materials, too.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 10 Apr 2019 16:44:30 +0200
parents adc8e9ccf706
children 85d1f3e94945
files pkg/imports/bn.go schema/gemma.sql
diffstat 2 files changed, 30 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/imports/bn.go	Wed Apr 10 16:04:40 2019 +0200
+++ b/pkg/imports/bn.go	Wed Apr 10 16:44:30 2019 +0200
@@ -81,6 +81,15 @@
   $13
 )
 RETURNING id`
+
+	insertBottleneckMaterialSQL = `
+INSERT INTO waterway.bottlenecks_riverbed_materials (
+   bottleneck_id,
+   riverbed
+) VALUES (
+   $1,
+   $2
+)`
 )
 
 type bnJobCreator struct{}
@@ -187,7 +196,7 @@
 
 	feedback.Info("Found %d bottlenecks for import", len(bns))
 
-	var hasStmt, insertStmt, trackStmt *sql.Stmt
+	var hasStmt, insertStmt, insertMaterialStmt, trackStmt *sql.Stmt
 
 	for _, x := range []struct {
 		sql  string
@@ -195,6 +204,7 @@
 	}{
 		{hasBottleneckSQL, &hasStmt},
 		{insertBottleneckSQL, &insertStmt},
+		{insertBottleneckMaterialSQL, &insertMaterialStmt},
 		{trackImportSQL, &trackStmt},
 	} {
 		var err error
@@ -211,7 +221,7 @@
 	for _, bn := range bns {
 		if err := storeBottleneck(
 			ctx, importID, conn, feedback, bn, &nids, tolerance,
-			hasStmt, insertStmt, trackStmt); err != nil {
+			hasStmt, insertStmt, insertMaterialStmt, trackStmt); err != nil {
 			return nil, err
 		}
 	}
@@ -237,7 +247,7 @@
 	bn *ifbn.BottleNeckType,
 	nids *[]string,
 	tolerance float64,
-	hasStmt, insertStmt, trackStmt *sql.Stmt,
+	hasStmt, insertStmt, insertMaterialStmt, trackStmt *sql.Stmt,
 ) error {
 
 	tx, err := conn.BeginTx(ctx, nil)
@@ -295,6 +305,21 @@
 		return nil
 	}
 
+	if bn.Riverbed != nil {
+		for _, material := range bn.Riverbed.Material {
+			if material != nil {
+				mat := string(*material)
+				if _, err := tx.Stmt(insertMaterialStmt).ExecContext(
+					ctx, nid, material); err != nil {
+					feedback.Warn(
+						"Failed to insert riverbed material '%s' for bottleneck '%s'.",
+						mat, bn.OBJNAM)
+					feedback.Warn(handleError(err).Error())
+				}
+			}
+		}
+	}
+
 	if _, err := tx.Stmt(trackStmt).ExecContext(
 		ctx, importID, "waterway.bottlenecks", nid,
 	); err != nil {
--- a/schema/gemma.sql	Wed Apr 10 16:04:40 2019 +0200
+++ b/schema/gemma.sql	Wed Apr 10 16:44:30 2019 +0200
@@ -547,7 +547,8 @@
         FOR EACH ROW EXECUTE PROCEDURE update_date_info()
 
     CREATE TABLE bottlenecks_riverbed_materials (
-        bottleneck_id int NOT NULL REFERENCES bottlenecks(id),
+        bottleneck_id int NOT NULL REFERENCES bottlenecks(id)
+            ON DELETE CASCADE,
         riverbed varchar NOT NULL REFERENCES riverbed_materials,
         -- XXX: should be 'natsur' according to IENC Encoding Guide M.4.3
         PRIMARY KEY (bottleneck_id, riverbed)