added ics support. and some thread safety
This commit is contained in:
24
DB.py
24
DB.py
@@ -3,14 +3,17 @@ import sqlite3
|
||||
|
||||
class Database:
|
||||
def __init__(self):
|
||||
self._con = sqlite3.connect("shifts.db")
|
||||
self.db_path = "shifts.db"
|
||||
self._setup_tables()
|
||||
pass
|
||||
|
||||
|
||||
def _get_connection(self):
|
||||
return sqlite3.connect(self.db_path)
|
||||
|
||||
def _setup_tables(self):
|
||||
# create the shifts table.
|
||||
cur = self._con.cursor()
|
||||
con = self._get_connection()
|
||||
cur = con.cursor()
|
||||
cur.execute('''
|
||||
CREATE TABLE IF NOT EXISTS shifts (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
@@ -24,12 +27,14 @@ class Database:
|
||||
UNIQUE(shift_start, department) -- No duplicates
|
||||
);
|
||||
''')
|
||||
self._con.commit()
|
||||
con.commit()
|
||||
con.close()
|
||||
|
||||
|
||||
def insert_shifts(self, shifts):
|
||||
# start db connection
|
||||
cur = self._con.cursor()
|
||||
con = self._get_connection()
|
||||
cur = con.cursor()
|
||||
inserted = 0
|
||||
|
||||
for shift in shifts:
|
||||
@@ -51,11 +56,14 @@ class Database:
|
||||
if cur.rowcount > 0:
|
||||
inserted += 1
|
||||
|
||||
self._con.commit()
|
||||
con.commit()
|
||||
con.close()
|
||||
print(f"✅ Inserted {inserted}/{len(shifts)} new shifts")
|
||||
|
||||
def delete_future_shifts(self):
|
||||
cur = self._con.cursor()
|
||||
con = self._get_connection()
|
||||
cur = con.cursor()
|
||||
cur.execute("DELETE FROM shifts WHERE shift_start > current_timestamp")
|
||||
self._con.commit()
|
||||
con.commit()
|
||||
con.close()
|
||||
print(f"✅ Deleted all future shifts")
|
||||
|
||||
Reference in New Issue
Block a user