Question

I'm working on automated tests for particular web app. It uses database to persist data (SQL Server in this case).

In our automated tests we perform several database changes (inserts, updates). And after tests have been executed we want to restore database to original state.

Steps will be something like this:

  1. Create somehow backup
  2. Execute tests
  3. Restore data from backup

The first version was pretty simple - create table backup and then restore it. But we've encountered an issue with references integrity.

After that we decided to use full database backup, but I don't like this idea.

Also we were thinking that we can track all references and backup only needed tables not a whole database.

Last thoughts was about somehow logging our actions (inserts, updates) and then perform reverse actions (deletes for inserts, updates with old data for updates), but it looks kinda complicated.

May be there is another solution?

Était-ce utile?

La solution 5

Found simple solution - renaming table.

Algorithm is pretty simple:

  1. Rename table to another table, e.g. "table" to "table-backup" (references will refer to that backup table)
  2. Create "table" from "table-backup" ("table" will not have any dependencies)
  3. Perform any actions in the application
  4. Drop "table" with dirty data (will not break references integrity)
  5. Rename "table-backup" to "table" (references integrity will be kept).

Thanks

Autres conseils

Actually, there is no need to restore the database in native SQL Server terms, nor to track the changes and then revert them back

You can use ApexSQL Restore – a SQL Server tool that attaches both native and natively compressed SQL database backups and transaction log backups as live databases, accessible via SQL Server Management Studio, Visual Studio or any other third-party tool. It allows attaching single or multiple full, differential and transaction log backups

For more information on how to use the tool in your scenario check the Using SQL database backups instead of live databases in a large development team online article

Disclaimer: I work as a Product Support Engineer at ApexSQL

If you wanting to do minimal changes to the database with Insert and Update, it is much better alternative to do those changes within transactions which can be rolled back at the end of the test. This way SQL server will automatically store information in regards what you changes and will reverse it back to state before test began.

BEGIN TRANSACTION http://technet.microsoft.com/en-us/library/ms188929.aspx

I think the better idea is to create test database. You can also create Interface for methods, first implementation for real data (with real db) and the second one for test db.

You can create a database snapshot.

The snapshot will keep track of all changed data pages that are changed during your test. Once you are done, you can restore back form the snapshot to the previous state.

CREATE DATABASE [test_snaphot1] ON
( NAME = test, FILENAME = 
'e:\SQLServer\Data\test_snapshot1.ss' )
AS SNAPSHOT OF [test];
GO

--do all your tests

RESTORE DATABASE [test] from 
DATABASE_SNAPSHOT = 'test_snaphot1';
GO

You have to create a snapshot file for each datafile of your database. So if you have a database with 4 data files, then your snapshot syntax should include 4 snapshot files.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top