From 5fc0cd9871202b2fc4d3a0e927c0a87e35d51961 Mon Sep 17 00:00:00 2001 From: Carl Berg Date: Wed, 10 Mar 2021 13:50:51 +0100 Subject: [PATCH] Added handling of 'text' data type + clearing instructions after go --- .../Scripts/Migrations/1.0.0/01.Schema.sql | 6 ++++ DataDude.Tests/Inserts/InstructionTests.cs | 33 +++++++++++++++++++ DataDude/DataDude.csproj | 2 +- DataDude/DataDudeContext.cs | 1 + DataDude/Dude.cs | 2 ++ .../ValueProviders/StringValueProvider.cs | 2 +- 6 files changed, 44 insertions(+), 2 deletions(-) diff --git a/DataDude.Tests/Core/Scripts/Migrations/1.0.0/01.Schema.sql b/DataDude.Tests/Core/Scripts/Migrations/1.0.0/01.Schema.sql index 9b2390e..ff5b4c8 100644 --- a/DataDude.Tests/Core/Scripts/Migrations/1.0.0/01.Schema.sql +++ b/DataDude.Tests/Core/Scripts/Migrations/1.0.0/01.Schema.sql @@ -76,6 +76,12 @@ CREATE TABLE Test_Nullable_FK( ) GO +CREATE TABLE Test_Nullable_Text_Data_Type( + Id INT PRIMARY KEY, + Text TEXT NULL, +) +GO + CREATE TRIGGER People.EmployeeUpdatedAt ON People.Employee AFTER UPDATE AS BEGIN UPDATE People.Employee SET People.Employee.UpdatedAt = GETDATE() diff --git a/DataDude.Tests/Inserts/InstructionTests.cs b/DataDude.Tests/Inserts/InstructionTests.cs index 884eeaa..a679a95 100644 --- a/DataDude.Tests/Inserts/InstructionTests.cs +++ b/DataDude.Tests/Inserts/InstructionTests.cs @@ -279,5 +279,38 @@ public async Task Can_Honor_Specified_Null_Values_When_Inserting_Nullable_FK() firstOfficeId.ShouldBe(1); secondOfficeId.ShouldBeNull(); } + + [Fact] + public async Task Can_Handle_Nullable_Text_Data_Type() + { + using var connection = Fixture.CreateNewConnection(); + + await new Dude() + .Insert("Test_Nullable_Text_Data_Type") + .Go(connection); + + var insertedItems = await connection.QueryAsync<(int, string)>("SELECT Id, Text FROM Test_Nullable_Text_Data_Type"); + insertedItems.ShouldNotBeEmpty(); + } + + [Fact] + public async Task Instructions_Are_Cleared_After_Go() + { + using var connection = Fixture.CreateNewConnection(); + + var dude = new Dude(); + await dude + .Insert("Office", new { Name = "test" }) + .Go(connection); + + await dude + .Insert("Employee", new { Name = "test" }) + .Go(connection); + + var insertedOffices = await connection.QuerySingleAsync("SELECT COUNT(1) FROM Buildings.Office"); + var insertedEmployees = await connection.QuerySingleAsync("SELECT COUNT(1) FROM People.Employee"); + insertedOffices.ShouldBe(1); + insertedEmployees.ShouldBe(1); + } } } diff --git a/DataDude/DataDude.csproj b/DataDude/DataDude.csproj index cf23e95..77cd6a2 100644 --- a/DataDude/DataDude.csproj +++ b/DataDude/DataDude.csproj @@ -4,7 +4,7 @@ netstandard2.0 9.0 enable - 0.6.0 + 0.7.0 true true snupkg diff --git a/DataDude/DataDudeContext.cs b/DataDude/DataDudeContext.cs index 8947c8e..6971633 100644 --- a/DataDude/DataDudeContext.cs +++ b/DataDude/DataDudeContext.cs @@ -44,6 +44,7 @@ public DataDudeContext(ISchemaLoader schemaLoader) ["varbinary"] = DbType.Binary, ["varchar"] = DbType.String, ["nvarchar"] = DbType.String, + ["text"] = DbType.String, ["geography"] = DbType.String, ["timestamp"] = DbType.Binary, }; diff --git a/DataDude/Dude.cs b/DataDude/Dude.cs index 4af16ed..23cd7ca 100644 --- a/DataDude/Dude.cs +++ b/DataDude/Dude.cs @@ -53,6 +53,8 @@ public async Task Go(IDbConnection connection, IDbTransaction? transaction = nul throw new HandlerException($"Instruction of type '{instruction.GetType()}' lacks handler and cannot be processed"); } } + + Context.Instructions.Clear(); } } } diff --git a/DataDude/Instructions/Insert/ValueProviders/StringValueProvider.cs b/DataDude/Instructions/Insert/ValueProviders/StringValueProvider.cs index 04e7f11..cc36c94 100644 --- a/DataDude/Instructions/Insert/ValueProviders/StringValueProvider.cs +++ b/DataDude/Instructions/Insert/ValueProviders/StringValueProvider.cs @@ -6,7 +6,7 @@ public class StringValueProvider : ValueProvider { protected override ColumnValue? GetDefaultValue(ColumnInformation column, ColumnValue value) { - if (column.DataType is "varchar" or "nvarchar") + if (column.DataType is "varchar" or "nvarchar" or "text") { return new ColumnValue(string.Empty); }