Skip to content

Spike D: SqlOperations in idempotent scripts and bundles #19

Closed

Description

@CaffeinatedCoder

Before preview.2 (plan, section 6).

Question

Every new object is a design-time SqlOperation. Does CREATE INDEX CONCURRENTLY (transaction suppressed) and a trigger survive migrations script --idempotent, applied twice, and a bundle, on PostgreSQL and SQL Server? EF wraps idempotent statements in a transaction-bound block on PostgreSQL and in IF NOT EXISTS … BEGIN … END on SQL Server, where CREATE TRIGGER must start its batch.

It fails when

The provider cannot wrap the statement. Then that combination is refused at scaffold with the declaration named, and the docs say so.

Done when

Each combination is either applied twice on a live server or refused loudly; none is emitted and broken.

Measured

2026-11-16, spikes/D.ScriptsAndBundles (its README holds how each cell is measured), EF Core 11.0.0-rc.3, Npgsql EF 11.0.0-rc.3. Twice: on a developer's machine against PostgreSQL 16.15 and 18.6 and SQL Server 2022 (16.0.4305.5), and in CI against PostgreSQL 16.16 and 18.7 and SQL Server 2022 (16.0.4325.4). Every cell answered the same in both places. The statements come from a contributor written against the kernel's public contract, so they reach the migration through SchemaKit's planner and differ. Every cell is judged by the catalog: an index has to be valid, a trigger has to refuse the row it must refuse. A script is applied as psql and sqlcmd apply one, and psql (16.15, 18.6) and sqlcmd (18.7) themselves left the same object, table and history. "The same identities" is by an identity a recreation changes, shown changing for each kind.

A bundle carries the statements of the question. A script does not carry two of them. One form the spike added fails in a bundle too: the trigger inside EXEC(…) with its line breaks, written through the contract's Literal, is not valid T-SQL and fails on every path (its rows are in the table).

"Applies": no error, the object is in the catalog and works, the migration is recorded. "Nothing changed": no error, and the catalog holds the same objects under the same identities.

Server Statement Path First run Second run Error
PostgreSQL 16 and 18 CREATE INDEX CONCURRENTLY, transaction suppressed migrations script, applied once Applies not applicable
migrations script --idempotent, applied twice Error, and psql goes on and exits 0: the table is there, the index is missing, the migration is recorded as applied No error, nothing changed: the index stays missing 25001: CREATE INDEX CONCURRENTLY cannot be executed from a function
the same with psql -v ON_ERROR_STOP=1 Error (exit 3): the table is there, the index is missing, no migration is recorded Error, nothing changed 25001, as above; second run: 42P07: relation "Things" already exists
migrations bundle, run twice Applies Nothing changed
CREATE FUNCTION with a body, and CREATE TRIGGER migrations script, applied once Applies not applicable
migrations script --idempotent, applied twice Applies Nothing changed
migrations bundle, run twice Applies Nothing changed
SQL Server 2022 ALTER DATABASE CURRENT SET ALLOW_SNAPSHOT_ISOLATION ON, transaction suppressed migrations script, applied once Applies not applicable
migrations script --idempotent, applied twice Applies Nothing changed
migrations bundle, run twice Applies Nothing changed
The same, the transaction not suppressed: the control migrations script, applied once Error, and the batch goes on (sqlcmd exits 0): the table is there, the option is off, the migration is recorded as applied not applicable 226: ALTER DATABASE statement not allowed within multi-statement transaction.
migrations script --idempotent, applied twice Error, and the batch goes on: the same No error, nothing changed: the option stays off 226, as above
the same with sqlcmd -b The same; sqlcmd exits 1 after the batch has committed No error, nothing changed 226, as above
migrations bundle, run twice Error (the bundle exits 1): no table, the option is off, no migration recorded The same error, nothing changed 226, as above
CREATE INDEX … WITH (ONLINE = ON), transaction suppressed migrations script, applied once Applies not applicable
migrations script --idempotent, applied twice Applies Nothing changed
migrations bundle, run twice Applies Nothing changed
CREATE TRIGGER migrations script, applied once Error (sqlcmd exits 0): no table, no trigger, no migration recorded not applicable 111: 'CREATE TRIGGER' must be the first statement in a query batch.
migrations script --idempotent, applied twice Error (sqlcmd exits 0): the same The same error, nothing changed 156: Incorrect syntax near the keyword 'TRIGGER'.
the same with sqlcmd -b Error (exit 1): the same The same error, nothing changed 156, as above
migrations bundle, run twice Applies Nothing changed
EXEC(N'CREATE TRIGGER …'), on one line migrations script, applied once Applies not applicable
migrations script --idempotent, applied twice Applies Nothing changed
migrations bundle, run twice Applies Nothing changed
EXEC(…) of the trigger with its line breaks, the constant written by the dialect's Literal migrations script, applied once Error: no table, no trigger, no migration recorded not applicable 102: Incorrect syntax near 'CONCAT'.
migrations script --idempotent, applied twice Error: the same The same error, nothing changed 102, as above
migrations bundle, run twice Error (the bundle exits 1): the same The same error, nothing changed 102, as above
EXEC(N'CREATE TRIGGER …'), the line breaks inside the constant, quotes doubled by hand migrations script, applied once Applies not applicable
migrations script --idempotent, applied twice Applies Nothing changed
migrations bundle, run twice Applies Nothing changed

