[04/10] importer: Sync geofeeds

Message ID 20220927164847.3409646-4-michael.tremer@ipfire.org
State Accepted
Commit ef8d1f676c62ba04edb919e09878521d410065d8
Headers
Series [01/10] importer: Store geofeed URLs from RIR data |

Commit Message

Michael Tremer Sept. 27, 2022, 4:48 p.m. UTC
  Geofeeds are kept in a separate table to only fetch them once per URL.
This needs to be kept in sync which is done before we update any feeds.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
---
 src/scripts/location-importer.in | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
  

Patch

diff --git a/src/scripts/location-importer.in b/src/scripts/location-importer.in
index 014f3b6..12035f1 100644
--- a/src/scripts/location-importer.in
+++ b/src/scripts/location-importer.in
@@ -1279,6 +1279,38 @@  class CLI(object):
 				yield line
 
 	def handle_update_geofeeds(self, ns):
+		# Sync geofeeds
+		with self.db.transaction():
+			# Delete all geofeeds which are no longer linked
+			self.db.execute("""
+				DELETE FROM
+					geofeeds
+				WHERE
+					NOT EXISTS (
+						SELECT
+							1
+						FROM
+							network_geofeeds
+						WHERE
+							geofeeds.url = network_geofeeds.url
+					)""",
+			)
+
+			# Copy all geofeeds
+			self.db.execute("""
+				INSERT INTO
+					geofeeds(
+						url
+					)
+				SELECT
+					url
+				FROM
+					network_geofeeds
+				ON CONFLICT (url)
+					DO NOTHING
+				""",
+			)
+
 		# Fetch all Geofeeds that require an update
 		geofeeds = self.db.query("""
 			SELECT