Files

73 lines
3.3 KiB
Bash
Executable File

#!/bin/sh
# =============================================================================
# Install capability schema streams. Installation only — promotion is a separate step, and a
# separate container, because it is a separate decision.
#
# Capability streams are not one Flyway run. Each of db/migration/jpa/* declares its own V1 and keeps
# its own history table, so pointing a single Flyway at all of them fails with "Found more than one
# migration with version 1" — which is what nine Compose lanes discovered the moment Wave 2 stopped
# PostgreSqlPersistenceConfig from discarding spring.flyway.locations.
#
# Two things come first regardless of what was requested, and the order between them is not a
# preference:
#
# 1. db/migration/postgresql, the application's own stream, into the default flyway_schema_history.
# The application ships baseline-on-migrate: false as policy (FLYWAY-C6, and re-enabling it under
# prod is a boot failure), so it refuses to start against a schema that has tables but no history
# table of its own. Installing any capability stream before this one produces exactly that state:
# the lane's first run applied ten notification migrations and then the application refused with
# "Found non-empty schema(s) but no schema history table" — correctly.
# 2. db/migration/jpa/core, which creates capability_schema_registry, the table every other stream
# registers itself into.
#
# Each stream registers itself INSTALLED_INACTIVE. Nothing here promotes anything — a table existing
# is not the same as a capability being sanctioned to use it, and this image has no psql to blur the
# two with even if that were wanted. infra/postgres/promote-capability-streams.sh is the operator
# half, and NotificationSchemaActivation refusing startup until it has run is the fail-closed third.
#
# CAPABILITY_STREAMS is a space-separated list of directory names under db/migration/jpa.
# =============================================================================
set -eu
: "${PGHOST:?PGHOST is required}"
: "${PGUSER:?PGUSER is required}"
: "${PGDATABASE:?PGDATABASE is required}"
: "${CAPABILITY_STREAMS:=}"
MIGRATIONS=/flyway/sql
JDBC="jdbc:postgresql://${PGHOST}:${PGPORT:-5432}/${PGDATABASE}"
run_flyway() {
location="$1"
history="$2"
baseline="$3"
[ -d "${MIGRATIONS}/${location}" ] || {
echo "capability-streams: no such stream 'db/migration/${location}'" >&2
exit 1
}
echo "capability-streams: applying ${location} into ${history}"
# shellcheck disable=SC2086
flyway \
-url="${JDBC}" -user="${PGUSER}" -password="${PGPASSWORD:-}" \
-locations="filesystem:${MIGRATIONS}/${location}" \
-table="${history}" \
${baseline} \
migrate
}
apply_stream() {
stream="$1"
# Capability streams baseline at 0 because each is installed into a database the core stream has
# already put tables in; the application's own stream must not, for the reason above.
run_flyway "jpa/${stream}" "flyway_jpa_$(echo "${stream}" | tr '-' '_')_history" \
"-baselineOnMigrate=true -baselineVersion=0"
}
run_flyway postgresql flyway_schema_history ""
apply_stream core
for stream in ${CAPABILITY_STREAMS}; do
apply_stream "${stream}"
done
echo "capability-streams: installed postgresql, core, [${CAPABILITY_STREAMS}]; none promoted"