fix: [outline] align database ownership before migrations

The ``pre_deploy`` hook reassigns every object of the outline database
(tables, sequences, views, materialized views, standalone types,
functions, procedures) to the application role before the container
runs its migrations.

Historical provisioning or restores running as the ``postgres``
superuser leave objects owned by ``postgres``, so any later ``ALTER``
on them fails with "must be owner of ..." and puts outline in a
crash-loop at migration time.  Seen on elabore.coop when upgrading
1.6.1 -> 1.10.0: migration
``20260714000000-add-mcp-to-search-queries-source.js`` failed on
``enum_search_queries_source``.

Extensions are excluded from the realignment (they are managed by the
``postgres`` charm).  The hook is idempotent, silent when there is no
drift, and blocks the deployment (``exit 1``) if any drift remains, so
the problem surfaces at deploy time instead of as a cryptic
crash-loop.
This commit is contained in:
Stéphan Sainléger
2026-09-16 23:41:42 +02:00
parent 31bcaab87f
commit 9947900389
4 changed files with 250 additions and 0 deletions

65
outline/lib/common Normal file
View File

@@ -0,0 +1,65 @@
# -*- mode: shell-script -*-
##
## Database access helpers (from cyclos/immich pattern in 0k-charms)
##
## Get target service name for a named relation
outline:relation-get-target-service() {
local relation="$1" ts
if ! read-0 ts _ _ < <(get_service_relation "$SERVICE_NAME" "$relation"); then
err "Couldn't find relation ${DARKCYAN}$relation${NORMAL}."
return 1
fi
e "$ts"
}
## Get the raw data of a named relation
outline:relation-get-config() {
local relation="$1" ts relation_dir
ts=$(outline:relation-get-target-service "$relation") || return 1
relation_dir=$(get_relation_data_dir "$SERVICE_NAME" "$ts" "$relation") || return 1
cat "${relation_dir}/data"
}
## Get a key from the relation data
outline:named-relation-get() {
local relation="$1" key="$2" config
config=$(outline:relation-get-config "$relation") || return 1
e "$config" | shyaml get-value "$key" || {
err "Couldn't get ${WHITE}$key${NORMAL} value" \
"in ${DARKCYAN}$relation${NORMAL} relation's data."
return 1
}
}
## Run SQL as the postgres superuser on the database related to this
## service through the "postgres-database" relation.
## Usage: sql < <(echo "SELECT ...")
## echo "SELECT ..." | sql
sql() {
(
local dbname ts target_charm target_charm_path
dbname="$(outline:named-relation-get "postgres-database" dbname)" || exit 1
ts=$(outline:relation-get-target-service "postgres-database") || exit 1
export SERVICE_NAME="$ts"
export SERVICE_DATASTORE="$DATASTORE/$SERVICE_NAME"
DOCKER_BASE_IMAGE=$(service_ensure_image_ready "$SERVICE_NAME") || exit 1
export DOCKER_BASE_IMAGE
target_charm=$(get_service_charm "$ts") || exit 1
target_charm_path=$(charm.get_dir "$target_charm") || exit 1
set +e
. "$target_charm_path/lib/common"
set -e
ensure_db_docker_running
ddb -d "$dbname" -v ON_ERROR_STOP=1
)
}