You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Script-Migration command should begin/commit transactions #7681
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).
Reacted by Jon Sagara, Victor Esquino and Brice Lambson
changed the title [-]Script-Migration command should check table existence[/-][+]Script-Migration command should check objects existence[/+]on Feb 22, 2017
changed the title [-]Script-Migration command should check objects existence[/-][+]Script-Migration command should check object existence[/+]on Feb 22, 2017
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.
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?
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.
changed the title [-]Script-Migration command should check object existence[/-][+]Script-Migration command should begin/commit transactions[/+]on Nov 19, 2018
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.
@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.
Activity
[-]Script-Migration command should check table existence[/-][+]Script-Migration command should check objects existence[/+][-]Script-Migration command should check objects existence[/-][+]Script-Migration command should check object existence[/+]ErikEJ commentedon Feb 22, 2017
Did you use the
command swith?
https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/powershell
Zysce commentedon Feb 22, 2017
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.
rowanmiller commentedon Feb 24, 2017
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 TRANSACTIONetc. in the script we generate?bricelam commentedon Feb 24, 2017
@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...
ajcvickers commentedon Mar 6, 2017
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.
[-]Script-Migration command should check object existence[/-][+]Script-Migration command should begin/commit transactions[/+]35 remaining items
bricelam commentedon Jul 28, 2020
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.
bricelam commentedon Jul 29, 2020
Design meeting notes:
--no-transactionsoption to revert to the previous behavior in the off chance that it breaks existing workflowsMigrations: Generate transaction statements in SQL script
Migrations: Generate transaction statements in SQL script
woeterman94 commentedon Aug 17, 2020
Any update on this?
bricelam commentedon Aug 17, 2020
@woeterman94 Will be available in 5.0.0-rc.1
ErroneousFatality commentedon May 12, 2023
@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.MigrateAsyncmethod 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.