Skip to content

Commit 01c0673

Browse files
author
=
committed
Centralized db initialization in flask cli
1 parent 983b6fd commit 01c0673

File tree

3 files changed

+20
-12
lines changed

3 files changed

+20
-12
lines changed

accessmapapi/__init__.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from . import auth
88
from . import jwt
9-
from .models import db
9+
from .models import init_app as db_init_app
1010
from . import blueprints
1111

1212

@@ -30,12 +30,7 @@ def create_app():
3030
)
3131

3232
# Attach database
33-
db.init_app(app)
34-
35-
# Migrate database
36-
# FIXME: move db creation stuff into a separate, trackable migration framework
37-
with app.app_context():
38-
db.create_all()
33+
db_init_app(app)
3934

4035
# Add oauth interface
4136
auth.init_app(app)

accessmapapi/db.py

Lines changed: 0 additions & 5 deletions
This file was deleted.

accessmapapi/models.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import click
12
from flask import g, current_app
3+
from flask.cli import with_appcontext
24
from flask_sqlalchemy import SQLAlchemy
35
from werkzeug.local import LocalProxy
46
from werkzeug.contrib.cache import FileSystemCache
@@ -7,6 +9,22 @@
79
db = SQLAlchemy()
810

911

12+
def init_db():
13+
db.create_all()
14+
15+
16+
@click.command("init-db")
17+
@with_appcontext
18+
def init_db_command():
19+
init_db()
20+
click.echo("Initialized the database.")
21+
22+
23+
def init_app(app):
24+
app.cli.add_command(init_db_command)
25+
db.init_app(app)
26+
27+
1028
class User(db.Model):
1129
user_id = db.Column(db.Integer, primary_key=True)
1230

0 commit comments

Comments
 (0)