Commits

Nick Stenning committed 30e8ba7

Add "paster db dropdataset <name>" command, to remove all non-shared data for
a dataset from mongo.

  • Participants
  • Parent commits 3797619

Comments (0)

Files changed (2)

File wdmmg/lib/command/db.py

 from base import WdmmgCommand
 
+import logging
+log = logging.getLogger(__name__)
+
 class DbCommand(WdmmgCommand):
     summary = "Interface to common database operations."
     usage = "<subcommand> [args, ...]"
                   Recognized subcommands:
                     drop:                 Drop database
                     dropcollections:      Drop collections
+                    dropdataset <name>:   Drop non-shared data for dataset <name>
                     loadexample <name>:   Load test fixture <name> into database
                   """
 
             self._cmd_drop()
         elif cmd == 'dropcollections':
             self._cmd_dropcollections()
+        elif cmd == 'dropdataset':
+            self._cmd_dropdataset()
         elif cmd == 'loadexample':
             self._cmd_loadexample()
         else:
         from wdmmg.model.mongo import drop_collections
         drop_collections()
 
+    def _cmd_dropdataset(self):
+        self._check_args_length(2)
+
+        ds_name = self.args[1]
+
+        log.warn("Dropping dataset '%s'", ds_name)
+
+        from wdmmg.model import mongo
+        db = mongo.db()
+
+        log.info("Removing entries for dataset %s", ds_name)
+        db.entry.remove({'dataset.name': ds_name})
+
+        log.info("Removing dimensions for dataset %s", ds_name)
+        db.dimension.remove({'dataset': ds_name})
+
+        log.info("Removing distincts for dataset %s", ds_name)
+        db['distincts__%s' % ds_name].drop()
+
+        log.info("Removing cubes for dataset %s", ds_name)
+        cubes = filter(lambda x: x.startswith('cubes.%s.' % ds_name),
+                       db.collection_names())
+        for c in cubes:
+            db[c].drop()
+
+        log.info("Removing dataset object for dataset %s", ds_name)
+        db.dataset.remove({'name': ds_name})
+
     def _cmd_loadexample(self):
         self._check_args_length(2)
         # TODO: separate the concepts of example/development data and test

File wdmmg/model/mongo.py

 connection = None
 db_name = None
 
+def make_connection(host, port):
+    global connection
+    connection = Connection(host, port)
+    connection.document_class = Base
+
+def db():
+    return Database(connection, db_name)
+
+def drop_db():
+    connection.drop_database(db_name)
+
+def drop_collections():
+    """
+    Drop all app collections from the database. This is far faster than
+    simply calling mongo.drop_db.
+    """
+    for name in db().collection_names():
+        if name not in ['system.indexes', 'system.js']:
+            db().drop_collection(name)
+
 class classproperty(property):
     def __get__(self, cls, owner):
         return self.fget.__get__(None, owner)()
                 if not ck in query_form.keys():
                     index_form[ck] = v
             index_form[k] = v
-        return index_form
-
-
-def make_connection(host, port):
-    global connection
-    connection = Connection(host, port)
-    connection.document_class = Base
-
-def db():
-    return Database(connection, db_name)
-
-def drop_db():
-    connection.drop_database(db_name)
-
-def drop_collections():
-    """
-    Drop all app collections from the database. This is far faster than
-    simply calling mongo.drop_db.
-    """
-    for name in db().collection_names():
-        if name not in ['system.indexes', 'system.js']:
-            db().drop_collection(name)
+        return index_form