sqlExport: Switch from REPLACE to INSERT...ON DUPLICATE KEY UPDATE

REPLACE DELETES the row even when there was no change, creating a
history entry even when none was needed or useful
This commit is contained in:
Adam Goldsmith 2020-02-19 15:40:46 -05:00
parent 01e08e1007
commit 516813b895

View File

@ -22,19 +22,23 @@ def insertFromTableMap(table, data, tableMap):
# TODO: this could probably be done better as a single statement? # TODO: this could probably be done better as a single statement?
# note: this only works if in python >= 3.7 where `dict` is ordered # note: this only works if in python >= 3.7 where `dict` is ordered
c.executemany('REPLACE INTO ' + table + ' (' + c.executemany(
','.join(f'`{k}`' for k in tableMap.keys()) + 'INSERT INTO ' + table + ' (' +
') VALUES (' + ','.join(f'`{k}`' for k in tableMap.keys()) +
','.join(len(tableMap) * ['%s']) + ') VALUES (' + ','.join(len(tableMap) * ['%s']) + ') ' +
');', 'ON DUPLICATE KEY UPDATE ' +
list(formatRows(data))) ', '.join(f'`{k}`=VALUES(`{k}`)' for k in tableMap.keys()) + ';',
list(formatRows(data)))
# TODO: delete non-valid labels # TODO: delete non-valid labels
def insertLabels(members): def insertLabels(members):
for member in members: for member in members:
for label, label_id in membershipworks._parse_flags()['labels'].items(): for label, label_id in membershipworks._parse_flags()['labels'].items():
if member[label]: if member[label]:
c.execute('REPLACE INTO member_labels (uid, label_id) VALUES (%s, %s);', c.execute("""
INSERT INTO member_labels (uid, label_id) VALUES (%s, %s)
ON DUPLICATE KEY UPDATE
uid=VALUES(uid), label_id=VALUES(label_id);""",
(member['Account ID'], label_id)) (member['Account ID'], label_id))
else: else:
c.execute('DELETE FROM member_labels WHERE uid=%s && label_id=%s;', c.execute('DELETE FROM member_labels WHERE uid=%s && label_id=%s;',
@ -91,7 +95,9 @@ try:
if field.get('typ') == 8 and field['lbl'] in m: # check box if field.get('typ') == 8 and field['lbl'] in m: # check box
m[field['lbl']] = True if m[field['lbl']] == 'Y' else False m[field['lbl']] = True if m[field['lbl']] == 'Y' else False
c.executemany("REPLACE INTO labels (label, label_id) VALUES (%s, %s)", c.executemany("""INSERT INTO labels (label, label_id) VALUES (%s, %s)
ON DUPLICATE KEY UPDATE
label=VALUES(label), label_id=VALUES(label_id);""",
membershipworks._parse_flags()['labels'].items()) membershipworks._parse_flags()['labels'].items())
insertFromTableMap('members', members, tableMapping['members']) insertFromTableMap('members', members, tableMapping['members'])