changeset 4895:9f799077a3e6 fairway-marks-import

Prevent importing non-distinct fairway marks
author Tom Gottfried <tom@intevation.de>
date Tue, 14 Jan 2020 18:24:51 +0100
parents 8eb36d0d5bdf
children dd888bbb7e43
files pkg/imports/fm.go schema/gemma.sql
diffstat 2 files changed, 22 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/imports/fm.go	Tue Jan 14 15:01:42 2020 +0100
+++ b/pkg/imports/fm.go	Tue Jan 14 18:24:51 2020 +0100
@@ -135,6 +135,13 @@
   WHERE pg_has_role('sys_admin', 'MEMBER')
     OR ST_Intersects((select a from a),
       ST_Transform(newfm, (select ST_SRID(a) from a)))
+ON CONFLICT (
+    CAST((0, geom,
+      datsta, datend, persta, perend, objnam, nobjnm, inform, ninfom,
+      scamin, picrep, txtdsc, sordat, sorind, colour, colpat, condtn,
+      bcnshp, catlam, dirimp) AS waterway.fairway_marks)
+  )
+  DO NOTHING
 RETURNING id
 `
 )
@@ -197,7 +204,7 @@
 		unsupported       = stringCounter{}
 		missingProperties int
 		badProperties     int
-		outside           int
+		outsideOrDup      int
 		features          int
 	)
 
@@ -287,7 +294,7 @@
 				})
 				switch {
 				case err == sql.ErrNoRows:
-					outside++
+					outsideOrDup++
 					// ignore -> filtered by responsibility_areas
 				case err != nil:
 					feedback.Error(pgxutils.ReadableError{Err: err}.Error())
@@ -315,13 +322,14 @@
 		feedback.Warn("Unsupported types found: %s", unsupported)
 	}
 
-	if outside > 0 {
-		feedback.Info("Features outside responsibility area: %d", outside)
+	if outsideOrDup > 0 {
+		feedback.Info(
+			"Features outside responsibility area and duplicates: %d",
+			outsideOrDup)
 	}
 
 	if features == 0 {
-		err := errors.New("no features found")
-		feedback.Error("%v", err)
+		err := errors.New("no valid new features found")
 		return nil, err
 	}
 
--- a/schema/gemma.sql	Tue Jan 14 15:01:42 2020 +0100
+++ b/schema/gemma.sql	Tue Jan 14 18:24:51 2020 +0100
@@ -864,6 +864,14 @@
         catlam int,
         dirimp smallint REFERENCES dirimps
     )
+    -- Prevent identical entries using composite type comparison
+    -- (i.e. considering two NULL values in a field equal):
+    CREATE UNIQUE INDEX fairway_marks_distinct_rows ON fairway_marks
+        ((CAST((0, geom,
+            datsta, datend, persta, perend, objnam, nobjnm, inform, ninfom,
+            scamin, picrep, txtdsc, sordat, sorind, colour, colpat, condtn,
+            bcnshp, catlam, dirimp) AS fairway_marks)
+        ))
 ;