Problem checking for existing index and creating a new index

Jan 9, 2015 at 2:01 PM
We ran into the following problem in a project that's under development, and I'm not sure if it's an NDatabase bug or something we're doing wrong.

We're using the following process on application startup:
  1. Defragment the database. Load the newly-defragmented database.
  2. Check each index we need (db.IndexManagerFor<T>().ExistIndex).
  3. If it does not exist, create the index (AddUniqueIndex).
  4. If it does exist, rebuild it (RebuildIndex)
One of our developers' databases started crashing on startup. It contained no data at the time, but had been used before.

When I checked the database, I found ExistIndex was returning false in step 2, but AddUniqueIndex in step 3 was causing a StackOverflowException trying to write the new index.

If I called db.QueryAndExecute<object>(), all the ClassInfoIndex objects still existed. Deleting these objects fixed the problem and allowed me to re-create the indexes.

Class names haven't changed. We have added a few new properties but (since it's still in the early stages of development) haven't defined any of these changes with RefactorManager.

Do you know why an index would become "orphaned" from its class info? Is there a way we can avoid this case or (worst case scenario) detect it and delete any indexes like this?