This commit is contained in:
EgorAnti
2026-11-19 10:57:39 +03:00
parent 75d2f78913
commit a4d58b2eec
26 changed files with 1141 additions and 96 deletions
+73
View File
@@ -0,0 +1,73 @@
param(
[Parameter(Mandatory = $true)]
[string]$AttachmentId,
[Parameter(Mandatory = $true)]
[ValidateRange(1, 2)]
[int]$PipelineVersion,
[string]$RequestedBy = $env:USERNAME,
[string]$EnvFile = ".env"
)
$parsedAttachmentId = [guid]::Empty
if (-not [guid]::TryParse($AttachmentId, [ref]$parsedAttachmentId)) {
throw "AttachmentId must be a UUID"
}
if ($RequestedBy -notmatch '^[A-Za-z0-9_.@-]{1,128}$') {
throw "RequestedBy contains unsupported characters"
}
$sql = @"
WITH candidate AS (
SELECT id, pipeline_version
FROM attachments
WHERE id = '$parsedAttachmentId'::uuid AND pipeline_version < $PipelineVersion
FOR UPDATE
), superseded AS (
UPDATE attachment_jobs j
SET status = 'superseded', published = true,
last_error_code = 'superseded_by_v$PipelineVersion',
lease_token = NULL, lease_until = NULL, updated_at = clock_timestamp()
FROM candidate c
WHERE j.attachment_id = c.id AND j.pipeline_version < $PipelineVersion
AND j.status IN ('queued', 'processing')
RETURNING j.id
), created AS (
INSERT INTO attachment_jobs
(id, attachment_id, pipeline_version, attempt, status, published,
next_attempt_at, created_at, updated_at)
SELECT gen_random_uuid(), id, $PipelineVersion, 1, 'queued', false,
clock_timestamp(), clock_timestamp(), clock_timestamp()
FROM candidate
ON CONFLICT (attachment_id, pipeline_version) DO NOTHING
RETURNING id, attachment_id
), promoted AS (
UPDATE attachments a
SET pipeline_version = $PipelineVersion, status = 'queued', error_code = NULL,
updated_at = clock_timestamp()
FROM candidate c, created n
WHERE a.id = c.id AND n.attachment_id = c.id
RETURNING a.id
), audited AS (
INSERT INTO attachment_pipeline_promotions
(attachment_id, from_version, to_version, job_id, requested_by)
SELECT c.id, c.pipeline_version, $PipelineVersion, n.id, '$RequestedBy'
FROM candidate c
JOIN created n ON n.attachment_id = c.id
JOIN promoted p ON p.id = c.id
RETURNING job_id
)
SELECT COALESCE((SELECT job_id::text FROM audited), 'no-op');
"@
$result = docker compose --env-file $EnvFile exec -T postgres `
psql -v ON_ERROR_STOP=1 -U messenger -d messenger -At -c $sql
if ($LASTEXITCODE -ne 0) {
throw "Pipeline promotion transaction failed"
}
$last = ($result | Select-Object -Last 1).Trim()
if ($last -eq "no-op") {
Write-Output "No-op: attachment is missing, already at v$PipelineVersion, or that version already exists"
} else {
Write-Output "Promoted attachment $parsedAttachmentId to pipeline v$PipelineVersion with job $last"
}
+59
View File
@@ -0,0 +1,59 @@
#!/bin/sh
set -eu
attachment_id="${1:?usage: promote-pipeline.sh ATTACHMENT_UUID PIPELINE_VERSION [REQUESTED_BY] [ENV_FILE]}"
pipeline_version="${2:?pipeline version is required}"
requested_by="${3:-operator}"
env_file="${4:-.env}"
case "$attachment_id" in
????????-????-????-????-????????????) ;;
*) echo "AttachmentId must be a UUID" >&2; exit 2 ;;
esac
case "$pipeline_version" in
1|2) ;;
*) echo "PipelineVersion must be 1 or 2" >&2; exit 2 ;;
esac
case "$requested_by" in
*[!A-Za-z0-9_.@-]*|'') echo "RequestedBy contains unsupported characters" >&2; exit 2 ;;
esac
sql="WITH candidate AS (
SELECT id, pipeline_version FROM attachments
WHERE id = '$attachment_id'::uuid AND pipeline_version < $pipeline_version FOR UPDATE
), superseded AS (
UPDATE attachment_jobs j SET status = 'superseded', published = true,
last_error_code = 'superseded_by_v$pipeline_version',
lease_token = NULL, lease_until = NULL, updated_at = clock_timestamp()
FROM candidate c WHERE j.attachment_id = c.id
AND j.pipeline_version < $pipeline_version AND j.status IN ('queued', 'processing')
RETURNING j.id
), created AS (
INSERT INTO attachment_jobs
(id, attachment_id, pipeline_version, attempt, status, published,
next_attempt_at, created_at, updated_at)
SELECT gen_random_uuid(), id, $pipeline_version, 1, 'queued', false,
clock_timestamp(), clock_timestamp(), clock_timestamp() FROM candidate
ON CONFLICT (attachment_id, pipeline_version) DO NOTHING
RETURNING id, attachment_id
), promoted AS (
UPDATE attachments a SET pipeline_version = $pipeline_version,
status = 'queued', error_code = NULL, updated_at = clock_timestamp()
FROM candidate c, created n WHERE a.id = c.id AND n.attachment_id = c.id
RETURNING a.id
), audited AS (
INSERT INTO attachment_pipeline_promotions
(attachment_id, from_version, to_version, job_id, requested_by)
SELECT c.id, c.pipeline_version, $pipeline_version, n.id, '$requested_by'
FROM candidate c JOIN created n ON n.attachment_id = c.id
JOIN promoted p ON p.id = c.id RETURNING job_id
)
SELECT COALESCE((SELECT job_id::text FROM audited), 'no-op');"
result="$(docker compose --env-file "$env_file" exec -T postgres \
psql -v ON_ERROR_STOP=1 -U messenger -d messenger -At -c "$sql")"
if [ "$result" = "no-op" ]; then
echo "No-op: attachment is missing, already at v$pipeline_version, or that version already exists"
else
echo "Promoted attachment $attachment_id to pipeline v$pipeline_version with job $result"
fi
+1
View File
@@ -20,6 +20,7 @@ WITH candidate AS (
JOIN attachments a ON a.id = j.attachment_id
WHERE j.id = '$parsedJobId'::uuid
AND j.status = 'failed' AND a.status = 'failed'
AND j.pipeline_version = a.pipeline_version
FOR UPDATE OF j, a
), audited AS (
INSERT INTO attachment_job_replays (job_id, requested_by, previous_attempt)
+1
View File
@@ -17,6 +17,7 @@ sql="WITH candidate AS (
SELECT j.id, j.attachment_id, j.attempt FROM attachment_jobs j
JOIN attachments a ON a.id = j.attachment_id
WHERE j.id = '$job_id'::uuid AND j.status = 'failed' AND a.status = 'failed'
AND j.pipeline_version = a.pipeline_version
FOR UPDATE OF j, a
), audited AS (
INSERT INTO attachment_job_replays (job_id, requested_by, previous_attempt)