Skip to content

Use one foreign-key state in ALTER TABLE - #2764

Merged
manticore-projects merged 2 commits into
JSQLParser:masterfrom
minleejae:refactor/alter-foreign-key-state-20261011
Oct 11, 2026
Merged

manticore-projects merged 2 commits into
JSQLParser:masterfrom
minleejae:refactor/alter-foreign-key-state-20261011

Conversation

@minleejae

@minleejae minleejae commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

Purpose

Make ALTER foreign-key edits consistent across the structured and legacy APIs. For example, after parsing an ADD FOREIGN KEY, setFkSourceTable("parent_history") currently changes the legacy getter while the SQL still references the original table.

Current design (As-is)

After #2747 separates constraints from indexes, AlterExpression still copies four FK properties and referential actions from ForeignKeyConstraint. Its renderer prefers the structured definition, so legacy edits can be ignored, replacement definitions leave stale getter values, and action column lists are absent from the copied state. A separate legacy renderer and validator path maintain these copies.

Target design (To-be)

  • Keep one ForeignKeyConstraint/ForeignKeyReference. Remove the duplicate FK fields, action store, grammar copying and fallback renderer; delegate existing FK accessors to that definition.
  • Share referenced-column lists and action objects. Make local FK names a rendered snapshot, matching PK/UK accessors; setters/adders and structured key elements provide editing APIs.
  • Create an FK for non-null legacy writes only when no competing definition exists. Reject writes over another constraint, index or legacy PK/UK. Absent reads, clears and removals create no payload; replacing the constraint exposes only the replacement.
  • Preserve the reference table object, catalogue and quoted identifier parts. Fix withFkSourceSchema() and validate the current structured target/columns without a second unqualified-table lookup. Document the editing and snapshot contracts.

PK/UK fallback cleanup and separate DROP targets are handled by #2771. The FK creation guard uses the existing key accessors so it remains compatible when that follow-up removes their backing fields. This PR keeps the parent’s index/constraint separation and does not expand SQL grammar.

Validation

  • Java 17 Gradle check passes: 9,964 tests, 9,939 passed, 25 skipped, zero failures/errors; grammar, formatting, Checkstyle, PMD, SpotBugs and coverage checks pass.

  • 15 FK regression cases plus one validator case cover MySQL/PostgreSQL mutation, rendering/reparsing, action column lists, quoted names, replacement/null handling and competing definitions. Running the FK cases on the parent produces 13 failures and two passing controls.

  • Before the parent merge, replayed 882 previously DB-validated DDL fixtures through the parser: 875 supported cases have unchanged AST inspection and SQL outputs; seven existing parse gaps remain. Error differences are generated grammar locations only. No fresh live DB replay.

  • Separate index and constraint ASTs (breaking API change) #2747 is now merged. Rebased only this follow-up onto current master (cdbaf1ad), verified that its patch is unchanged, and reran the complete checks above.

  • Integration check with the other independent refactors (Use one foreign-key state in ALTER TABLE #2764–Expose editable ALTER column identity actions #2768): all five patches apply without conflicts; full Java 17 Gradle check passes with 10,030 tests (10,005 passed, 25 skipped). Replaying 882 previously DB-validated fixtures preserves the AST and both SQL outputs for 875 supported cases, with the same seven existing parse gaps and no semantic regression. No fresh database execution.

  • The accessor-only compatibility adjustment for Separate ALTER key definitions from column drop targets #2771 was also verified with the complete Java 17 Gradle check; all checks pass.

@manticore-projects

Copy link
Copy Markdown
Contributor

Go ahead please.

Delegate legacy FK accessors to the structured constraint, remove copied grammar state and the fallback renderer, and validate the current reference. Preserve quoted name parts and protect existing non-FK definitions.

This follow-up is based on PR JSQLParser#2747 at 9be3636.

Signed-off-by: minleejae <mmj9808@gmail.com>
Signed-off-by: minleejae <mmj9808@gmail.com>
@manticore-projects
manticore-projects merged commit 49c1d52 into JSQLParser:master Oct 11, 2026
10 checks passed
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.

2 participants