#!/usr/bin/env bash # Regenerate the query layer. # # ./scripts/sqlc.sh regenerate into api/internal/store/dbgen # ./scripts/sqlc.sh diff regenerate elsewhere and compare, changing nothing # # sqlc runs in a container so it does not have to be installed on the host, the # same arrangement scripts/go.sh makes for the Go toolchain. # # The generated code is committed. `diff` is what CI uses to prove the commit # still matches the queries and the migrations it was generated from. set -euo pipefail cd "$(dirname "$0")/.." IMAGE=sqlc/sqlc:1.31.1 OUT=api/internal/store/dbgen # --user matters: without it the container writes the generated files as root, # and they land in the working tree owned by a user who cannot then commit them. DOCKER_USER=(--user "$(id -u):$(id -g)") run() { docker run --rm "${DOCKER_USER[@]}" -v "$PWD/api:/src" -w /src "$IMAGE" "$@"; } case "${1:-generate}" in generate) run generate echo "generated $OUT" ;; diff) # Generate into a scratch copy and compare. Not `git diff`: this has to work # on a dirty tree, and on a checkout that is not a git repository at all. tmp=$(mktemp -d) trap 'rm -rf "$tmp"' EXIT cp -r api "$tmp/api" rm -rf "$tmp/api/internal/store/dbgen" docker run --rm "${DOCKER_USER[@]}" -v "$tmp/api:/src" -w /src "$IMAGE" generate if diff -r "$OUT" "$tmp/api/internal/store/dbgen" >/dev/null 2>&1; then echo "generated code is up to date" else echo "generated code is stale — run ./scripts/sqlc.sh" diff -r "$OUT" "$tmp/api/internal/store/dbgen" | head -40 exit 1 fi ;; *) echo "usage: $0 {generate|diff}"; exit 2 ;; esac