From c5deffc367cc64cf129a7b14c7351948ca5ddb7b Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Tue, 21 Mar 2023 16:02:59 +0100 Subject: [PATCH] add upsert --- tools/genpw.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tools/genpw.py b/tools/genpw.py index ca0266a..9fa43d0 100755 --- a/tools/genpw.py +++ b/tools/genpw.py @@ -25,7 +25,7 @@ parser.add_argument('--topic', '-t', parser.add_argument('--acl', '-a', help='ACL value for topic, Bit0=read, Bit1=write, Bit2=subscribe', required=False) -parser.add_argument('--printonly', '-p', +parser.add_argument('--printonly', '-o', help='Just print the password hash, do not write to database', action='store_true') @@ -57,7 +57,7 @@ if not print_only: if (not login): raise Exception("For writing to database a username must be given") topic = args.topic - acl = int(args.acl) + acl = args.acl conn = psycopg2.connect() conn.autocommit = False @@ -68,8 +68,8 @@ if not print_only: cur.execute(""" insert into users_t (username, pw) values(%(username)s, %(pw)s) - on conflict do update - set pw = %(pw)s + on conflict on constraint users_t_uk_username + do update set pw = %(pw)s returning id """, { 'username': login, 'pw': pw }) @@ -79,6 +79,7 @@ if not print_only: id = res[0] print("User added to database") if (topic and acl): + acl = int(acl) with conn.cursor() as cur: cur.execute('insert into acls_t ("user", topic, rw) values(%(user)s, %(topic)s, %(rw)s)', { 'user': id, 'topic': topic, 'rw': acl })