Last active
September 9, 2019 09:39
-
-
Save danielplawgo/2657961b16074e7552746033a70f2953 to your computer and use it in GitHub Desktop.
Jak użyć Sql Server Snapshots do resetowania danych w testach
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DatabaseRestoreService : IDatabaseRestoreService | |
{ | |
private Lazy<DataContext> _dataContext; | |
protected DataContext DataContext => _dataContext.Value; | |
public DatabaseRestoreService(Lazy<DataContext> dataContext) | |
{ | |
_dataContext = dataContext; | |
} | |
public Result Restore() | |
{ | |
var databaseName = DataContext.Database.Connection.Database; | |
SqlConnectionStringBuilder connectionBuilder = new SqlConnectionStringBuilder(DataContext.Database.Connection.ConnectionString) | |
{ InitialCatalog = "master" }; | |
using (var conn = new SqlConnection(connectionBuilder.ConnectionString)) | |
{ | |
conn.Open(); | |
using (var cmd = conn.CreateCommand()) | |
{ | |
cmd.CommandText = $@"ALTER DATABASE {databaseName} SET SINGLE_USER WITH ROLLBACK IMMEDIATE; | |
RESTORE DATABASE {databaseName} FROM DATABASE_SNAPSHOT = '{databaseName}_Snapshot'; | |
ALTER DATABASE {databaseName} SET MULTI_USER;"; | |
cmd.ExecuteNonQuery(); | |
} | |
} | |
return Result.Ok(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DebugController : ApiController | |
{ | |
private Lazy<IDatabaseRestoreService> _databaseRestoreService; | |
protected IDatabaseRestoreService DatabaseRestoreService => _databaseRestoreService.Value; | |
public DebugController(Lazy<IDatabaseRestoreService> databaseRestoreService) | |
{ | |
_databaseRestoreService = databaseRestoreService; | |
} | |
[Route("api/debug/resettestdata")] | |
public IHttpActionResult ResetTestData() | |
{ | |
var result = DatabaseRestoreService.Restore(); | |
if (result.Success == false) | |
{ | |
return Content(System.Net.HttpStatusCode.BadRequest, result); | |
} | |
return Ok(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IDatabaseRestoreService | |
{ | |
Result Restore(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE DATABASE [nazwa bazy]_Snapshot ON | |
( NAME = [nazwa bazy], FILENAME = 'C:\db\snapshot\[nazwa bazy]_Snapshot.ss' ) | |
AS SNAPSHOT OF [nazwa bazy]; | |
GO |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ALTER DATABASE [nazwa bazy] SET SINGLE_USER WITH ROLLBACK IMMEDIATE; | |
RESTORE DATABASE [nazwa bazy] FROM DATABASE_SNAPSHOT = '[nazwa bazy]_Snapshot'; | |
ALTER DATABASE [nazwa bazy] SET MULTI_USER; | |
GO |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment