Skip to content

Commit

Permalink
Merge pull request #4 from VNG-Realisatie/feature/update-first-record
Browse files Browse the repository at this point in the history
feat: update record instead of creating when one is found
  • Loading branch information
joerivrij authored Feb 20, 2023
2 parents 929ffef + 7c5701b commit f0e1c12
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit f0e1c12

Please sign in to comment.