tags:

views:

1990

answers:

5

I'm building a new app that is using NHibernate to generate the database schema but i can see a possible problem in the future.

Obviously you use all the data from your database is cleared when you update the schema but what stratagies do people use to restore any data to the new database. I am aware that massive changes to the schema will make this hard but was wondering how other people have dealt with this problem.

Cheers Colin G

PS I will not be doing this against the live database only using it to restore test data for integration test and continuous integration

A: 

We don't update the schema from NHibernate. We use SQLCompare to move database schemas across environments. SQLCompare does this non-destructively.

If you're already using NHibernate I would recommend creating the test data with code.

Min
+1  A: 

I think it is a good thing in many situations to let NHibernate generate the schema for you. To recreate the test data you either use code driven by a testing framework (such as NUnit) or you could export your test data as a SQL script which you can run after you have updated the schema.

Petter Wigle
+5  A: 

When testing, we use NHibernate to create the database, then a series of builders to create the data for each test fixture. We also use Sqlite for these tests, so they are lightening fast.

Our builders look a something like this:

public class CustomerBuilder : Builder<Customer>
{
   string firstName;
   string lastName;
   Guid id = Guid.Empty;

   public override Customer Build()
   {
      return new Customer() { Id = id, FirstName = firstName, LastName = }
   }

   public CustomerBuilder WithId(Guid newId)
   {
      id= newId;
      return this;
   }

   public CustomerBuilder WithFirstName(string newFirstName)
   {
      firstName = newFirstName;
      return this;
   }

   public CustomerBuilder WithLastName(string newLastName)
   {
      lastName = newLastName;
      return this;
   }
}

and usage:

var customer = new CustomerBuilder().WithFirstName("John").WithLastName("Doe").Build();

Because every line of code is written with TDD, we build up a comprehensive suite of data from scatch and will generally refactor some of it to factories that will wrap the above and make it a breeze to get dummy data in.

Chris Canal
A: 

We do it similarly at our company. We use NHibernate to generate the database for our development and testing purposes. For testing we use SQLite as the back-end and just simply generate the test data separately for each test test suite.

When updating our staging/production servers we use SQLCompare and some manual editing if changes are more complex.

jl
A: 

Just a quick question directed @Chris Canal-

I understand that using a fluent interface to build your objects makes it look readable, but is it really worth the extra effort required to write these "builders" when you can use C# 3.0 syntax?

i.e. take your example:

var customer = new CustomerBuilder().WithFirstName("John").WithLastName("Doe").Build();

is this really worth the effort in constructing a "builder" when instead you can do this (which is arguably just as readable, and in fact less code)?:

var customer = new Customer { FirstName = "John", LastName = "Doe" };

Andrew W
The simple case that @chriscanal uses probably doesnt show the use of builders well. For example, you may say new CustomerBuilder().WithNumberOfItems(10) where the WithNumberOfItems can generate 10 random items for you test and set some default values in the items for your test. This can reduce code and increase readablity from something like this var customer = new Customer{Items = new List<Item>{ new Item(){SomeThing = "", Price = 10}, ..... }
Colin G
@Andrew W: If you have a follow up question you should post it asa new question, not as an answer to an old question.More people will read it since old questions are not frequented very much.In the top right is an "Ask Question" button to do so.You of course can always link back to this page for reference if you want to.
sth