From 3e1b2945bc7c31be59e89c5fed86a5d2a59ebd5a Mon Sep 17 00:00:00 2001 From: Tomas Krehlik Date: Sat, 14 Nov 2020 21:08:47 +0100 Subject: [PATCH] SQLite does not have case sensitive columns This solves a weird issue when there is record with metadata key that is only different in letter cases. See the test for details. --- healthkit_to_sqlite/utils.py | 16 +++++++++++++++- tests/export.xml | 4 ++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/healthkit_to_sqlite/utils.py b/healthkit_to_sqlite/utils.py index fea6bc7..720d38c 100644 --- a/healthkit_to_sqlite/utils.py +++ b/healthkit_to_sqlite/utils.py @@ -22,6 +22,7 @@ def find_all_tags(fp, tags, progress_callback=None): def convert_xml_to_sqlite(fp, db, progress_callback=None, zipfile=None): activity_summaries = [] records = [] + metadata_keys_map = {} for tag, el in find_all_tags( fp, {"Record", "Workout", "ActivitySummary"}, progress_callback ): @@ -35,7 +36,20 @@ def convert_xml_to_sqlite(fp, db, progress_callback=None, zipfile=None): elif tag == "Record": record = dict(el.attrib) for child in el.findall("MetadataEntry"): - record["metadata_" + child.attrib["key"]] = child.attrib["value"] + # sqlite does not have case sensitive columns, have to + # distinguish between meta_start and meta_Start + # we will make meta_Start into meta_start_1 + # that mapping is saved for other records to + # metadata_keys_map + meta_key = "metadata_" + child.attrib["key"] + candidates = list(filter( + lambda k: k[0].lower() == (meta_key).lower(), + metadata_keys_map.items())) + if not candidates: + metadata_keys_map[meta_key] = meta_key + elif meta_key not in metadata_keys_map.keys(): + metadata_keys_map[meta_key] = meta_key + "_" + str(len(candidates)) + record[metadata_keys_map[meta_key]] = child.attrib["value"] records.append(record) if len(records) >= 200: write_records(records, db) diff --git a/tests/export.xml b/tests/export.xml index b310870..428211e 100644 --- a/tests/export.xml +++ b/tests/export.xml @@ -11,6 +11,10 @@ + + + +