Skip to content

Script-Migration command should begin/commit transactions #7681

Description

@Zysce

Script-Migration command currently generate creation (or drop) script without checking objects existence.
If an error happens during script deployment (network, server error, ...), you can't redeploy generated script.

A check is done for table __EFMigrationsHistory :

IF OBJECT_ID(N'__EFMigrationsHistory') IS NULL
BEGIN
    CREATE TABLE [__EFMigrationsHistory] (
        [MigrationId] nvarchar(150) NOT NULL,
        [ProductVersion] nvarchar(32) NOT NULL,
        CONSTRAINT [PK___EFMigrationsHistory] PRIMARY KEY ([MigrationId])
    );
END;

GO

It should be the same for user tables, ex of generated creation script :

IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20170217083056_EFEntityInitialCreate')
BEGIN
    CREATE TABLE [SomeTable] (
        [Id] int NOT NULL IDENTITY,
        [Name] nvarchar(max) NOT NULL,
        CONSTRAINT [PK_SomeTable] PRIMARY KEY ([Id])
    );
END;

GO

The modification should be done in creation script (check if not exists) and drop script (check if exists).

Activity

  1. changed the title [-]Script-Migration command should check table existence[/-] [+]Script-Migration command should check objects existence[/+] on Feb 22, 2017
  2. changed the title [-]Script-Migration command should check objects existence[/-] [+]Script-Migration command should check object existence[/+] on Feb 22, 2017
  3. ErikEJ commented on Feb 22, 2017

    @ErikEJ
    Contributor
  4. Zysce commented on Feb 22, 2017

    @Zysce
    Author

    Yes,
    IdemPotent check if __EFMigrationsHistory table contains migration row.
    IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20170217083056_EFSomeTableInitialCreate')

    However, the row with MigrationId = '20170217083056_EFSomeTableInitialCreate' is added at the end of the script.

    If an error occurs on script deployment before the row is inserted, you cannot redeploy the script since the table __EFMigrationsHistory does not contains migration row.

  5. rowanmiller commented on Feb 24, 2017

    @rowanmiller
    Contributor

    When we execute migrations from tools/code, we use transactions for most operations (though there are some DDL operations that can't be executed in a transaction). @bricelam should we be emitting BEGIN TRANSACTION etc. in the script we generate?

  6. bricelam commented on Feb 24, 2017

    @bricelam
    Contributor

    @rowanmiller I think it depends on whether most tools (e.g. MSDeploy, SSMS, SSDT, etc.) give you the option to run it inside a transaction...

  7. ajcvickers commented on Mar 6, 2017

    @ajcvickers
    Contributor

    We discussed this at length and have decided that at this time we will not start generating any transactions code in the script. We want to leave the scripts generated to be useful in a variety of scenarios, some of which don't involve use of transactions, and some of which will do transactions themselves separately from the script. We considered emitting a comment saying this at the start of the script, but for now we don't think there is enough value in doing that. We will re-consider this if there is more feedback and/or specific pit-of-failure scenarios that we see people falling into.

  8. changed the title [-]Script-Migration command should check object existence[/-] [+]Script-Migration command should begin/commit transactions[/+] on Nov 19, 2018
  9. added this to the 3.0.0 milestone on Nov 19, 2018
  10. 35 remaining items

  11. bricelam commented on Jul 28, 2020

    @bricelam
    Contributor

    I looked into MSDeploy (used by Web Publish) and it's safe to include the BEGIN/COMMIT statements in the script. I was worried we might run into nested transaction errors because it automatically wraps the entire script inside a transaction, but it simply ignores the statements. I filed dotnet/sdk#12676 to, once this is implemented, disable the automatic transaction and start using the script's transactions instead.

  12. bricelam commented on Jul 29, 2020

    @bricelam
    Contributor

    Design meeting notes:

    • Start doing this by default
    • Add a --no-transactions option to revert to the previous behavior in the off chance that it breaks existing workflows
  13. added 2 commits that reference this issue on Aug 7, 2020
    eb3ae48
    2bdf741
  14. modified the milestones: 5.0.0, on Aug 14, 2020
  15. woeterman94 commented on Aug 17, 2020

    @woeterman94

    Any update on this?

  16. bricelam commented on Aug 17, 2020

    @bricelam
    Contributor

    @woeterman94 Will be available in 5.0.0-rc.1

  17. modified the milestones: , 5.0.0 on Nov 7, 2020
  18. ErroneousFatality commented on May 12, 2023

    @ErroneousFatality

    @bricelam I have an issue with this.
    I'm using YugabyteDB (through the npgsql framework), which doesn't support DDL code inside transactions.
    I'm using the DbContext.Database.MigrateAsync method in code to run my migrations through my application for migrating and seeding our databases.
    There's no parameter to tell the Migrate method to not use transactions.

    I opened a feature request issue about it here.

  19. added theissue type on Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions