You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
972 B
Python

3 years ago
import sqlite3
import click
from flask import current_app, g
from flask.cli import with_appcontext
5 months ago
def dict_factory(cursor, row):
d = {}
for idx, col in enumerate(cursor.description):
d[col[0]] = row[idx]
return d
3 years ago
def get_db():
5 months ago
if "db" not in g:
3 years ago
g.db = sqlite3.connect(
5 months ago
current_app.config["DATABASE"], detect_types=sqlite3.PARSE_DECLTYPES
3 years ago
)
g.db.row_factory = sqlite3.Row
return g.db
def close_db(e=None):
5 months ago
db = g.pop("db", None)
3 years ago
if db is not None:
db.close()
def init_db():
db = get_db()
5 months ago
with current_app.open_resource("schema.sql") as f:
db.executescript(f.read().decode("utf8"))
3 years ago
5 months ago
@click.command("init-db")
3 years ago
@with_appcontext
def init_db_command():
"""Clear the existing data and create new tables."""
init_db()
5 months ago
click.echo("Initialized the database.")
3 years ago
def init_app(app):
app.teardown_appcontext(close_db)
app.cli.add_command(init_db_command)