Repository navigation
feat(schema): expose FK/CHECK constraints and a round-trip CREATE TABLE (#574) - #577
Conversation
…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 Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
|
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.
|



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 schemacompare (it had to mark generated DDL "approximate").
How
IMetadataProvider.GetConstraints(table)publishes the constraint listinformation_schemaand thegRPC RPC already read (
DataStructures/TableConstraints), now including a foreign key's referencedtable/column, the referenced key's constraint name and
ON UPDATE/ON DELETE.IMetadataProvider.GetCreateTableScript(table)builds the statement through the new
DataStructures/TableDdlGeneratorfrom the same records, so a script cannotdescribe a table differently from the metadata.
ConstraintSchema.referenced_table/referenced_columns/referenced_constraint/on_update/on_delete,TableSchema.create_table_script,SchemaRequest.include_create_table_script);REST
/api/v1/schemagainsconstraintsandcreateTableScript(?includeScripts=true); ADO.NETGetSchemagainsConstraints,ForeignKeys,CheckConstraintsandTableScriptscollections plus the key/default flags onColumns;the .NET client forwards the new flag.
Defects fixed on the way
CHECK— the same DDL produced a constraint on directory storageand none on
.scdb.ON DELETE/ON UPDATE, and defaulted toNO ACTIONwhere the directory readerrecords
RESTRICT.FOREIGN KEYwas stored with its quotes ("customers"), naming a table that doesnot exist.
Verification
TableDdlGeneratorTests— 17 cases, directory and single-file: every generated script is executed against a freshdatabase 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).3190 / 0 failed(1 skipped by design), full server suite297 / 0 failed.Notes
A table or column name that contains a space still cannot be created by either
CREATE TABLEreader (thesingle-file reader matches the name with
\w+, the directory reader splits on whitespace); the generator quotes such aname correctly, and this is recorded in the test that pins the quoted-identifier case.