From 7c5701b5f964747bfa26ff7ea5f345a1bd3d9c35 Mon Sep 17 00:00:00 2001 From: joerivrij Date: Mon, 20 Feb 2023 12:25:55 +0100 Subject: [PATCH] feat: update record instead of creating when one is found --- main.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/main.py b/main.py index a08175a..0aaf417 100644 --- a/main.py +++ b/main.py @@ -99,10 +99,24 @@ def create_auth_config(auth_service, component, db_connection): print( f"adding authorizations_authorizationsconfig for component: {component} with api_root {api_root}" ) - cursor.execute( - "INSERT INTO authorizations_authorizationsconfig (api_root, component) VALUES(%s, %s)", - (api_root, component), - ) + query = "SELECT * FROM authorizations_authorizationsconfig" + + cursor.execute(query) + records = cursor.fetchall() + if len(records) == 1: + print("a record already exists so updating instead of creating") + print(records) + cursor.execute( + """ UPDATE authorizations_authorizationsconfig + SET api_root = %s + WHERE id = %s""", + (api_root, 1), + ) + else: + cursor.execute( + "INSERT INTO authorizations_authorizationsconfig (id, api_root, component) VALUES(1, %s, %s)", + (api_root, component), + ) db_connection.commit() cursor.close() except (Exception, psycopg2.DatabaseError) as error: