From 01e08e1007fbaef3ce107e52f453ea672e6b5e71 Mon Sep 17 00:00:00 2001 From: Adam Goldsmith Date: Wed, 19 Feb 2020 15:24:55 -0500 Subject: [PATCH] sqlExport: Delete labels that are no longer valid --- sqlExport.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/sqlExport.py b/sqlExport.py index c263b12..e55a6ed 100644 --- a/sqlExport.py +++ b/sqlExport.py @@ -31,14 +31,15 @@ def insertFromTableMap(table, data, tableMap): # TODO: delete non-valid labels def insertLabels(members): - def formatLabels(labels): - for member in members: - for label, label_id in membershipworks._parse_flags()['labels'].items(): - if member[label]: - yield member['Account ID'], label_id + 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);', + (member['Account ID'], label_id)) + else: + c.execute('DELETE FROM member_labels WHERE uid=%s && label_id=%s;', + (member['Account ID'], label_id)) - c.executemany('REPLACE INTO member_labels (uid, label_id) VALUES (%s, %s);', - list(formatLabels(members))) with open('tableMapping.yaml') as f: tableMapping = yaml.load(f, yaml.SafeLoader)