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?
# note: this only works if in python >= 3.7 where `dict` is ordered
c.executemany('REPLACE INTO ' + table + ' (' +
','.join(f'`{k}`' for k in tableMap.keys()) +
') VALUES (' +
','.join(len(tableMap) * ['%s']) +
');',
list(formatRows(data)))
c.executemany(
'INSERT INTO ' + table + ' (' +
','.join(f'`{k}`' for k in tableMap.keys()) +
') VALUES (' + ','.join(len(tableMap) * ['%s']) + ') ' +
'ON DUPLICATE KEY UPDATE ' +
', '.join(f'`{k}`=VALUES(`{k}`)' for k in tableMap.keys()) + ';',
list(formatRows(data)))
# TODO: delete non-valid labels
def insertLabels(members):
for member in members:
for label, label_id in membershipworks._parse_flags()['labels'].items():
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))
else:
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
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())
insertFromTableMap('members', members, tableMapping['members'])