ensure collections created on upgrade

upgrade does not check that collections exists. this can cause
issues when running upgrade before any data is inserted. this patch
creates collection on upgrade if it does not exist.

Change-Id: I1774d1c943050f1a87b386502392e18b0af8c840
Closes-Bug: #1434013
This commit is contained in:
gordon chung 2015-03-23 10:35:59 -04:00
parent 7e5a6711ce
commit 5c0762534b
3 changed files with 17 additions and 0 deletions

View File

@ -58,6 +58,13 @@ class Connection(pymongo_base.Connection):
# needed.
self.upgrade()
def upgrade(self):
# create collection if not present
if 'alarm' not in self.db.conn.collection_names():
self.db.conn.create_collection('alarm')
if 'alarm_history' not in self.db.conn.collection_names():
self.db.conn.create_collection('alarm_history')
def clear(self):
self.conn.drop_database(self.db.name)
# Connection will be reopened automatically if needed

View File

@ -53,6 +53,9 @@ class Connection(pymongo_base.Connection):
self.upgrade()
def upgrade(self):
# create collection if not present
if 'event' not in self.db.conn.collection_names():
self.db.conn.create_collection('event')
# Establish indexes
ttl = cfg.CONF.database.event_time_to_live
impl_mongodb.Connection.update_ttl(ttl, 'event_ttl', 'timestamp',

View File

@ -442,6 +442,13 @@ class Connection(pymongo_base.Connection):
# project_id values are usually mutually exclusive in the
# queries, so the database won't take advantage of an index
# including both.
# create collection if not present
if 'resource' not in self.db.conn.collection_names():
self.db.conn.create_collection('resource')
if 'meter' not in self.db.conn.collection_names():
self.db.conn.create_collection('meter')
name_qualifier = dict(user_id='', project_id='project_')
background = dict(user_id=False, project_id=True)
for primary in ['user_id', 'project_id']: