set instance path based on environment variable and determine db based on that from config init instead of config files w/ burnettk

This commit is contained in:
jasquat 2022-05-20 16:37:23 -04:00
parent a1fa050aee
commit 7079a2ca5b
8 changed files with 44 additions and 50 deletions

View File

@ -78,18 +78,6 @@ jobs:
FORCE_COLOR: "1"
PRE_COMMIT_COLOR: "always"
# services:
# mysql:
# if: matrix.database == 'mysql'
# image: mariadb:10.3
# env:
# MYSQL_DATABASE: spiff_workflow_webapp_testing
# MYSQL_ALLOW_EMPTY_PASSWORD: yes
# MYSQL_ROOT_PASSWORD:
# ports:
# - 3306:3306
# options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
steps:
- name: Check out the repository
uses: actions/checkout@v3.0.2
@ -147,24 +135,16 @@ jobs:
key: ${{ steps.pre-commit-cache.outputs.result }}-${{ hashFiles('.pre-commit-config.yaml') }}
restore-keys: |
${{ steps.pre-commit-cache.outputs.result }}-
- name: Setup Mysql
uses: mirromutth/mysql-action@v1.1
with:
host port: 3306
container port: 3306
mysql version: '8.0'
mysql database: 'spiff_workflow_webapp_testing'
mysql root password: 'password'
mysql version: "8.0"
mysql database: "spiff_workflow_webapp_testing"
mysql root password: "password"
if: matrix.database == 'mysql'
# image: mariadb:10.3
# env:
# MYSQL_DATABASE: spiff_workflow_webapp_testing
# MYSQL_ALLOW_EMPTY_PASSWORD: yes
# MYSQL_ROOT_PASSWORD:
# ports:
# - 3306:3306
# options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
- name: Run Nox
run: |

View File

@ -9,9 +9,10 @@ set -o errtrace -o errexit -o nounset -o pipefail
tasks=""
if [[ "${1:-}" == "clean" ]]; then
tasks="$tasks init"
rm -rf migrations/
if [[ "${2:-}" == "rmall" ]]; then
tasks="$tasks init migrate"
rm -rf migrations/
fi
rm -f ./src/spiff_workflow_webapp/db*.sqlite3
mysql -uroot -e "DROP DATABASE IF EXISTS spiff_workflow_webapp_development"
@ -19,7 +20,7 @@ if [[ "${1:-}" == "clean" ]]; then
mysql -uroot -e "DROP DATABASE IF EXISTS spiff_workflow_webapp_testing"
mysql -uroot -e "CREATE DATABASE spiff_workflow_webapp_testing"
fi
tasks="$tasks migrate upgrade"
tasks="$tasks upgrade"
for task in $tasks ; do
FLASK_ENV=development FLASK_APP=src/spiff_workflow_webapp poetry run flask db "$task"

View File

@ -34,6 +34,14 @@ nox.options.sessions = (
)
def setup_database(session: Session):
"""Run database migrations against the database."""
session.env["FLASK_INSTANCE_PATH"] = os.path.join(os.getcwd(), "instance")
session.env["FLASK_APP"] = "src/spiff_workflow_webapp"
session.env["FLASK_ENV"] = "testing"
session.run("flask", "db", "upgrade")
def activate_virtualenv_in_precommit_hooks(session: Session) -> None:
"""Activate virtualenv in hooks installed by pre-commit.
@ -132,9 +140,7 @@ def tests(session: Session) -> None:
session.install(".")
session.install("coverage[toml]", "pytest", "pygments")
try:
session.env["FLASK_APP"] = "src/spiff_workflow_webapp"
session.env["FLASK_ENV"] = "testing"
session.run("flask", "db", "upgrade")
setup_database(session)
session.run("coverage", "run", "--parallel", "-m", "pytest", *session.posargs)
finally:
if session.interactive:
@ -159,9 +165,7 @@ def typeguard(session: Session) -> None:
"""Runtime type checking using Typeguard."""
session.install(".")
session.install("pytest", "typeguard", "pygments")
session.env["FLASK_APP"] = "src/spiff_workflow_webapp"
session.env["FLASK_ENV"] = "testing"
session.run("flask", "db", "upgrade")
setup_database(session)
session.env["RUN_TYPEGUARD"] = "true"
session.run("pytest", *session.posargs)

View File

@ -1,4 +1,6 @@
"""__init__."""
import os
import flask.app
from flask import Flask
from flask_bpmn.api.api_error import api_error_blueprint
@ -12,7 +14,12 @@ from spiff_workflow_webapp.routes.user_blueprint import user_blueprint
def create_app() -> flask.app.Flask:
"""Create_app."""
app = Flask(__name__)
# We need to create the sqlite database in a known location.
# If we rely on the app.instance_path without setting an environment
# variable, it will be one thing when we run flask db upgrade in the
# noxfile and another thing when the tests actually run.
# instance_path is described more at https://flask.palletsprojects.com/en/2.1.x/config/
app = Flask(__name__, instance_path=os.environ.get("FLASK_INSTANCE_PATH"))
setup_config(app)
db.init_app(app)

View File

@ -1,13 +1,30 @@
"""__init__.py."""
import os
from flask.app import Flask
from werkzeug.utils import ImportStringError
def setup_config(app: Flask) -> None:
"""Setup_config."""
# ensure the instance folder exists
try:
os.makedirs(app.instance_path)
except OSError:
pass
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
app.config.from_object("spiff_workflow_webapp.config.default")
if os.environ.get("TEST_DATABASE_TYPE") == "sqlite":
app.config[
"SQLALCHEMY_DATABASE_URI"
] = f"sqlite:///{app.instance_path}/db_{app.env}.sqlite3"
else:
app.config[
"SQLALCHEMY_DATABASE_URI"
] = f"mysql+mysqlconnector://root:password@localhost/spiff_workflow_webapp_{app.env}"
try:
app.config.from_object("spiff_workflow_webapp.config." + app.env)
except ImportStringError as exception:

View File

@ -1,4 +1 @@
"""Default."""
SQLALCHEMY_DATABASE_URI = (
"mysql+mysqlconnector://root:@localhost/spiff_workflow_webapp_development"
)

View File

@ -1,4 +1 @@
"""Development."""
SQLALCHEMY_DATABASE_URI = (
"mysql+mysqlconnector://root:@localhost/spiff_workflow_webapp_development"
)

View File

@ -1,12 +1,3 @@
"""Testing.py."""
import os
# just for the matrix builds in CI
if os.environ.get("TEST_DATABASE_TYPE") == "sqlite":
SQLALCHEMY_DATABASE_URI = "sqlite:///db_testing.sqlite3"
else:
SQLALCHEMY_DATABASE_URI = (
"mysql+mysqlconnector://root:password@localhost/spiff_workflow_webapp_testing"
)
TESTING = True