Tasks 1.6 + 1.7 — schema tooling + real entrypoint flow

Two parallel tasks landing together. The boot pipeline is now wired
end-to-end: db-init → schema apply → directus bootstrap → pm2-runtime.
Live-verified by booting a fresh compose stack to a serving Directus
admin UI on :8055.

Task 1.6 — snapshot tooling:
- scripts/schema-snapshot.sh — host-side, dev-time. Verifies docker
  is on PATH and the directus compose service is running, runs
  `node /directus/cli.js schema snapshot --yes` inside the container,
  copies the YAML out to ./snapshots/schema.yaml. Used after admin-UI
  schema changes to capture the new state for git commit.
- scripts/schema-apply.sh — image-side, boot-time. Reads
  /directus/snapshots/schema.yaml, runs a dry-run preview, then
  applies. Gracefully skips when the snapshot is absent or whitespace-
  only (Phase 1 first-boot path before tasks 1.4/1.5 produce
  collections). SNAPSHOT_PATH env var override for CI flexibility.
- snapshots/README.md — lifecycle doc; warns against hand-editing.

Task 1.7 — real entrypoint flow:
- entrypoint.sh rewritten from Phase 1.1's placeholder to the
  4-step boot per ROADMAP design rule #3:
    1/4 db-init          → /directus/scripts/apply-db-init.sh
    2/4 schema apply     → /directus/scripts/schema-apply.sh
    3/4 directus bootstrap → node /directus/cli.js bootstrap
    4/4 directus start   → exec pm2-runtime start ecosystem.config.cjs
  set -euo pipefail halts boot on any step's non-zero exit. Each step
  emits a [entrypoint] log marker so an operator reading container
  logs sees which step failed.

Bug found and fixed during live verification:
- Both 1.6 scripts initially called bare `directus schema ...` as if
  the CLI were on PATH. Upstream directus/directus:11.17.4 does NOT
  expose `directus` on PATH — invocation is via `node /directus/cli.js`,
  same pattern as the entrypoint's bootstrap step. Both scripts
  corrected. Also added -T to docker compose exec in schema-snapshot.sh
  so the script works in non-TTY contexts (CI).

Phase 5 follow-up (non-blocking) flagged in 07's Done section: Directus
warns "Collection 'positions' doesn't have a primary key column and
will be ignored". The positions table uses UNIQUE INDEX (device_id, ts)
matching processor's pattern, not a PK constraint. Means positions is
not auto-registered as a Directus collection — fine for Phase 1, but
the operator faulty-flag workflow will need a custom endpoint or
manual collection registration in Phase 5.

ROADMAP marks 1.6 + 1.7 done. Phase 1 progress: 5/9 tasks complete
(1.1, 1.2, 1.3, 1.6, 1.7); 1.4, 1.5, 1.8, 1.9 remain.
This commit is contained in:
2026-05-01 23:14:28 +02:00
parent 25a9731070
commit e22d9d489a
7 changed files with 538 additions and 22 deletions
+154
View File
@@ -0,0 +1,154 @@
#!/usr/bin/env bash
# =============================================================================
# schema-apply.sh — TRM directus schema apply (image-side, boot-time)
#
# Applies the committed Directus schema snapshot to the running Postgres so
# the database schema matches what is in git. Called from entrypoint.sh as
# part of the three-step boot sequence:
# apply-db-init.sh → schema-apply.sh → directus start
#
# Usage
# Called automatically by entrypoint.sh (wired in Phase 1 task 1.7).
# Can also be run manually inside the container for debugging:
# bash scripts/schema-apply.sh
#
# Snapshot location
# /directus/snapshots/schema.yaml
# This is the image-baked path. The file is copied in by the Dockerfile.
# For the path to be overridden (e.g. in CI), set SNAPSHOT_PATH in the
# environment before calling this script.
#
# First-boot / no-snapshot behaviour
# If the snapshot file does not exist, or exists but contains only a
# .gitkeep marker (i.e. is empty or contains only whitespace), this script
# logs a skip message and exits 0. This is critical for Phase 1: tasks 1.4
# and 1.5 (collection creation) have not run yet, so there is no snapshot
# to apply. The entrypoint must not fail in this state.
#
# Dry-run preview
# Before applying, the script runs `directus schema apply --dry-run` and
# prints its output. This makes container boot logs self-explanatory:
# an operator reading logs sees exactly what is about to change.
# On a clean re-deploy where the DB already matches the snapshot, the diff
# output will show "No changes to apply" (or equivalent) and the real apply
# will be a no-op.
#
# Exit codes
# 0 Applied successfully -OR- snapshot not present / empty (skip).
# 1 directus schema apply failed (real apply, not dry-run).
# 2 directus CLI not found on PATH (image misconfiguration).
#
# Environment variables (all optional)
# SNAPSHOT_PATH Override the default /directus/snapshots/schema.yaml.
# Useful in CI where the path may differ.
# DEBUG Set to any non-empty value to enable extra verbosity.
#
# Wired into entrypoint.sh in Phase 1 task 1.7.
# =============================================================================
set -euo pipefail
# -----------------------------------------------------------------------------
# Logging helpers
# -----------------------------------------------------------------------------
log_info() {
printf '[schema-apply] %s\n' "$*"
}
log_error() {
printf '[schema-apply] ERROR: %s\n' "$*" >&2
}
# -----------------------------------------------------------------------------
# Configuration
# -----------------------------------------------------------------------------
SNAPSHOT_PATH="${SNAPSHOT_PATH:-/directus/snapshots/schema.yaml}"
# -----------------------------------------------------------------------------
# Step 0 — Verify the directus CLI is available
# -----------------------------------------------------------------------------
#
# Note: the upstream directus/directus image does NOT expose `directus` on
# PATH. The CLI is invoked as `node /directus/cli.js <subcommand>`, matching
# the upstream image's CMD (`node cli.js bootstrap && pm2-runtime ...`).
# We check that the cli.js entry script exists at the image-baked path.
readonly DIRECTUS_CLI=/directus/cli.js
if [[ ! -f "${DIRECTUS_CLI}" ]]; then
log_error "directus CLI not found at ${DIRECTUS_CLI}"
log_error "This script must run inside the directus container image."
exit 2
fi
# -----------------------------------------------------------------------------
# Step 1 — Check for snapshot existence and non-empty content
# -----------------------------------------------------------------------------
if [[ ! -f "${SNAPSHOT_PATH}" ]]; then
log_info "snapshot not found at ${SNAPSHOT_PATH} — no schema to apply, skipping"
exit 0
fi
# A file containing only a .gitkeep placeholder (empty or whitespace only)
# is treated as absent. Use `tr` to strip whitespace and check emptiness.
snapshot_content_stripped="$(tr -d '[:space:]' < "${SNAPSHOT_PATH}")"
if [[ -z "${snapshot_content_stripped}" ]]; then
log_info "snapshot at ${SNAPSHOT_PATH} is empty (placeholder only) — no schema to apply, skipping"
exit 0
fi
log_info "snapshot found at ${SNAPSHOT_PATH}"
if [[ -n "${DEBUG:-}" ]]; then
snapshot_bytes="$(wc -c < "${SNAPSHOT_PATH}" | tr -d '[:space:]')"
log_info "snapshot size: ${snapshot_bytes} bytes"
fi
# -----------------------------------------------------------------------------
# Step 2 — Dry-run preview (log the diff before applying)
# -----------------------------------------------------------------------------
log_info "--- schema diff preview (dry-run) ---"
dry_run_exit=0
# Capture output and stream it; we want each line prefixed for clarity.
while IFS= read -r line; do
log_info " ${line}"
done < <(node "${DIRECTUS_CLI}" schema apply --dry-run "${SNAPSHOT_PATH}" 2>&1) || dry_run_exit=$?
log_info "--- end diff preview ---"
# A non-zero dry-run exit is not fatal — some Directus versions exit non-zero
# when there are pending changes to report. We always proceed to the real
# apply step; only the real apply exit code is authoritative.
if [[ "${dry_run_exit}" -ne 0 ]]; then
log_info "dry-run exited ${dry_run_exit} (non-zero dry-run exit is non-fatal; proceeding to apply)"
fi
# -----------------------------------------------------------------------------
# Step 3 — Apply the snapshot
# -----------------------------------------------------------------------------
log_info "applying schema snapshot..."
apply_exit=0
apply_output=""
apply_output="$(node "${DIRECTUS_CLI}" schema apply --yes "${SNAPSHOT_PATH}" 2>&1)" || apply_exit=$?
# Always print the apply output so it appears in container logs.
while IFS= read -r line; do
log_info " ${line}"
done <<< "${apply_output}"
if [[ "${apply_exit}" -ne 0 ]]; then
log_error "directus schema apply failed (exit ${apply_exit})"
log_error "The container will not start. Fix the snapshot or the database state."
exit 1
fi
log_info "schema apply complete"
+168
View File
@@ -0,0 +1,168 @@
#!/usr/bin/env bash
# =============================================================================
# schema-snapshot.sh — TRM directus schema snapshot (host-side, dev-time)
#
# Captures the current Directus schema from the running dev compose stack and
# writes it to ./snapshots/schema.yaml for git commit.
#
# Usage
# Run from the repo root (where compose.dev.yaml lives):
# pnpm run schema:snapshot
# Or directly:
# bash scripts/schema-snapshot.sh
#
# Prerequisites
# - docker CLI must be on PATH.
# - The dev compose stack must be running:
# pnpm run dev (or: docker compose -f compose.dev.yaml up -d)
# - The directus service must be healthy (bootstrapped, not just started).
#
# What it does
# 1. Verifies docker is on PATH.
# 2. Verifies the compose directus service is in a running state.
# 3. Runs `directus schema snapshot --yes /tmp/schema-snapshot.yaml` inside
# the container (the container already has all required env vars for DB
# access).
# 4. Copies the generated file out to ./snapshots/schema.yaml.
# 5. Prints a one-line success log with the file size.
#
# Exit codes
# 0 Snapshot written successfully.
# 1 docker CLI not found, or directus service is not running, or snapshot
# command failed inside the container, or copy failed.
#
# Notes
# - The compose service name is `directus`; the compose file is
# `compose.dev.yaml`, resolved relative to the working directory.
# - Do NOT run this from inside the container — it is a host-side script.
# - Do NOT hand-edit snapshots/schema.yaml. Run this script after making
# schema changes via the Directus admin UI.
#
# Wired into package.json as `schema:snapshot`.
# =============================================================================
set -euo pipefail
# -----------------------------------------------------------------------------
# Logging helpers
# -----------------------------------------------------------------------------
log_info() {
printf '[schema-snapshot] %s\n' "$*"
}
log_error() {
printf '[schema-snapshot] ERROR: %s\n' "$*" >&2
}
# -----------------------------------------------------------------------------
# Configuration
# -----------------------------------------------------------------------------
COMPOSE_FILE="compose.dev.yaml"
COMPOSE_SERVICE="directus"
# Temporary path inside the container — chosen to be writable by the `node`
# user that the directus image runs as.
CONTAINER_TMP_PATH="/tmp/schema-snapshot.yaml"
# Destination on the host (relative to the repo root, where this script runs).
HOST_SNAPSHOT_PATH="./snapshots/schema.yaml"
# -----------------------------------------------------------------------------
# Step 1 — Verify docker CLI is available
# -----------------------------------------------------------------------------
if ! command -v docker > /dev/null 2>&1; then
log_error "docker CLI not found on PATH"
log_error "Install Docker Desktop or Docker Engine and ensure 'docker' is in your PATH."
exit 1
fi
log_info "docker CLI found: $(docker --version)"
# -----------------------------------------------------------------------------
# Step 2 — Verify the directus compose service is running
# -----------------------------------------------------------------------------
log_info "checking compose stack (${COMPOSE_FILE}) for service '${COMPOSE_SERVICE}'"
if [[ ! -f "${COMPOSE_FILE}" ]]; then
log_error "compose file '${COMPOSE_FILE}' not found."
log_error "Run this script from the directus/ repo root (where compose.dev.yaml lives)."
exit 1
fi
# `docker compose ps --status running --services` lists only services that are
# in the running state. We check whether our target service appears in that list.
running_services="$(docker compose -f "${COMPOSE_FILE}" ps --status running --services 2>&1)" || {
log_error "docker compose ps failed — is Docker running?"
log_error "Output: ${running_services}"
exit 1
}
if ! printf '%s\n' "${running_services}" | grep -qx "${COMPOSE_SERVICE}"; then
log_error "Directus container is not running."
log_error "Start the stack first: pnpm run dev"
log_error "(Services currently running: ${running_services:-<none>})"
exit 1
fi
log_info "service '${COMPOSE_SERVICE}' is running"
# -----------------------------------------------------------------------------
# Step 3 — Run `directus schema snapshot` inside the container
# -----------------------------------------------------------------------------
log_info "running 'directus schema snapshot' inside the container..."
snapshot_output=""
snapshot_exit=0
snapshot_output="$(
docker compose -f "${COMPOSE_FILE}" exec -T \
"${COMPOSE_SERVICE}" \
node /directus/cli.js schema snapshot --yes "${CONTAINER_TMP_PATH}" 2>&1
)" || snapshot_exit=$?
if [[ "${snapshot_exit}" -ne 0 ]]; then
log_error "directus schema snapshot failed (exit ${snapshot_exit})"
log_error "Container output:"
# Print each line prefixed so it's clearly from the container.
while IFS= read -r line; do
log_error " > ${line}"
done <<< "${snapshot_output}"
exit 1
fi
# -----------------------------------------------------------------------------
# Step 4 — Copy the snapshot out of the container
# -----------------------------------------------------------------------------
log_info "copying snapshot from container to ${HOST_SNAPSHOT_PATH}"
copy_exit=0
copy_output="$(
docker compose -f "${COMPOSE_FILE}" cp \
"${COMPOSE_SERVICE}:${CONTAINER_TMP_PATH}" \
"${HOST_SNAPSHOT_PATH}" 2>&1
)" || copy_exit=$?
if [[ "${copy_exit}" -ne 0 ]]; then
log_error "docker compose cp failed (exit ${copy_exit}): ${copy_output}"
exit 1
fi
# -----------------------------------------------------------------------------
# Step 5 — Report success
# -----------------------------------------------------------------------------
# Compute the size of the written file for the one-line success log.
if command -v stat > /dev/null 2>&1; then
# GNU stat (Linux) and BSD stat (macOS) use different flags; try both.
snapshot_bytes="$(stat -c%s "${HOST_SNAPSHOT_PATH}" 2>/dev/null \
|| stat -f%z "${HOST_SNAPSHOT_PATH}" 2>/dev/null \
|| echo "?")"
else
snapshot_bytes="?"
fi
log_info "snapshot written to snapshots/schema.yaml (${snapshot_bytes} bytes)"