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
|
||||
|
||||
|
||||
def add_to_database(cursor, habit_dict):
|
||||
placeholder = ", ".join("?" * len(habit_dict))
|
||||
columns = ", ".join(habit_dict.keys())
|
||||
def add_to_database(cursor, habit):
|
||||
"""Takes a habit in the form of a dictionary and inserts it into the Habits table.
|
||||
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(
|
||||
table="Habits", columns=columns, values=placeholder
|
||||
)
|
||||
values = list(habit_dict.values())
|
||||
values = list(habit.values())
|
||||
cursor.execute(sql, values)
|
||||
|
|
|
@ -8,14 +8,13 @@ def migrate(db, habitlist, events):
|
|||
for habit in habitlist:
|
||||
tag = f"#{habit['description']}"
|
||||
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']}")
|
||||
try:
|
||||
add_repetition(c, id, event["end"])
|
||||
except sqlite3.IntegrityError:
|
||||
print("fail to register: not unique")
|
||||
pass
|
||||
add_to_database(c, fetch_habit_id(c, habit), event["end"])
|
||||
|
||||
|
||||
def fetch_habit_id(c, habit):
|
||||
c.execute("select id from Habits where uuid = ?", [(habit["uuid"])])
|
||||
return c.fetchone()[0]
|
||||
|
||||
|
||||
# does not do:
|
||||
|
@ -24,12 +23,17 @@ def migrate(db, habitlist, events):
|
|||
# non-range habits but still #habit(3) number included, adding multiple?
|
||||
|
||||
|
||||
def add_repetition(cursor, habit_id, timestamp, value=2):
|
||||
cursor.execute(
|
||||
"""
|
||||
INSERT INTO
|
||||
Repetitions(id, habit, timestamp, value)
|
||||
VALUES (NULL, ?, ?, ?)
|
||||
""",
|
||||
(habit_id, timestamp, value),
|
||||
)
|
||||
def add_to_database(cursor, habit_id, timestamp, value=2):
|
||||
try:
|
||||
cursor.execute(
|
||||
"""
|
||||
INSERT INTO
|
||||
Repetitions(id, habit, timestamp, value)
|
||||
VALUES (NULL, ?, ?, ?)
|
||||
""",
|
||||
(habit_id, timestamp, value),
|
||||
)
|
||||
except sqlite3.IntegrityError:
|
||||
# TODO better error handling
|
||||
print("fail to register: not unique")
|
||||
pass
|
||||
|
|
Loading…
Reference in a new issue