Skip to content

Commit 35930a9

Browse files
Add the SqlServerDatabase.ClearData() method.
1 parent 683c435 commit 35930a9

File tree

2 files changed

+48
-29
lines changed

2 files changed

+48
-29
lines changed

src/Directory.Build.props

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212
<PackageLicenseExpression>MIT</PackageLicenseExpression>
1313
<PackageReleaseNotes>
1414
2.2.0
15+
- Add SqlServerDatabase.ClearDataAsync() method.
1516
- Add SqlServer.CreateDatabase() method to create database from an Entity Framework DbContext.
1617
- Add SqlServer.CreateEmptyDatabaseAsync() method.
1718
- Add SqlServer.DeleteDatabaseAsync() method.
18-
- Add SqlServer.ExecuteScriptAsync() method.
19-
- Add SqlServer.InsertIntoAsync() method.
19+
- Add SqlServerDatabase.ExecuteScriptAsync() method.
20+
- Add SqlServerDatabase.InsertIntoAsync() method.
2021

2122
2.1.0
2223
- PosInformatique.Testing.Databases.SqlServer target the .NET Standard 2.0 platform.

src/Testing.Databases.SqlServer/SqlServerDatabaseExtensions.cs

+45-27
Original file line numberDiff line numberDiff line change
@@ -100,34 +100,24 @@ public static Task<int> InsertIntoAsync<T>(this SqlServerDatabase database, stri
100100
/// <param name="database">SQL Server database which the data have to be deleted.</param>
101101
public static void ClearAllData(this SqlServerDatabase database)
102102
{
103-
database.ExecuteNonQuery("EXEC sp_msforeachtable 'ALTER TABLE ? NOCHECK CONSTRAINT all'");
104-
105-
database.ExecuteNonQuery("EXEC sp_msforeachtable 'SET QUOTED_IDENTIFIER ON; DELETE FROM ?'");
106-
107-
// Re-initialize the seed of the IDENTITY columns.
108-
// For each table which contains an IDENTITY column, execute the following SQL statement:
109-
// DBCC CHECKIDENT ('[<schema>].[<table>]', RESEED, <seed>)
110-
database.ExecuteNonQuery(@"
111-
DECLARE @sqlcmd VARCHAR(MAX);
112-
113-
SET @sqlcmd = (
114-
SELECT STRING_AGG(CAST('DBCC CHECKIDENT (''[' + [s].[name] + '].[' + [t].[name] + ']'', RESEED, ' + CAST([ic].[seed_value] AS VARCHAR(20)) + ')' AS NVARCHAR(MAX)),';' + CHAR(10)) WITHIN GROUP (ORDER BY [t].[name])
115-
FROM
116-
[sys].[schemas] AS [s],
117-
[sys].[tables] AS [t],
118-
[sys].[columns] AS [c],
119-
[sys].[identity_columns] AS [ic]
120-
WHERE
121-
[s].[schema_id] = [t].[schema_id]
122-
AND [t].[object_id] = [c].[object_id]
123-
AND [c].[is_identity] = 1
124-
AND [c].[object_id] = [ic].[object_id]
125-
AND [c].[column_id] = [ic].[column_id]
126-
)
127-
128-
EXEC (@sqlcmd)");
103+
foreach (var statement in GetClearDataStatements())
104+
{
105+
database.ExecuteNonQuery(statement);
106+
}
107+
}
129108

130-
database.ExecuteNonQuery("EXEC sp_msforeachtable 'ALTER TABLE ? WITH CHECK CHECK CONSTRAINT all'");
109+
/// <summary>
110+
/// Clear all in the database asynchronously.
111+
/// </summary>
112+
/// <param name="database">SQL Server database which the data have to be deleted.</param>
113+
/// <param name="cancellationToken"><see cref="CancellationToken"/> used to cancel the asynchronous operation.</param>
114+
/// <returns>A <see cref="Task"/> which represents the asynchronous operation.</returns>
115+
public static async Task ClearAllDataAsync(this SqlServerDatabase database, CancellationToken cancellationToken = default)
116+
{
117+
foreach (var statement in GetClearDataStatements())
118+
{
119+
await database.ExecuteNonQueryAsync(statement, cancellationToken);
120+
}
131121
}
132122

133123
/// <summary>
@@ -243,6 +233,34 @@ Type t when Array.Exists(AuthorizedNonStringTypes, at => at == t) => builder.Add
243233
return statement;
244234
}
245235

236+
private static string[] GetClearDataStatements()
237+
{
238+
return
239+
[
240+
"EXEC sp_msforeachtable 'ALTER TABLE ? NOCHECK CONSTRAINT all'",
241+
"EXEC sp_msforeachtable 'SET QUOTED_IDENTIFIER ON; DELETE FROM ?'",
242+
@"DECLARE @sqlcmd VARCHAR(MAX);
243+
244+
SET @sqlcmd = (
245+
SELECT STRING_AGG(CAST('DBCC CHECKIDENT (''[' + [s].[name] + '].[' + [t].[name] + ']'', RESEED, ' + CAST([ic].[seed_value] AS VARCHAR(20)) + ')' AS NVARCHAR(MAX)),';' + CHAR(10)) WITHIN GROUP (ORDER BY [t].[name])
246+
FROM
247+
[sys].[schemas] AS [s],
248+
[sys].[tables] AS [t],
249+
[sys].[columns] AS [c],
250+
[sys].[identity_columns] AS [ic]
251+
WHERE
252+
[s].[schema_id] = [t].[schema_id]
253+
AND [t].[object_id] = [c].[object_id]
254+
AND [c].[is_identity] = 1
255+
AND [c].[object_id] = [ic].[object_id]
256+
AND [c].[column_id] = [ic].[column_id]
257+
)
258+
259+
EXEC (@sqlcmd)",
260+
"EXEC sp_msforeachtable 'ALTER TABLE ? WITH CHECK CHECK CONSTRAINT all'",
261+
];
262+
}
263+
246264
private sealed class SqlInsertStatementBuilder
247265
{
248266
private readonly string tableName;

0 commit comments

Comments
 (0)