Skip to content

refactor(ast): add named DropBehavior constants#4461

Draft
luongs3 wants to merge 1 commit into
sqlc-dev:mainfrom
luongs3:refactor/drop-behavior-constants
Draft

refactor(ast): add named DropBehavior constants#4461
luongs3 wants to merge 1 commit into
sqlc-dev:mainfrom
luongs3:refactor/drop-behavior-constants

Conversation

@luongs3
Copy link
Copy Markdown

@luongs3 luongs3 commented May 28, 2026

Adds named DropBehavior constants to internal/sql/ast/drop_behavior.go. No behavior change — purely additive.

Why

internal/sql/ast currently has:

type DropBehavior uint

func (n *DropBehavior) Pos() int {
	return 0
}

The numeric values come from pganalyze/pg_query_go's DropBehavior enum, but they are nowhere named on the sqlc side. Callers comparing against the wire value have to write stmt.Behavior == 2 for CASCADE — a magic number that's invisible to grep and silently miscompiles if pg_query reshuffles the enum.

This PR adds:

const (
	DropBehaviorUndefined DropBehavior = 0
	DropBehaviorRestrict  DropBehavior = 1
	DropBehaviorCascade   DropBehavior = 2
)

Comments at the top of the file document where the values come from so future contributors don't have to chase the pg_query source.

What does NOT change

  • No use-site is updated in this PR. Existing comparisons against integer literals continue to compile.
  • No behavior or AST shape changes — only constants.
  • No new tests required: the constants are typed and validated by the compiler.

Context

This is the follow-up I offered in a review note on #4419. That PR uses stmt.Behavior == 2 in internal/sql/catalog/table.go. Once the constants land, refactoring those use-sites to stmt.Behavior == ast.DropBehaviorCascade is a 1-line change — happy to bundle into #4419 (if maintainers prefer one PR) or send as a separate sweep PR after #4419 merges. Both work; I have no preference.

The pg_query DropBehavior enum currently lives as a bare `type DropBehavior uint` —
callers that compare against the wire values have to write magic numbers
(`stmt.Behavior == 2` for CASCADE). Add named constants matching pganalyze/pg_query_go's
DropBehavior enum so usage sites can read `stmt.Behavior == ast.DropBehaviorCascade`
instead, and survive a future pg_query enum reshuffle without silently miscompiling.

No behavior change — just constants. Existing code that compares to integer literals
continues to work; this is purely additive.

Follow-up to the review on sqlc-dev#4419 (which currently uses `Behavior == 2` literal):
happy to apply the same rename in that PR's diff once it lands, or in a separate
sweep PR depending on what the maintainers prefer.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant