Commit fe8e432
committed
ALTER TABLE ADD / DROP CONSTRAINT across all four families:
[WITH CHECK | WITH NOCHECK] ADD [CONSTRAINT name] (PRIMARY KEY | UNIQUE | FOREIGN KEY | CHECK | DEFAULT) … routes through a new Simulation.AlterTableConstraint.cs partial — FK reuses ResolveForeignKeys via a single-element pending list (referenced-key + Msg 1785 cascade-cycle validation); PK / UQ resolve column names against the live table and append into HeapTable.KeyConstraints (now List<KeyConstraint>, mutable contents on a readonly reference — same shape applied to CheckConstraints); DEFAULT introduces a new DefaultConstraint storage type alongside HeapColumn.Default (now mutable), with inline DEFAULT at CREATE TABLE auto-allocating a system-named entry so sys.default_constraints reflects both ALTER-added and CREATE-inline forms. WITH-CHECK (default) existing-data scan raises Msg 1505 ("CREATE UNIQUE INDEX statement terminated …") on PK / UQ duplicates and Msg 547 with the "ALTER TABLE statement" verb prefix on FK orphan / CHECK false rows (the only wording variance from runtime INSERT-time conflicts); WITH NOCHECK skips and sets ForeignKey.IsNotTrusted / CheckConstraint.IsNotTrusted (exposed via sys.foreign_keys.is_not_trusted + the new sys.check_constraints view). New SchemaErrors factories: PrimaryKeyAlreadyExists (1779), DuplicateKeyOnCreate (1505), ColumnAlreadyHasDefault (1781), DefaultColumnInvalid (1752), ForeignKeyInvalidColumn (1769), IndexColumnMissing (1911), NotAConstraint (3728), ConstraintReferencedByForeignKey (3725); ConstraintErrors gains AlterCheckConstraintConflict / AlterForeignKeyConflict — the trailing Msg 1750 / 3727 informational pair that real SQL Server emits after each gets dropped (documented quirk, single primary error per failure). DROP shapes — DROP CONSTRAINT [IF EXISTS] name [, …] — walk KeyConstraints / CheckConstraints / OutgoingForeignKeys / per-column DefaultConstraint in order (first hit wins), atomically validated: any Msg 3728 / 3725 against any name in the list leaves the entire table's constraint state unchanged (probe-confirmed). PK / UQ blocked from drop while an incoming FK references them; FK drop detaches from both endpoints; DEFAULT drop clears HeapColumn.Default + HeapColumn.DefaultConstraint in lockstep. Three new catalog views ship — sys.check_constraints (parent_column_id 1-based for inline / 0 for table-level; is_not_trusted + is_system_named; definition NULL since the simulator stores parsed trees not source text), sys.key_constraints (PK / UQ split via type / type_desc, is_system_named inferred from name prefix), sys.default_constraints (one row per named DEFAULT, inline + ALTER both populate). ALTER TABLE dispatch in Simulation.Alter.cs widens beyond SET SYSTEM_VERSIONING — switch over the post-name token routes Set / Add / Drop branches; identifier-leading ADD (the ADD COLUMN sibling) raises NotSupportedException for the consistent unmodeled signal. MoveNextRequired → MoveNextOptional adjustments in ParseReferentialAction handle ALTER's end-of-batch case. AutoCheckName / AutoForeignKeyName / AutoDefaultName consolidated through a new Fnv1a32 accumulator + FormatAutoConstraintName formatter (PK / UQ's 64-bit / 16-hex form stays separate, matching real SQL Server's object-id-derived shape there). 31 new AlterTableConstraintTests cover ADD across all five families, WITH NOCHECK plumbing + is_not_trusted catalog reflection, existing-data validation per family, DROP including IF EXISTS + atomic multi-drop + referenced-by-FK rejection, all three catalog views, and the ADD-COLUMN-still-unmodeled path. New docs/claude/alter-table.md captures the full grammar / validation / catalog surface / fidelity gaps; CLAUDE.md trigger phrase added; ALTER-TABLE "Not modeled" entry rewritten; foreign-keys.md fidelity-gaps section collapsed (ALTER ADD / DROP / WITH NOCHECK paths no longer deferred).1 parent 8ea005a commit fe8e432
17 files changed
Lines changed: 1767 additions & 76 deletions
File tree
- SqlServerSimulator.Tests
- SqlServerSimulator
- Errors
- Simulation
- Storage
- docs/claude
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
152 | 152 | | |
153 | 153 | | |
154 | 154 | | |
| 155 | + | |
155 | 156 | | |
156 | 157 | | |
157 | 158 | | |
| |||
176 | 177 | | |
177 | 178 | | |
178 | 179 | | |
179 | | - | |
| 180 | + | |
180 | 181 | | |
181 | 182 | | |
182 | 183 | | |
| |||
0 commit comments