fix(database): escape DO block dollar-quote for HCL templating (#317) #318
No reviewers
Labels
No labels
domain:backend
domain:devops
domain:frontend
status:approved
status:in-progress
status:needs-fix
status:qa
type:bug
type:devops
type:feature
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
ldraney/pal-e-platform!318
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "317-fix-do-block-dollar-escape"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Summary
Fix the SQL anonymous DO block's dollar-quote so it survives HCL string templating. Replace
DO $$ ... $$;withDO $body$ ... $body$;.Why
HCL strings interpret
$$as a literal-$escape (to prevent Terraform's${expression}interpolation). The Job's SQL heredoc hadDO $$ ... $$;which rendered asDO $ ... $;in the final Job spec. Postgres rejected withsyntax error at or near "$" at character 4. Job hit BackoffLimitExceeded after 4 retries; apply errored.Discovered when Ava ran
make tofu-apply— postgres pod logs showed the rendered SQL with single dollars where doubles were intended.Named dollar-quote (
$body$) is the cleanest fix: postgres treats any matched$tag$as a delimiter, and$body$contains no$$so HCL doesn't touch it. Self-documenting too.Changes
terraform/modules/database/main.tfline 193:DO $$→DO $body$terraform/modules/database/main.tfline 201:$$;→$body$;Test Plan
tofu apply -target=module.database -lock=falseruns without Job failureSucceeded(vs current failure state)==> admin_app role provisioned successfully\du admin_appshows the role exists with login + passwordReview Checklist
$tag$)$body$has no$$)Closes #317story:admin-row-crud,arch:postgres)Related Notes
pal-e-platform#315(label value/), salt master crash 12 days ago — third post-merge runtime gap during this bootstrapfeedback_tofu_validate_not_k8s_api,feedback_verification_before_completionCloses #317
HCL strings interpret $$ as a literal-$ escape (to prevent ${...} interpolation). The SQL heredoc had `DO $$ ... $$;` which rendered as `DO $ ... $;` — postgres rejected with syntax error at character 4. Switch to named dollar-quote `DO $body$ ... $body$;`. Postgres treats any matched $tag$ as a dollar-quote delimiter; $body$ contains no $$ so HCL doesn't touch it. Self-documenting and dodges the HCL escape collision entirely. Closes #317 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>PR #318 Review
DOMAIN REVIEW
Stack: Terraform/HCL + k8s Job rendering inline SQL via heredoc.
Correctness:
$$is the escape for literal$(to dodge${...}interpolation). The previousDO $$ ... $$;rendered asDO $ ... $;in the Job spec — confirmed root cause of the postgressyntax error at or near "$".$body$. Postgres accepts any matched$tag$as a delimiter (alphanumeric/underscore tag, must match on open/close) —$body$is valid.$body$contains no$$substring, so HCL leaves it untouched. Both ends symmetric.<<'SQL') so shell doesn't expand inside, but HCL templating still applies at the Terraform layer — that's the layer being fixed here. Correct.BEGIN/END,IF NOT EXISTS,format()with%Lquoting,:'admin_pw'psql variable binding, GRANT statements all unchanged.BLOCKERS
None.
NITS
\du admin_app) are unchecked — expected, but should be ticked during validation column work, not silently skipped.SOP COMPLIANCE
317-fix-do-block-dollar-escapefollows{issue}-{kebab}conventionCloses #317presentstory:admin-row-crud,arch:postgres) noted in bodyPROCESS OBSERVATIONS
Third post-merge runtime gap during bootstrap (alongside #315 label-value
/and salt master crash) — pattern istofu validatepasses but k8s API rejects rendered output. Worth a convention note: HCL-templated inline scripts (SQL, shell, etc.) should prefer named delimiters over$$/%%-style doubles to dodge HCL escape rules. Add to validation pipeline: render Job spec and grep for\$\s/\$;before apply.VERDICT: APPROVED