Skip to content

feat(schema): expose FK/CHECK constraints and a round-trip CREATE TABLE (#574) - #577

Merged
MPCoreDeveloper merged 2 commits into
masterfrom
feat/574-constraint-metadata-and-ddl
Dec 6, 2026
Merged

MPCoreDeveloper merged 2 commits into
masterfrom
feat/574-constraint-metadata-and-ddl

Conversation

@MPCoreDeveloper

Copy link
Copy Markdown
Owner

What

Issue #574: expose foreign-key and CHECK constraints through every client metadata route and provide a reliable
round-trip CREATE TABLE. That is the precondition SCDMS listed for its table designer, ER diagram and schema
compare (it had to mark generated DDL "approximate").

How

  • Engine — IMetadataProvider.GetConstraints(table) publishes the constraint list information_schema and the
    gRPC RPC already read (DataStructures/TableConstraints), now including a foreign key's referenced
    table/column, the referenced key's constraint name and ON UPDATE/ON DELETE. IMetadataProvider.GetCreateTableScript(table)
    builds the statement through the new DataStructures/TableDdlGenerator from the same records, so a script cannot
    describe a table differently from the metadata.
  • Protocol/clients — additive proto fields only (ConstraintSchema.referenced_table/referenced_columns/
    referenced_constraint/on_update/on_delete, TableSchema.create_table_script, SchemaRequest.include_create_table_script);
    REST /api/v1/schema gains constraints and createTableScript (?includeScripts=true); ADO.NET GetSchema gains
    Constraints, ForeignKeys, CheckConstraints and TableScripts collections plus the key/default flags on Columns;
    the .NET client forwards the new flag.

Defects fixed on the way

  1. Single-file storage skipped every table-level CHECK — the same DDL produced a constraint on directory storage
    and none on .scdb.
  2. Single-file storage ignored ON DELETE/ON UPDATE, and defaulted to NO ACTION where the directory reader
    records RESTRICT.
  3. A quoted identifier inside a FOREIGN KEY was stored with its quotes ("customers"), naming a table that does
    not exist.

Verification

  • TableDdlGeneratorTests — 17 cases, directory and single-file: every generated script is executed against a fresh
    database and both tables' columns/constraints are compared field by field (PK, UNIQUE, CHECK column- and table-level,
    DEFAULT literal and expression, FK with actions, keyless table, quoted identifiers).
  • ProviderSchemaCollectionsTests — 8 cases over the new collections and the restriction overload.
  • SchemaRpcTests — 3 new cases (FK target/actions/check over gRPC, opt-in script, object type).
  • Full core suite 3190 / 0 failed (1 skipped by design), full server suite 297 / 0 failed.

Notes

A table or column name that contains a space still cannot be created by either CREATE TABLE reader (the
single-file reader matches the name with \w+, the directory reader splits on whitespace); the generator quotes such a
name correctly, and this is recorded in the test that pins the quoted-identifier case.

…LE (fixes #574)

SCDMS could not offer a table designer, an ER diagram or a schema compare: the
client metadata route exposed a constraint's kind but not what a foreign key
referenced, the ADO.NET provider exposed no constraint collection at all, and no
round-trip existed - a generated CREATE TABLE was "approximate" and could
silently drop a constraint.

Engine
- IMetadataProvider.GetConstraints(table) surfaces the constraint list the
  information_schema views and the gRPC RPC already read (TableConstraints):
  primary key, unique, NOT NULL, CHECK (with its column when it belongs to one)
  and FOREIGN KEY with its referenced table/column, the referenced key's
  constraint name and ON UPDATE/ON DELETE in SQL spelling.
- IMetadataProvider.GetCreateTableScript(table) and DataStructures/
  TableDdlGenerator emit a CREATE TABLE that recreates the table from metadata:
  single-column key inline (AUTOINCREMENT included), composite key as a table
  constraint, single-column CHECK inline, uniques, DEFAULT (literal or
  expression), NOT NULL and FOREIGN KEY ... REFERENCES t(c) ON DELETE/ON UPDATE.
  Identifiers are quoted only when the reader would misread them and the
  injected _rowid is never scripted.

Protocol and clients
- Additive proto fields: ConstraintSchema.referenced_table/referenced_columns/
  referenced_constraint/on_update/on_delete, TableSchema.create_table_script and
  TableSchema.type, SchemaRequest.include_create_table_script.
- REST /api/v1/schema returns restConstraintInfo per table and createTableScript
  with ?includeScripts=true; ADO.NET GetSchema gains Constraints, ForeignKeys,
  CheckConstraints and TableScripts collections plus the key/default flags on
  Columns; the .NET client forwards includeCreateTableScript.

Fixed on the way (same DDL, two different tables)
- Single-file storage skipped every table-level CHECK constraint outright.
- Single-file storage ignored ON DELETE / ON UPDATE (and defaulted to NO ACTION
  where directory storage records RESTRICT).
- A quoted identifier inside a FOREIGN KEY definition was stored with its
  quotes, naming a table and columns that do not exist.

Tests: TableDdlGeneratorTests (17, both layouts, every script executed against
fresh metadata and compared field by field), ProviderSchemaCollectionsTests (8)
and three SchemaRpcTests cases. Core suite 3190/0, server suite 297/0.
@codecov

codecov Bot commented Dec 5, 2026 •

Copy link
Copy Markdown

@MPCoreDeveloper

Copy link
Copy Markdown
Owner Author

Stack note (automated session): this is the base of a three-PR stack - #577 (#574, metadata constraints + round-trip DDL) -> #578 (#570, views as catalog objects) -> #579 (#575, enforced gRPC capability contract + the engine-metadata mutation slice). Merge bottom-up. #578/#579 currently target a feature branch, so ci.yml does not run on them (it triggers on PRs to master); retarget them to master after this lands. Local verification on the top of the stack: core suite 3230/0, server suite 301/0, plus the new nightly mutation slice engine-metadata (first run 48.93% over 654 mutants; SqlViewColumns improved to 64.11% after SqlViewColumnsTests).

…x (S6444)

SonarCloud failed the PR gate on new_security_rating=B: csharpsquid:S6444 flagged the static Regex.Replace("\s+") that normalised the ON DELETE/ON UPDATE action. Split on whitespace instead, so the only pattern left is the compiled ReferentialActionRegex, which carries a one-second timeout.
@sonarqubecloud

sonarqubecloud Bot commented Dec 5, 2026

Copy link
Copy Markdown

@MPCoreDeveloper
MPCoreDeveloper merged commit 8c238ec into master Dec 6, 2026
14 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.

1 participant