add upsert
This commit is contained in:
parent
61bc3f8dfc
commit
c5deffc367
@ -25,7 +25,7 @@ parser.add_argument('--topic', '-t',
|
|||||||
parser.add_argument('--acl', '-a',
|
parser.add_argument('--acl', '-a',
|
||||||
help='ACL value for topic, Bit0=read, Bit1=write, Bit2=subscribe',
|
help='ACL value for topic, Bit0=read, Bit1=write, Bit2=subscribe',
|
||||||
required=False)
|
required=False)
|
||||||
parser.add_argument('--printonly', '-p',
|
parser.add_argument('--printonly', '-o',
|
||||||
help='Just print the password hash, do not write to database',
|
help='Just print the password hash, do not write to database',
|
||||||
action='store_true')
|
action='store_true')
|
||||||
|
|
||||||
@ -57,7 +57,7 @@ if not print_only:
|
|||||||
if (not login):
|
if (not login):
|
||||||
raise Exception("For writing to database a username must be given")
|
raise Exception("For writing to database a username must be given")
|
||||||
topic = args.topic
|
topic = args.topic
|
||||||
acl = int(args.acl)
|
acl = args.acl
|
||||||
|
|
||||||
conn = psycopg2.connect()
|
conn = psycopg2.connect()
|
||||||
conn.autocommit = False
|
conn.autocommit = False
|
||||||
@ -68,8 +68,8 @@ if not print_only:
|
|||||||
cur.execute("""
|
cur.execute("""
|
||||||
insert into users_t (username, pw)
|
insert into users_t (username, pw)
|
||||||
values(%(username)s, %(pw)s)
|
values(%(username)s, %(pw)s)
|
||||||
on conflict do update
|
on conflict on constraint users_t_uk_username
|
||||||
set pw = %(pw)s
|
do update set pw = %(pw)s
|
||||||
returning id
|
returning id
|
||||||
""",
|
""",
|
||||||
{ 'username': login, 'pw': pw })
|
{ 'username': login, 'pw': pw })
|
||||||
@ -79,6 +79,7 @@ if not print_only:
|
|||||||
id = res[0]
|
id = res[0]
|
||||||
print("User added to database")
|
print("User added to database")
|
||||||
if (topic and acl):
|
if (topic and acl):
|
||||||
|
acl = int(acl)
|
||||||
with conn.cursor() as cur:
|
with conn.cursor() as cur:
|
||||||
cur.execute('insert into acls_t ("user", topic, rw) values(%(user)s, %(topic)s, %(rw)s)',
|
cur.execute('insert into acls_t ("user", topic, rw) values(%(user)s, %(topic)s, %(rw)s)',
|
||||||
{ 'user': id, 'topic': topic, 'rw': acl })
|
{ 'user': id, 'topic': topic, 'rw': acl })
|
||||||
|
Loading…
x
Reference in New Issue
Block a user