08ef94bb21
`dotnet ef migrations script --idempotent` from migration 0 stopped at AddSiteIdToDataConnections (Msg 207 'SiteId') and, past it, hit Msg 156/178/137 on MigrateParametersToJsonSchema's CREATE FUNCTIONs and Msg 207 on the ExecutionId / ParentExecutionId filtered indexes. Same root cause as #126: the script compiles a whole migration as one batch behind IF ... BEGIN guards. (The Msg 1911 'Category' / later @legacy errors in the issue were cascades of those failures and disappear once they are fixed.) - New MigrationSql.Deferred(sql): wraps a body in EXEC(N'...') with quotes doubled, so it compiles at execution time as its own batch. Migrate() runs the identical text. - Deferred: AddSiteIdToDataConnections step 3 (data move + cursor, DECLAREs kept in the same body); MigrateParametersToJsonSchema's three CREATE FUNCTIONs and the UPDATE that calls them; AddAuditLogExecutionId / AddAuditLogParentExecutionId filtered partition-aligned indexes. No model/snapshot change. - IdempotentMigrationScriptTests: new IdempotentScript_FromEmpty_RunsClean_AndMatchesMigrate runs the full script on an empty DB batch by batch, re-runs it as a no-op, and compares a schema snapshot (columns, defaults, indexes incl. filter + data space, FKs, checks, modules, principals, permissions, partitioning, per-table row count + checksum) against a Migrate()-built DB. Fails on the unfixed migrations with Msg 207 'SiteId'. Batch failures now name the migration. Schema equivalence: Migrate() before vs after this change, and the fixed from-empty script, all produce byte-identical snapshots (939 lines); the 57->head script still runs clean and lands on the same snapshot; the seeded data path through AddSiteIdToDataConnections + MigrateParametersToJsonSchema gives identical rows via the fixed script and the unfixed Migrate(). Co-Authored-By: Claude <noreply@anthropic.com>