aboutsummaryrefslogtreecommitdiff
path: root/todo/db/init.py
diff options
context:
space:
mode:
Diffstat (limited to 'todo/db/init.py')
-rw-r--r--todo/db/init.py51
1 files changed, 51 insertions, 0 deletions
diff --git a/todo/db/init.py b/todo/db/init.py
new file mode 100644
index 0000000..5d847a3
--- /dev/null
+++ b/todo/db/init.py
@@ -0,0 +1,51 @@
+import sqlite3
+import os.path
+import time
+
+def init(path):
+
+ is_db_new = not os.path.isfile(path)
+
+ database = sqlite3.connect(path)
+
+ cursor = database.cursor()
+
+ if is_db_new:
+
+ cursor.execute(
+ " CREATE TABLE IF NOT EXISTS tasks("
+ " id INTEGER PRIMARY KEY,"
+ " created_at INTEGER NOT NULL,"
+ " updated_at INTEGER NOT NULL,"
+ " name TEXT NOT NULL,"
+ " duration INTEGER,"
+ " difficulty INT,"
+ " priority INT,"
+ " description TEXT,"
+ " status TEXT"
+ " )")
+
+ cursor.execute(
+ " CREATE TABLE IF NOT EXISTS tags("
+ " id INTEGER PRIMARY KEY,"
+ " created_at INTEGER NOT NULL,"
+ " updated_at INTEGER NOT NULL,"
+ " name TEXT NOT NULL,"
+ " color TEXT NOT NULL"
+ " )")
+
+ cursor.execute(
+ " CREATE TABLE IF NOT EXISTS task_tags("
+ " task_id INTEGER NOT NULL,"
+ " tag_id INTEGER NOT NULL,"
+ " created_at INTEGER NOT NULL,"
+ " FOREIGN KEY (task_id) REFERENCES tasks(id),"
+ " FOREIGN KEY (tag_id) REFERENCES tags(id),"
+ " PRIMARY KEY (task_id, tag_id)"
+ " )")
+
+ cursor.execute("PRAGMA foreign_keys = ON")
+
+ database.commit()
+
+ return database