Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure that case-insensitive column names are unique #22

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion healthkit_to_sqlite/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ 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"]
metadata_name = "metadata_" + child.attrib["key"]
metadata_name = _create_unique_key(metadata_name, record.keys())
record[metadata_name] = child.attrib["value"]
records.append(record)
if len(records) >= 200:
write_records(records, db)
Expand All @@ -46,6 +48,16 @@ def convert_xml_to_sqlite(fp, db, progress_callback=None, zipfile=None):
if activity_summaries:
db["activity_summary"].insert_all(activity_summaries, alter=True)

def _create_unique_key(key, existing_keys):
"""Appends a number to key if lower(key) is already in lower(existing_keys)"""
unqiue_key = key
postfix = 2
keys = [str.lower(key) for key in existing_keys]
while str.lower(unqiue_key) in keys:
unqiue_key = key + "_" + str(postfix)
postfix += 1
return unqiue_key


def workout_to_db(workout, db, zipfile=None):
record = dict(workout.attrib)
Expand Down