Refactor repetitions migration
This commit is contained in:
parent
49fdf47657
commit
269a255885
2 changed files with 32 additions and 20 deletions
|
@ -43,11 +43,19 @@ def check_habit_duplicate(cursor, habit):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def add_to_database(cursor, habit_dict):
|
def add_to_database(cursor, habit):
|
||||||
placeholder = ", ".join("?" * len(habit_dict))
|
"""Takes a habit in the form of a dictionary and inserts it into the Habits table.
|
||||||
columns = ", ".join(habit_dict.keys())
|
Parameters:
|
||||||
|
cursor (db.cursor): SQL executing cursor
|
||||||
|
habit (dict): A Loop habit to be added to the database. Must contain a minimum of
|
||||||
|
'archived','color','description','freq_den','freq_num','highlight','name',
|
||||||
|
'question','position','unit','uuid' as keys.
|
||||||
|
"""
|
||||||
|
|
||||||
|
placeholder = ", ".join("?" * len(habit))
|
||||||
|
columns = ", ".join(habit.keys())
|
||||||
sql = "insert into `{table}` ({columns}) values ({values});".format(
|
sql = "insert into `{table}` ({columns}) values ({values});".format(
|
||||||
table="Habits", columns=columns, values=placeholder
|
table="Habits", columns=columns, values=placeholder
|
||||||
)
|
)
|
||||||
values = list(habit_dict.values())
|
values = list(habit.values())
|
||||||
cursor.execute(sql, values)
|
cursor.execute(sql, values)
|
||||||
|
|
|
@ -8,14 +8,13 @@ def migrate(db, habitlist, events):
|
||||||
for habit in habitlist:
|
for habit in habitlist:
|
||||||
tag = f"#{habit['description']}"
|
tag = f"#{habit['description']}"
|
||||||
if tag in text:
|
if tag in text:
|
||||||
c.execute("select id from Habits where uuid = ?", [(habit["uuid"])])
|
|
||||||
id = c.fetchone()[0]
|
|
||||||
print(f"register: {habit['name']} in {text} at {event['end']}")
|
print(f"register: {habit['name']} in {text} at {event['end']}")
|
||||||
try:
|
add_to_database(c, fetch_habit_id(c, habit), event["end"])
|
||||||
add_repetition(c, id, event["end"])
|
|
||||||
except sqlite3.IntegrityError:
|
|
||||||
print("fail to register: not unique")
|
def fetch_habit_id(c, habit):
|
||||||
pass
|
c.execute("select id from Habits where uuid = ?", [(habit["uuid"])])
|
||||||
|
return c.fetchone()[0]
|
||||||
|
|
||||||
|
|
||||||
# does not do:
|
# does not do:
|
||||||
|
@ -24,7 +23,8 @@ def migrate(db, habitlist, events):
|
||||||
# non-range habits but still #habit(3) number included, adding multiple?
|
# non-range habits but still #habit(3) number included, adding multiple?
|
||||||
|
|
||||||
|
|
||||||
def add_repetition(cursor, habit_id, timestamp, value=2):
|
def add_to_database(cursor, habit_id, timestamp, value=2):
|
||||||
|
try:
|
||||||
cursor.execute(
|
cursor.execute(
|
||||||
"""
|
"""
|
||||||
INSERT INTO
|
INSERT INTO
|
||||||
|
@ -33,3 +33,7 @@ def add_repetition(cursor, habit_id, timestamp, value=2):
|
||||||
""",
|
""",
|
||||||
(habit_id, timestamp, value),
|
(habit_id, timestamp, value),
|
||||||
)
|
)
|
||||||
|
except sqlite3.IntegrityError:
|
||||||
|
# TODO better error handling
|
||||||
|
print("fail to register: not unique")
|
||||||
|
pass
|
||||||
|
|
Loading…
Reference in a new issue