diff --git a/spiffworkflow-backend/bin/recreate_db b/spiffworkflow-backend/bin/recreate_db index b53952640..499762f54 100755 --- a/spiffworkflow-backend/bin/recreate_db +++ b/spiffworkflow-backend/bin/recreate_db @@ -26,10 +26,17 @@ if [[ -z "${SPIFFWORKFLOW_BACKEND_BPMN_SPEC_ABSOLUTE_DIR:-}" ]]; then export SPIFFWORKFLOW_BACKEND_BPMN_SPEC_ABSOLUTE_DIR fi +if [[ -n "${SPIFFWORKFLOW_BACKEND_ENV:-}" ]] && ! grep -Eq '^(local_development|unit_testing)$' <<<"$SPIFFWORKFLOW_BACKEND_ENV"; then + echo >&2 "ERROR: SPIFFWORKFLOW_BACKEND_ENV is set to '${SPIFFWORKFLOW_BACKEND_ENV}'. Only local_development and unit_testing are allowed." + exit 1 +fi + database_host="localhost" database_port="" database_username="root" database_password="" +databases_to_run_on="spiffworkflow_backend_local_development spiffworkflow_backend_unit_testing" +database_name_from_uri="" if [[ -n "${SPIFFWORKFLOW_BACKEND_DATABASE_URI:-}" ]]; then database_host_and_port=$(grep -oP "^[^:]+://.*@\K(.+?)[/]" <<<"$SPIFFWORKFLOW_BACKEND_DATABASE_URI" | sed -E 's/[\/]$//') database_host=$(awk -F ':' '{print $1}' <<<"$database_host_and_port") @@ -37,6 +44,10 @@ if [[ -n "${SPIFFWORKFLOW_BACKEND_DATABASE_URI:-}" ]]; then database_username_and_password=$(grep -oP "^[^:]+://\K([^@]+)[@]" <<<"$SPIFFWORKFLOW_BACKEND_DATABASE_URI" | sed -E 's/[@]$//') database_username=$(awk -F ':' '{print $1}' <<<"$database_username_and_password") database_password=$(awk -F ':' '{print $2}' <<<"$database_username_and_password") + database_name_from_uri=$(grep -oP "/\K(\w+)$" <<<"$SPIFFWORKFLOW_BACKEND_DATABASE_URI") + if ! grep "\<$database_name_from_uri\>" <<<"$databases_to_run_on"; then + databases_to_run_on="$database_name_from_uri" + fi fi database_host_args="-h $database_host -u $database_username" @@ -50,6 +61,13 @@ fi # uncomment this line to fix branching conflicts # poetry run flask db merge heads -m "merging heads" +function run_command_on_mysql_databases { + local command="$1" + for database_name in $databases_to_run_on; do + mysql $database_host_args -e "$command $database_name" + done +} + tasks="" if [[ "${1:-}" == "clean" ]]; then subcommand="${2:-}" @@ -64,8 +82,7 @@ if [[ "${1:-}" == "clean" ]]; then if [[ "${SPIFFWORKFLOW_BACKEND_DATABASE_TYPE:-mysql}" == "sqlite" ]]; then rm -f ./src/instance/*.sqlite3 elif [[ "${SPIFFWORKFLOW_BACKEND_DATABASE_TYPE:-mysql}" == "mysql" ]]; then - mysql $database_host_args -e "DROP DATABASE IF EXISTS spiffworkflow_backend_local_development" - mysql $database_host_args -e "DROP DATABASE IF EXISTS spiffworkflow_backend_unit_testing" + run_command_on_mysql_databases "DROP DATABASE IF EXISTS" elif [[ "${SPIFFWORKFLOW_BACKEND_DATABASE_TYPE:-}" == "postgres" ]]; then # TODO: check to see if the db already exists and we can connect to it. also actually clean it up. # start postgres in background with one db @@ -99,20 +116,16 @@ else fi if [[ "${SPIFFWORKFLOW_BACKEND_DATABASE_TYPE:-mysql}" == "mysql" ]]; then - mysql $database_host_args -e "CREATE DATABASE IF NOT EXISTS spiffworkflow_backend_local_development" - mysql $database_host_args -e "CREATE DATABASE IF NOT EXISTS spiffworkflow_backend_unit_testing" + run_command_on_mysql_databases "CREATE DATABASE IF NOT EXISTS" fi for task in $tasks; do SPIFFWORKFLOW_BACKEND_ENV=local_development FLASK_APP=src/spiffworkflow_backend poetry run flask db "$task" done -SPIFFWORKFLOW_BACKEND_ENV=unit_testing FLASK_APP=src/spiffworkflow_backend poetry run flask db upgrade -if [[ -n "${SPIFFWORKFLOW_BACKEND_ENV:-}" ]] && ! grep -Eq '^(local_development|unit_testing)$' <<<"$SPIFFWORKFLOW_BACKEND_ENV"; then - if [[ "${SPIFFWORKFLOW_BACKEND_DATABASE_TYPE:-mysql}" == "mysql" ]]; then - mysql $database_host_args -e "CREATE DATABASE IF NOT EXISTS spiffworkflow_backend_$SPIFFWORKFLOW_BACKEND_ENV" - fi - FLASK_APP=src/spiffworkflow_backend poetry run flask db upgrade +if [[ -z "$database_name_from_uri" ]] || [[ "$database_name_from_uri" == "spiffworkflow_backend_local_development" ]]; then + hacked_up_uri=$(sed -E 's/spiffworkflow_backend_local_development$/spiffworkflow_backend_unit_testing/' <<<"${SPIFFWORKFLOW_BACKEND_DATABASE_URI:-}") + SPIFFWORKFLOW_BACKEND_DATABASE_URI="$hacked_up_uri" SPIFFWORKFLOW_BACKEND_ENV=unit_testing FLASK_APP=src/spiffworkflow_backend poetry run flask db upgrade fi # for ./bin/tests-par (parallel tests with xdist) diff --git a/spiffworkflow-backend/migrations/versions/3191627ae224_.py b/spiffworkflow-backend/migrations/versions/3191627ae224_.py index 9bcd5894d..ec4e8fd14 100644 --- a/spiffworkflow-backend/migrations/versions/3191627ae224_.py +++ b/spiffworkflow-backend/migrations/versions/3191627ae224_.py @@ -27,6 +27,7 @@ def is_postgres() -> bool: def upgrade(): # ### commands auto generated by Alembic - please adjust! ### # we did in fact heavily adjust this one. be careful if auto-generating. + # 2024-07-18 - edited again for mysql 8.4 restrict-fk-on-non-standard-key support with op.batch_alter_table('task', schema=None) as batch_op: if is_mysql(): batch_op.drop_index('guid') @@ -41,14 +42,16 @@ def upgrade(): elif is_mysql(): batch_op.drop_constraint('human_task_ibfk_5', type_='foreignkey') batch_op.drop_index('ix_human_task_task_model_id') - batch_op.create_index(batch_op.f('ix_human_task_task_guid'), ['task_guid'], unique=False) - batch_op.create_foreign_key('human_task_ibfk_task_guid', 'task', ['task_guid'], ['guid']) batch_op.drop_column('task_model_id') with op.batch_alter_table('task', schema=None) as batch_op: batch_op.drop_column('id') batch_op.create_primary_key('guid_pk', ['guid']) + with op.batch_alter_table('human_task', schema=None) as batch_op: + batch_op.create_index(batch_op.f('ix_human_task_task_guid'), ['task_guid'], unique=False) + batch_op.create_foreign_key('human_task_ibfk_task_guid', 'task', ['task_guid'], ['guid']) + # ### end Alembic commands ###