StackStorm/st2 — pull request #777 by Kami (2014-11-05 06:05:09+00:00)
Title: Create database indexes up-front in setup_db function before starting the app

This branch fixes one of the issues reported in #776.

## Background

By default, mongoengine creates database indexes lazily when inserting a document in the database. This means, database could end up in a bad / corrupted state if there are multiple concurrent request trying to insert a document before an index has been created.

In our case, this showed up as duplicate entries which violate the unique constraint in the DB. The reason why this could happen is because insert happened before unique and other indexes were created.

## Proposed solution

In this branch I create all the indexes up-front in the `setup_db` function. This function is always called first before the application becomes available. This means it should now be impossible for the database to end up in a state which violates index constraints.