fix: emit COMMENT and DISABLE for newly added table triggers#516
Conversation
The added-trigger branch in table.go collected CREATE TRIGGER SQL but did not call generateTriggerComment or generateTriggerEnabledState, causing trigger comments (and disabled state) to be silently dropped on first apply. The modified-trigger and view-trigger paths already handled both correctly. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Greptile SummaryThis PR fixes a silent data-loss bug where
Confidence Score: 4/5Safe to merge; the fix is a small, targeted addition that closes a silent omission in the add-trigger code path. The code change is correct and directly mirrors three pre-existing patterns in the same codebase. The only gaps are a missing test for the disabled-trigger-on-add case and a dropped trailing newline in plan.txt. Neither affects correctness of the generated SQL. testdata/diff/create_trigger/add_trigger/plan.txt — trailing newline was dropped during fixture regeneration. Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[AddedTriggers loop] --> B[generateTriggerSQLWithMode\nCREATE TRIGGER]
B --> C[collector.collect\nDiffOperationCreate]
C --> D{trigger.Comment != empty}
D -- Yes --> E[generateTriggerComment\nCOMMENT ON TRIGGER\nDiffOperationAlter]
D -- No --> F{trigger.Disabled}
E --> F
F -- Yes --> G[generateTriggerEnabledState\nALTER TABLE DISABLE TRIGGER\nDiffOperationAlter]
F -- No --> H[Next trigger]
G --> H
style B fill:#90EE90
style E fill:#90EE90
style G fill:#90EE90
%%{init: {'theme': 'base', 'themeVariables': {"darkMode": true, "background": "#0d1117", "primaryColor": "#21262d", "primaryTextColor": "#e6edf3", "primaryBorderColor": "#8b949e", "lineColor": "#8b949e", "textColor": "#e6edf3", "edgeLabelBackground": "#161b22", "actorBkg": "#21262d", "actorBorder": "#8b949e", "actorTextColor": "#e6edf3", "actorLineColor": "#8b949e", "signalColor": "#8b949e", "signalTextColor": "#e6edf3", "noteBkgColor": "#373320", "noteBorderColor": "#d4a72c", "noteTextColor": "#f0e6c0", "labelBoxBkgColor": "#21262d", "labelBoxBorderColor": "#8b949e", "labelTextColor": "#e6edf3", "loopTextColor": "#e6edf3", "activationBkgColor": "#30363d", "activationBorderColor": "#8b949e"}}}%%
flowchart TD
A[AddedTriggers loop] --> B[generateTriggerSQLWithMode\nCREATE TRIGGER]
B --> C[collector.collect\nDiffOperationCreate]
C --> D{trigger.Comment != empty}
D -- Yes --> E[generateTriggerComment\nCOMMENT ON TRIGGER\nDiffOperationAlter]
D -- No --> F{trigger.Disabled}
E --> F
F -- Yes --> G[generateTriggerEnabledState\nALTER TABLE DISABLE TRIGGER\nDiffOperationAlter]
F -- No --> H[Next trigger]
G --> H
style B fill:#90EE90
style E fill:#90EE90
style G fill:#90EE90
Reviews (1): Last reviewed commit: "fix: emit COMMENT and DISABLE for newly ..." | Re-trigger Greptile |
There was a problem hiding this comment.
Pull request overview
This PR fixes a schema convergence bug where newly added table triggers were created but their associated COMMENT ON TRIGGER and disabled state (ALTER TABLE ... DISABLE TRIGGER) were not emitted on the first apply, causing immediate post-apply drift (Issue #515).
Changes:
- Emit
COMMENT ON TRIGGERafterCREATE TRIGGERfor triggers in theAddedTriggerspath ininternal/diff/table.go. - Emit
ALTER TABLE ... DISABLE TRIGGERfor newly added triggers that are marked disabled in the desired state. - Update the existing
create_trigger/add_triggerfixture to include a trigger comment and verify the end-to-end plan/apply output.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| internal/diff/table.go | Ensures newly added table triggers also emit comment and disabled-state DDL, matching the behavior of modified-trigger and view-trigger paths. |
| testdata/diff/create_trigger/add_trigger/new.sql | Adds a trigger comment to the desired-state fixture for coverage. |
| testdata/diff/create_trigger/add_trigger/diff.sql | Updates expected diff output to include COMMENT ON TRIGGER for the newly added trigger. |
| testdata/diff/create_trigger/add_trigger/plan.sql | Updates expected plan SQL to include the trigger comment statement. |
| testdata/diff/create_trigger/add_trigger/plan.txt | Updates expected human-readable plan output to reflect the additional trigger comment step. |
| testdata/diff/create_trigger/add_trigger/plan.json | Updates expected machine-readable plan output to include an alter step for the trigger comment. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Both Greptile (4/5) and Copilot reviewed — no generated comments, safe to merge. Greptile noted two minor points:
|
Fixes #515
Summary
table.goemittedCREATE TRIGGERbut skippedCOMMENT ON TRIGGERandALTER TABLE DISABLE TRIGGER, causing trigger comments (and disabled state) to be silently dropped on first apply.create_trigger/add_triggertest fixture to include a trigger comment, verifying the fix end-to-end.Test plan
PGSCHEMA_TEST_FILTER="create_trigger/add_trigger" go test ./internal/diff/ -run TestDiffFromFilespassesPGSCHEMA_TEST_FILTER="create_trigger/add_trigger" go test ./cmd/ -run TestPlanAndApplypassesTestDiffFromFilessuite passes with no regressions🤖 Generated with Claude Code