feat(sqlserver): composite FK aggregation via STRING_AGG / FOR XML PATH (#146)#214
Open
saurabh500 wants to merge 1 commit into
Open
Conversation
…TH (TabularisDB#146) Aggregate composite foreign-key columns into a single row per constraint. Dispatch STRING_AGG on SQL Server 2017+ and STUFF/FOR XML PATH on 2012-2016 via a runtime SERVERPROPERTY('ProductVersion') probe with @@Version fallback. ForeignKey gains columns / ref_columns / ref_schema (serde-default for backward-compatible IPC) plus local_columns / referenced_columns helpers that fall back to the legacy single-column fields. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
Author
|
@debba this is ready for review |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #146.
What
Aggregate composite foreign-key columns into a single result row per constraint, instead of one row per column. Adds
STRING_AGG (WITHIN GROUP ...)for SQL Server 2017+ and aSTUFF(... FOR XML PATH(''), ...)fallback for 2012-2016, dispatched at runtime via a newdetect_server_versionprobe againstSERVERPROPERTY('ProductVersion')(with@@VERSIONand aDEFAULT_MAJOR = 14fallback).Changes
models.rs—ForeignKeygains 3#[serde(default)]fields (columns: Vec<String>,ref_columns: Vec<String>,ref_schema: Option<String>), pluslocal_columns()/referenced_columns()helpers that fall back to the legacy single-column fields. Old persisted JSON deserializes unchanged.drivers/sqlserver/introspection.rsQ_GET_FOREIGN_KEYS/Q_GET_ALL_FOREIGN_KEYS_BATCHwith_STRING_AGG/_XML_PATHvariants. Both branches emit the same row shape:name, ref_schema, ref_table, columns, ref_columns, on_update, on_delete(+ table_namefor the batch variants). Cascade actions are normalised in SQL viaCASEoversys.foreign_keys.update_referential_actionso the output matches the INFORMATION_SCHEMA form the rest of the codebase already expects (NO ACTION, notNO_ACTION).build_foreign_keysignature now takes column arrays; legacycolumn_name/ref_columnare derived from the first array element so downstream consumers (FK list view, ER diagram) keep working before they migrate.get_foreign_keys/get_all_foreign_keys_batchdetect the server version once per call, pick the right query, and parse the comma-aggregated columns via a newsplit_agg_columnshelper.detect_server_versionasync helper reuses the existingparse_major_version/parse_version_banner/DEFAULT_MAJORfromversion.rs.drivers/{sqlite,mysql,postgres}/mod.rs— 6 FK construction sites get..Default::default()so new fields stay at their default.Tests
_STRING_AGGand_XML_PATHvariants and both single-table and batch flavours (assertsSTRING_AGG WITHIN GROUP,STUFF(... FOR XML PATH('')), deterministicORDER BY fkc.constraint_column_id, normalised action strings).build_foreign_keytests covering: composite columns, empty arrays, missing actions.split_agg_columnsparser tests.ForeignKey::local_columns()/referenced_columns()fallback test for drivers that haven't migrated.columns/ref_columns/ref_schema) — verifies serde defaults still kick in.cargo test --lib drivers::sqlserver— 103 passed, 0 failed.cargo test --lib— 608 passed, 2 pre-existing flakyupdater::testsfailures (Windows flatpak/snap detection) unrelated to this PR.