Friday, August 17, 2007

Wierd ActiveRecord "bug"

As I have written earlier I am using the Castle ActiveRecord framework extensibly. It is an extremely cool framework that has cut down on the amount of database work I am doing - shortening development cycles considerably. But I am still learning, and today I finally squashed a very odd bug that had bugged me for a while.

The scenario is this: I am storing some data for the Warlocks game server in a database. This database uses the ASP.NET 2.0 Membership API to store user data as well. The part where I am at right now involves setting up some user data and then using some ActiveRecords. And for some reason this did not work.

The problem, it seemed, was that the schema for the ActiveRecords did not exist in the database. I spent several debugging sessions assuring that I had calld the "Init" method of the AbstractModelTestCase (kindly provided by Castle for making unit testing of ActiveRecords much easier) - which in turn creates the scheme. But the error was there nonetheless.

Working systematically through the test by uncommenting lines to find out what caused the error, I found out that the problem occured when I added the code that added users through the Membership class. Without that it ran fine! I tried adding a new Init call after that code, and the test worked like a charm.

So what is up here? It seems like initializing and using the Membership provider will in some way reset the database. The database is a local file database that is in the project and is copied to output - ie. the data directory. Maybe the Membership framework copies the database file again, thus overwriting the database that has had the Schema created in it. I haven't cracked it yet - but if you know what is happening, please tell me. For now I have fixed my AbstractModelTestCase to initialize the Membership framwork before creating the scheme.

If you are wondering why I am not using a database on a server, it is simply because it is much easier to use a file database so the tests can be distributed to other developers and my buildserver without setting up anything.