Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/disable triggers #50

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 32 additions & 4 deletions Respawn.DatabaseTests/SqlServerTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Threading.Tasks;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;

Expand Down Expand Up @@ -237,7 +237,7 @@ public async Task ShouldHandleCircularRelationships()
{
await checkpoint.Reset(_connection);
}
catch
catch
{
_output.WriteLine(checkpoint.DeleteSql);
throw;
Expand Down Expand Up @@ -468,8 +468,8 @@ public async Task ShouldReseedIdAccordingToIdentityInitialSeedValue()
[Fact]
public async Task ShouldReseedIdAccordingToIdentityInitialSeedValue_TableHasNeverHadAnyData()
{
_database.Execute("create table Foo ([id] [int] IDENTITY(1001,1), Value int)");
_database.Execute("create table Foo ([id] [int] IDENTITY(1001,1), Value int)");

var checkpoint = new Checkpoint();
checkpoint.WithReseed = true;

Expand All @@ -486,5 +486,33 @@ public async Task ShouldReseedIdAccordingToIdentityInitialSeedValue_TableHasNeve
_database.Insert(new Foo { Value = 0 });
_database.ExecuteScalar<int>("SELECT MAX(id) FROM Foo").ShouldBe(1001);
}

[Fact]
public async Task ShouldDisableTriggers()
{
_database.Execute("create table a (id int primary key)");
_database.Execute("create table b (a_id int)");
_database.Execute("CREATE TRIGGER a_Insert ON a FOR DELETE AS INSERT INTO b (a_id) SELECT Id FROM Deleted");
_database.Execute("insert into a (id) values (1)");

_database.ExecuteScalar<int>("SELECT COUNT(1) FROM a").ShouldBe(1);

var checkpoint = new Checkpoint
{
TablesToIgnore = new[] {"b"}
};
try
{
await checkpoint.Reset(_connection);
}
catch
{
_output.WriteLine(checkpoint.DeleteSql ?? string.Empty);
throw;
}

_database.ExecuteScalar<int>("SELECT COUNT(1) FROM a").ShouldBe(0);
_database.ExecuteScalar<int>("SELECT COUNT(1) FROM b").ShouldBe(0);
}
}
}
2 changes: 2 additions & 0 deletions Respawn/DbAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,9 @@ public string BuildDeleteCommandText(GraphBuilder graph)
}
foreach (var table in graph.ToDelete)
{
builder.AppendLine($"ALTER TABLE {table.GetFullName(QuoteCharacter)} DISABLE TRIGGER ALL;");
builder.AppendLine($"DELETE {table.GetFullName(QuoteCharacter)};");
builder.AppendLine($"ALTER TABLE {table.GetFullName(QuoteCharacter)} ENABLE TRIGGER ALL;");
}
foreach (var table in graph.CyclicalTableRelationships.Select(rel => rel.ParentTable))
{
Expand Down