Skip to content

SQL Server migrations should use QUOTENAME when constructing SQL for EXEC - #36271

Merged
roji merged 1 commit into
dotnet:mainfrom
duncansmart:efcore-issue-36264
Jun 23, 2025
Merged

roji merged 1 commit into
dotnet:mainfrom
duncansmart:efcore-issue-36264

Conversation

@duncansmart

Copy link
Copy Markdown
Contributor

Fixes #36264

@duncansmart
duncansmart requested a review from a team as a code owner June 20, 2025 08:30
@duncansmart
duncansmart force-pushed the efcore-issue-36264 branch 2 times, most recently from 6e4f2ed to b7567fc Compare June 20, 2025 09:58
@duncansmart

Copy link
Copy Markdown
Contributor Author

It also makes me realise that sysname is an inappropriate for the variables in DECLARE @var sysname = QUOTENAME(SCHEMA_NAME()) because QUOTENAME returns nvarchar(258). I'll change where appropriate tonvarchar(max) (as used already in AlterDatabaseOperation).

…EXEC

* Refactor `SqlServerMigrationsSqlGenerator.cs` to use `QUOTENAME` when generating dynamic SQL passed to EXEC, improving safety and correctness in SQL command generation.
* Update related tests to ensure expected SQL output aligns with these changes.

Fixes dotnet#36264
@roji

roji commented Jun 22, 2025

Copy link
Copy Markdown
Member

@duncansmart just a remind that if your PR is ready for another review, please ask for one explicitly ("re-request review" button in top-right corner, under "Reviewers").

@duncansmart
duncansmart requested a review from roji June 22, 2025 20:31

@roji roji left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @duncansmart!

@roji
roji enabled auto-merge (squash) June 23, 2025 06:44
@roji
roji merged commit 4f44953 into dotnet:main Jun 23, 2025
cincuranet added a commit to cincuranet/efcore that referenced this pull request Jun 26, 2025
cincuranet added a commit that referenced this pull request Jun 26, 2025
m-x-shokhzod added a commit to m-x-shokhzod/efcore that referenced this pull request May 31, 2026
Addresses Copilot review on dotnet#38250: the variable was changed from
nvarchar(max) to sysname, but the SELECT still stores QUOTENAME(...)
whose output can reach 258 chars and would silently truncate inside a
128-char sysname, producing invalid DROP CONSTRAINT SQL for long names.

The variable-type change was never part of issue dotnet#34620, which only asks
to replace the sys.default_constraints JOIN with a single-table lookup
via OBJECT_NAME([c].[default_object_id]). QUOTENAME is required here by
dotnet#36271 (use QUOTENAME when constructing SQL for EXEC), so the matching
nvarchar(max) must stay. Revert the variable to nvarchar(max) and keep
only the lookup simplification.

Restore the nvarchar(max) baseline in all affected test expectations.
m-x-shokhzod added a commit to m-x-shokhzod/efcore that referenced this pull request Jun 9, 2026
Replace the JOIN against [sys].[default_constraints] + [sys].[columns]
with a single-table lookup on [sys].[columns] via
OBJECT_NAME([c].[default_object_id]). One sys-table, no JOIN; behavior is
preserved (OBJECT_NAME returns NULL when there is no default constraint,
so the existing IF @var IS NOT NULL guard still skips the drop).

The nvarchar(max) variable and QUOTENAME wrapping are kept: QUOTENAME's
output can exceed sysname's 128 chars, and QUOTENAME is required when
constructing the dynamic SQL passed to EXEC (dotnet#36271).

Fixes dotnet#34620
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SQL Server migrations should use QUOTENAME when constructing SQL for EXEC

3 participants