TheDeeno's Blog

DbSnapshot v0.1 Released!

Posted in programming by TheDeeno on August 31, 2009

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!

Advertisement
Tagged with: , , , ,

4 Responses

Subscribe to comments with RSS.

  1. David L. Penton said, on September 9, 2009 at 9:28 pm

    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

    • TheDeeno said, on September 9, 2009 at 9:30 pm

      Will do. I’ll update the release later tonight.

    • TheDeeno said, on September 9, 2009 at 10:05 pm

      Re: quoting db name

      Couldn’t I get away with just quoting the name in the command?

      ex. BACKUP DATABASE “dbname”

      • David L. Penton said, on September 9, 2009 at 9:51 pm

        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 “\”" } }


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.