Skip to content

Commit

Permalink
Move new database creation into own script
Browse files Browse the repository at this point in the history
  • Loading branch information
le717 committed Jun 8, 2024
1 parent 42ee3ab commit dbdce77
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ _Released TDB_
- Update Docker image to `python:3.11-slim`
- Update all dependencies to their latest versions
- Switch to [ruff](https://docs.astral.sh/ruff/) for linting and formatting
- Move new database creation into own script
- Various internal tweaks and adjustments

# 1.2.0
Expand Down
23 changes: 23 additions & 0 deletions db/dummy_db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import sys
from pathlib import Path

import sys_vars
from flask import Flask

# We have to add the app path to the path to get the db schema
sys.path.insert(0, Path(__file__).parent.parent.as_posix())

from src.core.database.schema import db


def create_app() -> Flask:
"""Dummy Flask instance used for database management."""
app = Flask(__name__)
app.config["SECRET_KEY"] = "secret tunnel"

db_path = sys_vars.get_path("DB_PATH").resolve()
with app.app_context():
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
app.config["SQLALCHEMY_DATABASE_URI"] = f"sqlite:///{db_path.as_posix()}"
db.init_app(app)
return app
Empty file added scripts/__init__.py
Empty file.
32 changes: 32 additions & 0 deletions scripts/make_database.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import sys
from pathlib import Path

from alembic import command
from alembic.config import Config
from sqlalchemy import inspect

# We have to add the app path to the path to get the db
sys.path.insert(0, Path(__file__).parent.parent.as_posix())


from db.dummy_db import create_app
from src.core.database.schema import db


def create_database() -> None:
"""Create a brand new copy of the database."""
app = create_app()
with app.app_context():
# Create the database tables if needed
if not bool(inspect(db.engine).get_table_names()):
print("Creating new database...") # noqa: T201
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")


if __name__ == "__main__":
create_database()
11 changes: 0 additions & 11 deletions src/app_factory.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from importlib import import_module

import sys_vars
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 @@ -49,13 +47,4 @@ def create_app() -> Flask:
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db.init_app(app)

# 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 dbdce77

Please sign in to comment.