add upsert

This commit is contained in:
Wolfgang Hottgenroth 2023-03-21 16:02:59 +01:00
parent 61bc3f8dfc
commit c5deffc367
Signed by: wn
GPG Key ID: 836E9E1192A6B132

View File

@ -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 })