From 5c0762534b6cceccff151001399e047ffa7f9498 Mon Sep 17 00:00:00 2001 From: gordon chung Date: Mon, 23 Mar 2015 10:35:59 -0400 Subject: [PATCH] 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 --- ceilometer/alarm/storage/impl_mongodb.py | 7 +++++++ ceilometer/event/storage/impl_mongodb.py | 3 +++ ceilometer/storage/impl_mongodb.py | 7 +++++++ 3 files changed, 17 insertions(+) diff --git a/ceilometer/alarm/storage/impl_mongodb.py b/ceilometer/alarm/storage/impl_mongodb.py index 07a78edc2a..f4549af3a5 100644 --- a/ceilometer/alarm/storage/impl_mongodb.py +++ b/ceilometer/alarm/storage/impl_mongodb.py @@ -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 diff --git a/ceilometer/event/storage/impl_mongodb.py b/ceilometer/event/storage/impl_mongodb.py index 728c1258e3..822831cf2f 100644 --- a/ceilometer/event/storage/impl_mongodb.py +++ b/ceilometer/event/storage/impl_mongodb.py @@ -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', diff --git a/ceilometer/storage/impl_mongodb.py b/ceilometer/storage/impl_mongodb.py index d7b114420f..557f8e226a 100644 --- a/ceilometer/storage/impl_mongodb.py +++ b/ceilometer/storage/impl_mongodb.py @@ -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']: