From 8dafb470b7333f976546c770383fd70d4c9e536c Mon Sep 17 00:00:00 2001 From: pstruebi Date: Fri, 17 Jun 2022 11:23:33 +0200 Subject: [PATCH] make database sqlite compatible. --- db.py | 23 ++++++++++++++--------- schema.sql | 2 +- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/db.py b/db.py index 755cd07..318cdd8 100644 --- a/db.py +++ b/db.py @@ -5,10 +5,15 @@ from flask import current_app, g from flask.cli import with_appcontext -def get_db(): +def get_db(mod_path=None): if 'db' not in g: + # provide modified path if is not None + if mod_path is None: + path=current_app.config["DATABASE_URI"] + else: + path=mod_path g.db = sqlite3.connect( - current_app.config["DATABASE_URI"], + path, detect_types=sqlite3.PARSE_DECLTYPES ) g.db.row_factory = sqlite3.Row @@ -21,18 +26,18 @@ def close_db(e=None): if db is not None: db.close() -def init_db(): - db = get_db() - - with current_app.open_resource('schema.sql') as f: - db.executescript(f.read().decode('utf8')) @click.command('init-db') @with_appcontext def init_db_command(): """Clear the existing data and create new tables.""" - init_db() - click.echo('Initialized the database.') + path = "./users.db" + db = get_db(mod_path="users.db") + + with current_app.open_resource('schema.sql') as f: + db.executescript(f.read().decode('utf8')) + + click.echo('Initialized the database at %s. Copy it to the according directory.'% path) def init_app(app): app.teardown_appcontext(close_db) diff --git a/schema.sql b/schema.sql index fd29f82..e13eedd 100644 --- a/schema.sql +++ b/schema.sql @@ -1,5 +1,5 @@ DROP TABLE IF EXISTS user; - +PRAGMA journal_mode=wal; CREATE TABLE user ( id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT NOT NULL,