Restoring SQL db quickly through TestComplete
Hi all,
I would like to perform multiple functional tests using our software which, in the back end, interacts with a MS SQL database in a data destructive manner i.e. changes table data, etc.
Then I would like to quickly restore the db to it's pre-functional test state and perform more tests (so each set of tests always start with the db in the same state).
Currently it seems I would have to manually restore the db between each set of tests.
Is there a way with TestComplete to restore a SQL db at runtime to a previous state without going through the SQL Management studio UI?
Just thought this might be a problem many people have had and is solved.
Thanks much
Colin_McCrae yes this is a problem. Our devs (and I also found this googling) have this method of making sure no other processes are running:
USE master; ALTER DATABASE <DB_NAME_GOES_HERE> SET SINGLE_USER WITH ROLLBACK IMMEDIATE; ALTER DATABASE <DB_NAME_GOES_HERE> SET MULTI_USER;
Sorry for the delay. Basically all you're doing is passing up some sql that is generated from SSMS. Then I have parameterized this so I can do this for any dbs I want to restore.
So I'm sure you know that if you right click on a db in SSMS you can restore a database. After you fill in the db name you can go to "New Query Editor Window". This will generate the sql script that would restore the db if you did this from within SSMS.
USE [master]
RESTORE DATABASE [My_TestDB] FROM DISK = N'C:\db_backups\My_TestDB.bak' WITH FILE = 1, MOVE N'My_Data' TO N'C:\Program Files\Microsoft SQL Server\MSSQL12.SQLDEV2014\MSSQL\DATA\My_TestDB.MDF', MOVE N'MyData_Log' TO N'C:\Program Files\Microsoft SQL Server\MSSQL12.SQLDEV2014\MSSQL\DATA\MyData_Log.LDF', NOUNLOAD, STATS = 5However that query can also be passed as a query to sql from TC/TE to restore dbs!
So parameterize that and you're gold!
var dbTargetName = "MyNewDB";
var dbSourceFile = "C:\\db_backups\\automated_testing_source\\" + "MyDB.bak";
var queryStringSource = "USE [master]RESTORE DATABASE [%s] FROM DISK = N'%s' WITH FILE = 1, MOVE N'Data' TO N'C:\\Program Files\\Microsoft SQL Server\\MSSQL10_50.TESTSERVER\\MSSQL\\DATA\\%s.MDF', MOVE N'Log' TO N'C:\\Program Files\\Microsoft SQL Server\\MSSQL10_50.TESTSERVER\\MSSQL\\DATA\\%s.LDF', NOUNLOAD, STATS = 5"; var queryStringFinal = aqString.Format(queryStringSource, dbTargetName, dbSourceFile, dbTargetName, dbTargetName);Your paths to where your SQL server is installed may vary so you will have to update those but can be parameterized as well.
To see the bolded bits above come from the Files area of the Restore Database dialog. The values under the "Logical File Name". Mine, for the most part, have been exactly the same. There is a way to query those values which I do in a C# tool version of the restore functionality. I'm not sure how to do this in jscript but can research that if you like.
but let me know of any questions. I'm sure there are areas that I could have presented more clearly.