2021-08-28 07:54:41 +00:00
|
|
|
def migrate(db, trackers):
|
|
|
|
c = db.cursor()
|
2021-08-28 08:35:29 +00:00
|
|
|
habits = trackers_to_habits(trackers)
|
|
|
|
for habit in habits:
|
|
|
|
existing_habit = check_habit_duplicate(c, habit)
|
|
|
|
if not existing_habit:
|
|
|
|
add_to_database(c, habit)
|
|
|
|
else:
|
|
|
|
print(f"Found duplicate Habit: {existing_habit} - skipping.")
|
2021-08-28 07:54:41 +00:00
|
|
|
|
|
|
|
# DEBUGGING
|
|
|
|
c.execute("""select * from Habits""")
|
|
|
|
print(c.fetchall())
|
|
|
|
|
|
|
|
|
2021-08-28 08:35:29 +00:00
|
|
|
def trackers_to_habits(trackers):
|
|
|
|
habits = []
|
|
|
|
# for each list entry tracker dict transform to habit dict
|
|
|
|
for tracker_name in trackers.keys():
|
|
|
|
t = trackers[tracker_name]
|
|
|
|
habits.append(
|
|
|
|
{
|
|
|
|
"archived": 0 if t["hidden"] == False else 1,
|
|
|
|
"color": 11,
|
|
|
|
"description": t["label"],
|
|
|
|
"freq_den": 1,
|
|
|
|
"freq_num": 1,
|
|
|
|
"highlight": 0,
|
|
|
|
"name": f"{t['emoji']} {t['label']}",
|
|
|
|
"question": "",
|
|
|
|
"unit": "" if t["uom"] == "num" else t["uom"],
|
|
|
|
"uuid": t["id"],
|
|
|
|
}
|
|
|
|
# FIXME conditional for ranges (if type = range > habits[-1]['type'] = intforrangetype; habits[-1]['max'] =)
|
|
|
|
)
|
|
|
|
return habits
|
|
|
|
|
|
|
|
|
|
|
|
def check_habit_duplicate(cursor, habit):
|
|
|
|
cursor.execute("select name from Habits where uuid = ?", [habit["uuid"]])
|
|
|
|
name = cursor.fetchone()
|
|
|
|
if name:
|
|
|
|
return name[0]
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2021-08-28 07:54:41 +00:00
|
|
|
def add_to_database(cursor, habit_dict):
|
|
|
|
placeholder = ", ".join("?" * len(habit_dict))
|
|
|
|
columns = ", ".join(habit_dict.keys())
|
|
|
|
sql = "insert into `{table}` ({columns}) values ({values});".format(
|
|
|
|
table="Habits", columns=columns, values=placeholder
|
|
|
|
)
|
2021-08-28 08:35:29 +00:00
|
|
|
values = list(habit_dict.values())
|
2021-08-28 07:54:41 +00:00
|
|
|
cursor.execute(sql, values)
|
|
|
|
|
|
|
|
|
|
|
|
# grab next tracker info
|
|
|
|
|
|
|
|
# look for unique id of next habit
|
|
|
|
# if exists skip and warn user
|
|
|
|
|
|
|
|
# insert tracker into habit table
|
|
|
|
|
|
|
|
# FOR REPETITION MIGRATION
|
|
|
|
# c.execute(
|
|
|
|
# """
|
|
|
|
# INSERT INTO
|
|
|
|
# Repetitions(id, habit, timestamp, value)
|
|
|
|
# VALUES (NULL, 5, 1629676800000, 2)
|
|
|
|
# """
|
|
|
|
# )
|
|
|
|
# c.execute(
|
|
|
|
# """
|
|
|
|
# INSERT INTO
|
|
|
|
# Repetitions(id, habit, timestamp, value)
|
|
|
|
# VALUES (NULL, 6, 1629676800000, 2)
|
|
|
|
# """
|
|
|
|
# )
|
|
|
|
# """
|
|
|
|
# )
|
|
|
|
# """
|
|
|
|
# )
|
|
|
|
# """
|
|
|
|
# )
|
|
|
|
# """
|
|
|
|
# )
|
|
|
|
# """
|
|
|
|
# )
|
|
|
|
# """
|
|
|
|
# )
|
|
|
|
# """
|
|
|
|
# )
|
|
|
|
# """
|
|
|
|
# )
|
|
|
|
# """
|
|
|
|
# )
|
|
|
|
# """
|
|
|
|
# )
|