On SQL Server it is ALTER DATABASE that says whether the suppression arrives: the server refuses it inside a transaction (Msg 226), so it applies only where EF Core ends the transaction first, and the control without the suppression fails on every path. The ONLINE index does not say it: SQL Server builds one inside a transaction too.

A plain CREATE INDEX, the control, applies on every path on all three servers. The exclusion constraint is in the comment below and on #34.

The combinations to refuse

By this issue's rule. The spike builds no refusal.

  1. PostgreSQL, a statement with the transaction suppressed, in migrations script --idempotent. EF Core wraps it in a DO block, where PostgreSQL refuses it. Under psql's defaults the migration ends up recorded without its index. Scaffold does not know how a migration will be applied; the generator is handed MigrationsSqlGenerationOptions.Idempotent. An idempotent script cannot carry a statement with the transaction suppressed on PostgreSQL: the migration is recorded without its index #155.
  2. SQL Server, CREATE TRIGGER as a statement, in migrations script with --idempotent and without. EF Core 11 writes a migration's statements into one batch (11.0.0 writes the same scripts as rc.3). Inside EXEC(N'…') the trigger survives every path, which the contract can write for a statement on one line only. A trigger on SQL Server has to be written inside EXEC, and the contract cannot write a statement with line breaks into a string constant #156.

Corrected on 2026-11-16 after an independent analysis of the PR: no cell above changed. The ALTER DATABASE rows are new, the headline no longer says "every statement", and the harness now fails where it used to pass over a database it could not create.

Not measured: Down, a migration that fails halfway on a bundle, DROP INDEX CONCURRENTLY and REINDEX CONCURRENTLY, several migrations in one script, --no-transactions, an edition of SQL Server without ONLINE = ON.

Activity

  1. added this to the 11.0.0 milestone on Nov 11, 2026
  2. added
    tests
    package/postgresql
    package/sqlserver
    area/migrations
    on Nov 11, 2026
  3. CaffeinatedCoder commented on Nov 14, 2026

    @CaffeinatedCoder
    OwnerAuthor

    Measured on the way, pre-preview sweep of main @ 948496a, 2026-11-14: for an exclusion constraint, from a packed consumer outside the repository, migrations script --idempotent applied twice and a bundle both leave the constraint in the catalog and refuse the overlapping row, on PostgreSQL 16 and 18 (the table on #34). That is the transaction-bound case. The spike's own question, a statement that suppresses the transaction and a trigger, on PostgreSQL and SQL Server, is still open.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/migrationspackage/postgresqlpackage/sqlservertests

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions