fix: scope index/constraint drop macros to the model's schema - #774
Merged
axellpadilla merged 1 commit intoJul 27, 2026
Merged
Conversation
drop_xml_indexes(), drop_spatial_indexes(), drop_fk_constraints() and drop_pk_constraints() matched tables by name alone, so a post-hook calling drop_all_indexes_on_table() dropped the primary key, inbound foreign keys and XML/spatial indexes of every same-named table in other schemas of the same database. Only the trailing remaining-indexes statement filtered by schema. All four now filter on the model's schema. In drop_fk_constraints() the filter applies to the referenced table (the model); the constraint is still dropped from the referencing parent table, which may live in another schema. Identifiers in the generated dynamic SQL are wrapped in QUOTENAME(), using REPLACE() for composite schema.name literals since QUOTENAME() returns NULL above 128 input characters and would silently drop rows from the FOR XML aggregation.
joshmarkovic
marked this pull request as ready for review
July 27, 2026 15:28
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.
Problem
drop_all_indexes_on_table()and the macros it calls matched tables by name only, with no schema predicate:drop_xml_indexes()drop_spatial_indexes()drop_fk_constraints()drop_pk_constraints()Only the trailing remaining-indexes statement filtered by schema. These are public macros users call from post-hooks.
Impact
Given
staging.ordersandmarts.ordersin one database, a post-hook on either model dropped the other's primary key, inbound foreign keys, and XML/spatial indexes.Changes
drop_fk_constraints()the filter applies to the referenced table (the model). The constraint is still dropped from the referencing parent table, which may legitimately live in another schema. Comment added, since this asymmetry is easy to "fix" wrongly later.QUOTENAME().schema.nameliterals useREPLACE()instead of nestedQUOTENAME(), becauseQUOTENAME()returnsNULLabove 128 input characters and would silently drop rows from theFOR XMLaggregation.Testing
Extended
TestIndexDropsOnlySchemawith a PK and an inbound FK in the sibling schema. Verified against SQL Server 2022 that the test fails without the macro change:assert 1 == 2(PK's clustered index gone)assert 0 == 1(inbound FK gone)Not addressed
These macros aggregate with
for xml path(''), which XML-escapes&,<and>. An identifier containing those still produces broken SQL. That is a separate mechanism (fix would be, type).value('.', 'nvarchar(max)')) and is left out of scope here.