Skip to content

Commit

Permalink
Only run migrations when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
le717 committed Oct 28, 2021
1 parent 6116f9a commit 3702d0e
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/app_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from importlib import import_module
import logging

from alembic import command
from alembic.config import Config
from flask import Flask
from flask_cors import CORS
from flask_smorest import Api
Expand Down Expand Up @@ -46,13 +48,17 @@ def create_app():
# Create a database connection
db_path = sys_vars.get_path("DB_PATH").resolve()
with app.app_context():
app.config["SQLALCHEMY_DATABASE_URI"] = f"sqlite:///{fspath(db_path)}"
app.config["SQLALCHEMY_DATABASE_URI"] = f"sqlite:///{db_path.as_posix()}"
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db.init_app(app)
db.init_app(app)

# Create the database if needed
if not db_path.exists():
with app.app_context():
# Create the database if needed
if not db_path.exists():
db.create_all()

# Tell Alembic this is a new database and we don't need
# to update it to a newer schema
alembic_cfg = Config("alembic.ini")
command.stamp(alembic_cfg, "head")

return app

0 comments on commit 3702d0e

Please sign in to comment.