DbSnapshot v0.1 Released!
Last night I finished up work on a small .net library called DbSnapshot. The library helps test database interactions by allowing you to easily save and restore snapshots of your database. Simplicity was the goal, so its increidibly easy to use. Lets see it in action!
public void MyTest()
{
using (var manager = new SqlSnapshotManager(connectionString, databaseName))
{
manager.SaveSnapshot();
// execute some potentially destructive db commands
manager.RestoreSnapshot();
// execute some more destructive db commands
manager.RestoreSnapshot();
}
}
You can easily port the above to setup a test fixture (example in nunit):
[TestFixture]
public class MyFixture
{
IDbSnapshotManager _manager;
[TestFixtureSetUp]
public void FixtureSetup()
{
_manager = new SqlSnapshotManager(CONNECTION_STRING, TEST_DATABASE_NAME);
_manager.SaveSnapshot(); // prepare for db tests
}
[SetUp]
public void Setup()
{
_manager.RestoreSnapshot(); // put db in an expected state before each test
}
// ...
// DO TONS OF POTENTIALLY DESTRUCTIVE TESTS
// ...
[TestFixtureTearDown]
public void FixtureSetup()
{
_manager.Dispose(); // clean up when finished with tests
}
}
The SqlSnapshotManager works by using SqlServer’s built in backup/restore facilities. It creates a COPY_ONLY backup (so it doesn’t interupt your current backup cycle) and saves it to a file in the “.dbsnapshot” directory under your project’s current directory. It implements IDisposable so make sure to dispose it! Disposal will ensure the database always gets restored and the work files/folders are removed.
Right now it only works with SqlServer but I plan on supporting MySql and others in the future.
If you want to help by creating a snapshot manager for your flavor of database engine, just implement IDbSnapshotManager and send me a patch!
You can get the v0.1 v.02 release here.
The source is on GitHub.
Clone url: git://github.com/thedeeno/DbSnapshot.git
Enjoy!

Dane,
For the database name, please quote the name. The SQL Server method of quoting a name is to replace ] with ]] and then wrap it in square brackets. So the database name
test[quote]test
would be
[test[quote]]test]
For the file path, replace any single quotes with two single quotes (replace ‘ with ”) as well.
This could be handled in your Fill extension method.
Thanks,
David
Will do. I’ll update the release later tonight.
Re: quoting db name
Couldn’t I get away with just quoting the name in the command?
ex. BACKUP DATABASE “dbname”
You could, but the potential for a name with ” in it would be there. The methodology I’ve used in the past for something like this: a method that could be overridden that would provide for the implementation. For instance, MySQL uses ` for a quoted identifier. Oracle and DB2 uses ” and SQL Server uses [ and ] (note: it does use ” as well, but the QUOTENAME method by default uses square brackets).
/* coded in Firefox/html studio editor
*/
public abstract string LeftQuote { get; }
public abstract string RightQuote { get; }
public virtual string Quotename(string dbobject)
{
if ( dbobject == null || dbobject.Length == 0 )
throw new ArgumentException(
“dbobject”
, “The parameter cannot be null or a zero length string.”
);
return LeftQuote
+ dbobject.Replace( RightQuote, RightQuote + RightQuote )
+ RightQuote;
}
/* For SQL Server */
public string LeftQuote { get { return “[" } }
public string RightQuote { get { return "]” } }
/* For MySQL */
public string LeftQuote { get { return “`” } }
public string RightQuote { get { return “`” } }
/* For Oracle and DB2 */
public string LeftQuote { get { return “\”" } }
public string RightQuote { get { return “\”" } }