From 92678ea6bf37dc6910c68eace913cd32cc0829a3 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Wed, 15 Dec 2021 17:51:14 +0100 Subject: [PATCH] Refactor internal schema function api --- src/habitmove/schema.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/habitmove/schema.py b/src/habitmove/schema.py index 17272e8..c725648 100644 --- a/src/habitmove/schema.py +++ b/src/habitmove/schema.py @@ -1,7 +1,8 @@ import sqlite3 +import sys -def create_database(db_file: str) -> sqlite3.Connection: +def create_database(db_file: str = ":memory:") -> sqlite3.Connection: """create a database connection to the SQLite database specified by db_file :param db_file: database file @@ -13,10 +14,10 @@ def create_database(db_file: str) -> sqlite3.Connection: return conn except sqlite3.Error as e: print(e) + sys.exit(1) -def create_tables(db): - c = db.cursor() +def create_tables(c: sqlite3.Cursor): c.execute( """ CREATE TABLE IF NOT EXISTS Habits ( id integer PRIMARY KEY AUTOINCREMENT, @@ -49,8 +50,7 @@ def create_tables(db): ) -def create_constraints(db): - c = db.cursor() +def create_constraints(c: sqlite3.Cursor): c.execute( """ CREATE UNIQUE INDEX IF NOT EXISTS idx_repetitions_habit_timestamp on Repetitions( habit, timestamp); @@ -58,15 +58,15 @@ def create_constraints(db): ) -def create_pragma(db): - c = db.cursor() +def create_pragma(c: sqlite3.Cursor): c.execute(""" PRAGMA user_version = 24; """) c.execute(""" PRAGMA schema_version = 30; """) -def migrate(name): - db = create_database(name) - create_tables(db) - create_constraints(db) - create_pragma(db) +def migrate(fname): + db = create_database(fname) + c = db.cursor() + create_tables(c) + create_constraints(c) + create_pragma(c) return db