From 1c420283f4a0a6f821dd625cfe8c04dbef68adcf Mon Sep 17 00:00:00 2001 From: Hermenix Date: Tue, 3 Nov 2026 04:32:29 +0000 Subject: [PATCH] buzz: prebuild artifact-sync indexes before relay migrates The relay image is ghcr.io/block/buzz:main with autoUpdate and BUZZ_AUTO_MIGRATE=true. On 2026-11-02 an auto-update pulled a newer build whose migrations include 0078_artifact_sync_indexes.sql. That migration refuses to create its four indexes inline once the target table is populated -- it raises "prebuild concurrently" and points at docs/artifact-sync-deployment.md. channel_members is populated on a live relay, so migration 78 aborts, the relay exits 1 and has been crash-looping ever since. The schema sits at 0077, so every earlier migration (including 0077_artifact_sync_timestamps) is applied; rolling the image back is not an option because sqlx rejects a schema that is ahead of the binary's known migrations. Fix the gate instead of the image: a one-shot compose service, relay-index-prebuild, runs on the postgres image after postgres reports healthy and builds the four indexes CONCURRENTLY. Each index is checked first -- missing, valid, or invalid (present but not indisvalid/indisready/indislive); an invalid leftover is dropped concurrently before the rebuild, and an already-valid index is left alone, so re-runs are cheap and safe. The relay now waits on relay-index-prebuild completing successfully, next to its existing postgres/redis/minio/minio-init conditions. Authored by hermenix. --- hosts/hl-minipc1/services/buzz.nix | 42 ++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/hosts/hl-minipc1/services/buzz.nix b/hosts/hl-minipc1/services/buzz.nix index 74fb1bb..afb5124 100644 --- a/hosts/hl-minipc1/services/buzz.nix +++ b/hosts/hl-minipc1/services/buzz.nix @@ -42,6 +42,7 @@ in redis.condition = "service_healthy"; minio.condition = "service_healthy"; minio-init.condition = "service_completed_successfully"; + relay-index-prebuild.condition = "service_completed_successfully"; }; networks = [ "internal" ]; environmentFiles = [ config.age.secrets.buzz-env.path ]; @@ -190,6 +191,47 @@ in }; restart = "no"; }; + relay-index-prebuild = { + image = buzz.images.posgres; + networks = [ "internal" ]; + environmentFiles = [ config.age.secrets.buzz-env.path ]; + environment = { + POSTGRES_DB = "buzz"; + POSTGRES_USER = "buzz"; + }; + files."/usr/local/bin/prebuild-indexes.sh" = '' + #!/bin/sh + set -eu + export PGPASSWORD="$POSTGRES_PASSWORD" + + run_psql() { + psql -v ON_ERROR_STOP=1 -h postgres -U "$POSTGRES_USER" -d "$POSTGRES_DB" "$@" + } + + ensure_index() { + name="$1" + sql="$2" + state=$(run_psql -tAc "SELECT coalesce((SELECT CASE WHEN i.indisvalid AND i.indisready AND i.indislive THEN 'valid' ELSE 'invalid' END FROM pg_index i WHERE i.indexrelid = to_regclass('public.$name')), 'missing')") + if [ "$state" = valid ]; then + return 0 + fi + if [ "$state" = invalid ]; then + run_psql -c "DROP INDEX CONCURRENTLY IF EXISTS public.$name" + fi + run_psql -c "$sql" + } + + ensure_index artifact_heads_modified "CREATE INDEX CONCURRENTLY artifact_heads_modified ON public.artifact_heads (community_id, modified_at, artifact_id);" + ensure_index artifact_heads_channel "CREATE INDEX CONCURRENTLY artifact_heads_channel ON public.artifact_heads (community_id, channel_id, artifact_id);" + ensure_index channel_members_sync "CREATE INDEX CONCURRENTLY channel_members_sync ON public.channel_members (community_id, pubkey, sync_modified_at, channel_id);" + ensure_index artifact_revisions_moves "CREATE INDEX CONCURRENTLY artifact_revisions_moves ON public.artifact_revisions (community_id, modified_at, event_id) WHERE source_channel_id IS NOT NULL;" + ''; + extraConfig = { + entrypoint = [ "/bin/sh" "/usr/local/bin/prebuild-indexes.sh" ]; + depends_on.postgres.condition = "service_healthy"; + }; + restart = "no"; + }; }; }; }