diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 63c9ee1c..5edda276 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -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: | diff --git a/bin/recreate_db b/bin/recreate_db index e2cb344d..b638f733 100755 --- a/bin/recreate_db +++ b/bin/recreate_db @@ -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" diff --git a/noxfile.py b/noxfile.py index 38710e05..1dfb35e8 100644 --- a/noxfile.py +++ b/noxfile.py @@ -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) diff --git a/src/spiff_workflow_webapp/__init__.py b/src/spiff_workflow_webapp/__init__.py index cace5243..46b3e2b2 100644 --- a/src/spiff_workflow_webapp/__init__.py +++ b/src/spiff_workflow_webapp/__init__.py @@ -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) diff --git a/src/spiff_workflow_webapp/config/__init__.py b/src/spiff_workflow_webapp/config/__init__.py index 8af99b5c..f51e613b 100644 --- a/src/spiff_workflow_webapp/config/__init__.py +++ b/src/spiff_workflow_webapp/config/__init__.py @@ -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: diff --git a/src/spiff_workflow_webapp/config/default.py b/src/spiff_workflow_webapp/config/default.py index 0430dead..b938ca7c 100644 --- a/src/spiff_workflow_webapp/config/default.py +++ b/src/spiff_workflow_webapp/config/default.py @@ -1,4 +1 @@ """Default.""" -SQLALCHEMY_DATABASE_URI = ( - "mysql+mysqlconnector://root:@localhost/spiff_workflow_webapp_development" -) diff --git a/src/spiff_workflow_webapp/config/development.py b/src/spiff_workflow_webapp/config/development.py index 5da131a0..5ddd1a28 100644 --- a/src/spiff_workflow_webapp/config/development.py +++ b/src/spiff_workflow_webapp/config/development.py @@ -1,4 +1 @@ """Development.""" -SQLALCHEMY_DATABASE_URI = ( - "mysql+mysqlconnector://root:@localhost/spiff_workflow_webapp_development" -) diff --git a/src/spiff_workflow_webapp/config/testing.py b/src/spiff_workflow_webapp/config/testing.py index 5e3dcb7c..4a877331 100644 --- a/src/spiff_workflow_webapp/config/testing.py +++ b/src/spiff_workflow_webapp/config/testing.py @@ -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