Skip to content

Modular logistics and supply chain operating system (LSOS)

ci(api): run the fresh migrate on MySQL 8.4.8 and check foreign key targets

Ronald A. Richardson80981269a554
diff --git a/.github/workflows/api.yml b/.github/workflows/api.yml
index 8ac62d17..094520b3 100644
--- a/.github/workflows/api.yml
+++ b/.github/workflows/api.yml
@@ -144,13 +144,21 @@ jobs:
# on SQLite stand-in schemas that never load the spatial connection, so they stay
# green while a migration or the connection class itself is fatal on MySQL.
migrate:
- name: Fresh MySQL Migrate
+ name: Fresh MySQL Migrate (${{ matrix.mysql }})
runs-on: ubuntu-latest
timeout-minutes: 30
+ # 8.4 turns on restrict_fk_on_non_standard_key by default, as production's RDS 8.4.8 does.
+ # A fresh install still needs core-api's migration hook for the older create migrations; the
+ # last step then proves that, once migrated, no table needs it.
+ strategy:
+ fail-fast: false
+ matrix:
+ mysql: ['8.0-oracle', '8.4.8']
+
services:
mysql:
- image: mysql:8.0-oracle
+ image: mysql:${{ matrix.mysql }}
env:
MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
ports:
@@ -233,3 +241,6 @@ jobs:
- name: Assert both connections migrated
run: bash ../scripts/ci/assert-migrated.sh
+
+ - name: Assert foreign keys point at unique keys
+ run: bash ../scripts/ci/assert-fk-targets-unique.sh
diff --git a/scripts/ci/assert-fk-targets-unique.sh b/scripts/ci/assert-fk-targets-unique.sh
new file mode 100755
index 00000000..176f748a
--- /dev/null
+++ b/scripts/ci/assert-fk-targets-unique.sh
@@ -0,0 +1,79 @@
+#!/usr/bin/env bash
+# Checks that every foreign key points at a unique or primary key, after `migrate` and `sandbox:migrate`.
+#
+# MySQL 8.4 turns on `restrict_fk_on_non_standard_key` by default. With it on, MySQL refuses any ALTER
+# of a table that a foreign key points at through a non-unique key ("1553 Cannot drop index
+# '<unknown key name>'"). core-api turns the setting off while migrations run, but nothing else does,
+# so this runs as a plain mysql client with the server's default and no Laravel in between.
+# Run from api/ with the DB_* environment the app uses.
+set -euo pipefail
+
+main="$DB_DATABASE"
+schemas="'$main','${main}_sandbox','${main}_storefront'"
+sql() {
+ MYSQL_PWD="${DB_PASSWORD:-}" mysql -h"$DB_HOST" -P"$DB_PORT" -u"$DB_USERNAME" -N -B -e "$1"
+}
+
+failed=0
+fail() {
+ echo "::error::$1"
+ failed=1
+}
+
+setting="$(sql "SELECT @@global.restrict_fk_on_non_standard_key" 2>/dev/null || echo 'absent')"
+echo "MySQL $(sql 'SELECT VERSION()'), restrict_fk_on_non_standard_key: $setting"
+
+# 1. Every column a foreign key points at has a unique or primary key of its own.
+nonstandard="$(sql "
+ SELECT DISTINCT CONCAT(k.REFERENCED_TABLE_SCHEMA, '.', k.REFERENCED_TABLE_NAME, '.', k.REFERENCED_COLUMN_NAME)
+ FROM information_schema.KEY_COLUMN_USAGE k
+ WHERE k.REFERENCED_TABLE_NAME IS NOT NULL
+ AND k.REFERENCED_TABLE_SCHEMA IN ($schemas)
+ AND EXISTS (SELECT 1 FROM information_schema.TABLES t
+ WHERE t.TABLE_SCHEMA = k.REFERENCED_TABLE_SCHEMA AND t.TABLE_NAME = k.REFERENCED_TABLE_NAME)
+ AND NOT EXISTS (
+ SELECT 1 FROM information_schema.STATISTICS s
+ WHERE s.TABLE_SCHEMA = k.REFERENCED_TABLE_SCHEMA AND s.TABLE_NAME = k.REFERENCED_TABLE_NAME
+ AND s.NON_UNIQUE = 0 AND s.COLUMN_NAME = k.REFERENCED_COLUMN_NAME AND s.SEQ_IN_INDEX = 1
+ AND (SELECT COUNT(*) FROM information_schema.STATISTICS s2
+ WHERE s2.TABLE_SCHEMA = s.TABLE_SCHEMA AND s2.TABLE_NAME = s.TABLE_NAME AND s2.INDEX_NAME = s.INDEX_NAME) = 1)
+ ORDER BY 1")"
+if [ -n "$nonstandard" ]; then
+ echo "$nonstandard"
+ fail "Foreign keys point at columns with no unique key. Give each new table's uuid ->unique(), or add a migration that makes these unique."
+else
+ echo "Every foreign key points at a unique or primary key."
+fi
+
+# 2. No foreign key points at a table that doesn't exist, as sandbox's access_token_id keys used to.
+dangling="$(sql "
+ SELECT CONCAT(c.CONSTRAINT_SCHEMA, '.', c.TABLE_NAME, ' ', c.CONSTRAINT_NAME, ' -> ', c.UNIQUE_CONSTRAINT_SCHEMA, '.', c.REFERENCED_TABLE_NAME)
+ FROM information_schema.REFERENTIAL_CONSTRAINTS c
+ WHERE c.CONSTRAINT_SCHEMA IN ($schemas)
+ AND NOT EXISTS (SELECT 1 FROM information_schema.TABLES t
+ WHERE t.TABLE_SCHEMA = c.UNIQUE_CONSTRAINT_SCHEMA AND t.TABLE_NAME = c.REFERENCED_TABLE_NAME)
+ ORDER BY 1")"
+if [ -n "$dangling" ]; then
+ echo "$dangling"
+ fail "Foreign keys point at tables that don't exist."
+fi
+
+# 3. Every table a foreign key points at can be altered with the server's default setting.
+parents="$(sql "
+ SELECT DISTINCT CONCAT('\`', REFERENCED_TABLE_SCHEMA, '\`.\`', REFERENCED_TABLE_NAME, '\`')
+ FROM information_schema.KEY_COLUMN_USAGE k
+ WHERE REFERENCED_TABLE_NAME IS NOT NULL AND REFERENCED_TABLE_SCHEMA IN ($schemas)
+ AND EXISTS (SELECT 1 FROM information_schema.TABLES t
+ WHERE t.TABLE_SCHEMA = k.REFERENCED_TABLE_SCHEMA AND t.TABLE_NAME = k.REFERENCED_TABLE_NAME)
+ ORDER BY 1")"
+altered=0
+for table in $parents; do
+ if output="$(sql "ALTER TABLE $table ADD COLUMN ci_fk_probe INT NULL; ALTER TABLE $table DROP COLUMN ci_fk_probe;" 2>&1)"; then
+ altered=$((altered + 1))
+ else
+ fail "ALTER TABLE $table failed: $output"
+ fi
+done
+echo "Altered $altered tables that foreign keys point at."
+
+exit "$failed"