Skip to content

Commit

Permalink
Ensure AUTOCOMMIT is set to 1 where needed. (#1744)
Browse files Browse the repository at this point in the history
  • Loading branch information
lauxjpn committed Jan 16, 2023
1 parent e837789 commit 4ad5d4d
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 9 deletions.
4 changes: 4 additions & 0 deletions src/EFCore.MySql/Update/Internal/MySqlUpdateSqlGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Pomelo Foundation. All rights reserved.
// Licensed under the MIT. See LICENSE in the project root for license information.

using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
Expand Down Expand Up @@ -344,6 +345,9 @@ protected override bool IsIdentityOperation(IColumnModification modification)
return isIdentityOperation;
}

public override void PrependEnsureAutocommit(StringBuilder commandStringBuilder)
=> commandStringBuilder.Insert(0, $"SET AUTOCOMMIT = 1{SqlGenerationHelper.StatementTerminator}{Environment.NewLine}");

private static (string tableName, string schema) GetTableNameAndSchema(IColumnModification modification, IProperty property)
{
if (modification.Column?.Table is { } table)
Expand Down
27 changes: 19 additions & 8 deletions test/EFCore.MySql.FunctionalTests/DataAnnotationMySqlTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,33 +114,43 @@ public override void ConcurrencyCheckAttribute_throws_if_value_in_database_chang
base.ConcurrencyCheckAttribute_throws_if_value_in_database_changed();

AssertSql(
@"SELECT `s`.`Unique_No`, `s`.`MaxLengthProperty`, `s`.`Name`, `s`.`RowVersion`, `s`.`AdditionalDetails_Name`, `s`.`AdditionalDetails_Value`, `s`.`Details_Name`, `s`.`Details_Value`
"""
SELECT `s`.`Unique_No`, `s`.`MaxLengthProperty`, `s`.`Name`, `s`.`RowVersion`, `s`.`AdditionalDetails_Name`, `s`.`AdditionalDetails_Value`, `s`.`Details_Name`, `s`.`Details_Value`
FROM `Sample` AS `s`
WHERE `s`.`Unique_No` = 1
LIMIT 1",
LIMIT 1
""",
//
@"SELECT `s`.`Unique_No`, `s`.`MaxLengthProperty`, `s`.`Name`, `s`.`RowVersion`, `s`.`AdditionalDetails_Name`, `s`.`AdditionalDetails_Value`, `s`.`Details_Name`, `s`.`Details_Value`
"""
SELECT `s`.`Unique_No`, `s`.`MaxLengthProperty`, `s`.`Name`, `s`.`RowVersion`, `s`.`AdditionalDetails_Name`, `s`.`AdditionalDetails_Value`, `s`.`Details_Name`, `s`.`Details_Value`
FROM `Sample` AS `s`
WHERE `s`.`Unique_No` = 1
LIMIT 1",
LIMIT 1
""",
//
@"@p2='1'
"""
@p2='1'
@p0='ModifiedData' (Nullable = false) (Size = 4000)
@p1='00000000-0000-0000-0003-000000000001'
@p3='00000001-0000-0000-0000-000000000001'

SET AUTOCOMMIT = 1;
UPDATE `Sample` SET `Name` = @p0, `RowVersion` = @p1
WHERE `Unique_No` = @p2 AND `RowVersion` = @p3;
SELECT ROW_COUNT();",
SELECT ROW_COUNT();
""",
//
@"@p2='1'
"""
@p2='1'
@p0='ChangedData' (Nullable = false) (Size = 4000)
@p1='00000000-0000-0000-0002-000000000001'
@p3='00000001-0000-0000-0000-000000000001'

SET AUTOCOMMIT = 1;
UPDATE `Sample` SET `Name` = @p0, `RowVersion` = @p1
WHERE `Unique_No` = @p2 AND `RowVersion` = @p3;
SELECT ROW_COUNT();");
SELECT ROW_COUNT();
""");
}

public override void DatabaseGeneratedAttribute_autogenerates_values_when_set_to_identity()
Expand All @@ -159,6 +169,7 @@ public override void DatabaseGeneratedAttribute_autogenerates_values_when_set_to
@p5='Third Name' (Size = 4000)
@p6='0' (Nullable = true)

SET AUTOCOMMIT = 1;
INSERT INTO `Sample` (`MaxLengthProperty`, `Name`, `RowVersion`, `AdditionalDetails_Name`, `AdditionalDetails_Value`, `Details_Name`, `Details_Value`)
VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6)
RETURNING `Unique_No`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public override void Input_query_escapes_parameter()
AssertSql(
@"@p0='Back\slash's Garden Party' (Nullable = false) (Size = 4000)
SET AUTOCOMMIT = 1;
INSERT INTO `Artists` (`Name`)
VALUES (@p0)
RETURNING `ArtistId`;",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public override void Input_query_escapes_parameter()
AssertSql(
@"@p0='Back\slash's Garden Party' (Nullable = false) (Size = 4000)
SET AUTOCOMMIT = 1;
INSERT INTO `Artists` (`Name`)
VALUES (@p0)
RETURNING `ArtistId`;",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ public override async Task Principal_and_dependent_roundtrips_with_cycle_breakin
if (AppConfig.ServerVersion.Supports.Returning)
{
AssertSql(
"""
"""
@p0='AC South' (Size = 4000)

SET AUTOCOMMIT = 1;
INSERT INTO `AuthorsClub` (`Name`)
VALUES (@p0)
RETURNING `Id`;
Expand All @@ -27,6 +28,7 @@ public override async Task Principal_and_dependent_roundtrips_with_cycle_breakin
@p1='1'
@p2='Alice' (Size = 4000)

SET AUTOCOMMIT = 1;
INSERT INTO `Author` (`AuthorsClubId`, `Name`)
VALUES (@p1, @p2)
RETURNING `Id`;
Expand All @@ -36,6 +38,7 @@ public override async Task Principal_and_dependent_roundtrips_with_cycle_breakin
@p3='1'
@p4=NULL (Size = 4000)

SET AUTOCOMMIT = 1;
INSERT INTO `Book` (`AuthorId`, `Title`)
VALUES (@p3, @p4)
RETURNING `Id`;
Expand All @@ -51,6 +54,7 @@ LIMIT 2
"""
@p0='AC North' (Size = 4000)

SET AUTOCOMMIT = 1;
INSERT INTO `AuthorsClub` (`Name`)
VALUES (@p0)
RETURNING `Id`;
Expand All @@ -60,6 +64,7 @@ LIMIT 2
@p1='2'
@p2='Author of the year 2023' (Size = 4000)

SET AUTOCOMMIT = 1;
INSERT INTO `Author` (`AuthorsClubId`, `Name`)
VALUES (@p1, @p2)
RETURNING `Id`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public override async Task Add_with_generated_values(bool async)
"""
@p0='1000'

SET AUTOCOMMIT = 1;
INSERT INTO `WithSomeDatabaseGenerated` (`Data2`)
VALUES (@p0)
RETURNING `Id`, `Data1`;
Expand Down Expand Up @@ -91,6 +92,7 @@ public override async Task Add_with_no_generated_values(bool async)
@p1='1000'
@p2='1000'

SET AUTOCOMMIT = 1;
INSERT INTO `WithNoDatabaseGenerated` (`Id`, `Data1`, `Data2`)
VALUES (@p0, @p1, @p2);
""");
Expand All @@ -104,6 +106,7 @@ public override async Task Add_with_all_generated_values(bool async)
{
AssertSql(
"""
SET AUTOCOMMIT = 1;
INSERT INTO `WithAllDatabaseGenerated` ()
VALUES ()
RETURNING `Id`, `Data1`, `Data2`;
Expand Down Expand Up @@ -149,6 +152,7 @@ public override async Task Modify_with_no_generated_values(bool async)
@p0='1000'
@p1='1000'

SET AUTOCOMMIT = 1;
UPDATE `WithNoDatabaseGenerated` SET `Data1` = @p0, `Data2` = @p1
WHERE `Id` = @p2;
SELECT ROW_COUNT();
Expand All @@ -165,6 +169,7 @@ public override async Task Delete(bool async)
"""
@p0='1'

SET AUTOCOMMIT = 1;
DELETE FROM `WithSomeDatabaseGenerated`
WHERE `Id` = @p0
RETURNING 1;
Expand All @@ -176,6 +181,7 @@ public override async Task Delete(bool async)
"""
@p0='1'

SET AUTOCOMMIT = 1;
DELETE FROM `WithSomeDatabaseGenerated`
WHERE `Id` = @p0;
SELECT ROW_COUNT();
Expand Down Expand Up @@ -241,6 +247,7 @@ public override async Task Add_Add_with_same_entity_type_and_no_generated_values
@p4='1001'
@p5='1001'

SET AUTOCOMMIT = 1;
INSERT INTO `WithNoDatabaseGenerated` (`Id`, `Data1`, `Data2`)
VALUES (@p0, @p1, @p2),
(@p3, @p4, @p5);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,7 @@ public override async Task Tpt_mixed_sproc_and_non_sproc(bool async)
@p2='1'
@p3='8'

SET AUTOCOMMIT = 1;
INSERT INTO `Child1` (`Id`, `Child1Property`)
VALUES (@p2, @p3);
""");
Expand Down

0 comments on commit 4ad5d4d

Please sign in to comment.