[CalendarServer-changes] [8633] CalendarServer/trunk/calendarserver/tools/dbinspect.py

source_changes at macosforge.org source_changes at macosforge.org
Wed Feb 8 08:57:20 PST 2012


Revision: 8633
          http://trac.macosforge.org/projects/calendarserver/changeset/8633
Author:   cdaboo at apple.com
Date:     2012-02-08 08:57:20 -0800 (Wed, 08 Feb 2012)
Log Message:
-----------
Add option to display size of each table.

Modified Paths:
--------------
    CalendarServer/trunk/calendarserver/tools/dbinspect.py

Modified: CalendarServer/trunk/calendarserver/tools/dbinspect.py
===================================================================
--- CalendarServer/trunk/calendarserver/tools/dbinspect.py	2012-02-08 15:45:43 UTC (rev 8632)
+++ CalendarServer/trunk/calendarserver/tools/dbinspect.py	2012-02-08 16:57:20 UTC (rev 8633)
@@ -25,7 +25,8 @@
 from calendarserver.tools import tables
 from calendarserver.tools.cmdline import utilityMain
 from pycalendar.datetime import PyCalendarDateTime
-from twext.enterprise.dal.syntax import Select, Parameter, Count, Delete
+from twext.enterprise.dal.syntax import Select, Parameter, Count, Delete,\
+    Constant
 from twisted.application.service import Service
 from twisted.internet.defer import inlineCallbacks, returnValue
 from twisted.python.filepath import FilePath
@@ -101,6 +102,42 @@
     def doIt(self, txn):
         raise NotImplementedError
 
+
+class TableSizes(Cmd):
+    
+    _name = "Show Size of each Table"
+    
+    @inlineCallbacks
+    def doIt(self, txn):
+        
+        results = []
+        for dbtable in schema.model.tables: #@UndefinedVariable
+            dbtable = getattr(schema, dbtable.name)
+            count = yield self.getTableSize(txn, dbtable)
+            results.append((dbtable.model.name, count,))
+        
+        # Print table of results
+        table = tables.Table()
+        table.addHeader(("Table", "Row Count"))
+        for dbtable, count in sorted(results):
+            table.addRow((
+                dbtable,
+                count,
+            ))
+        
+        print "\n"
+        print "Database Tables (total=%d):\n" % (len(results),)
+        table.printTable()
+
+    @inlineCallbacks
+    def getTableSize(self, txn, dbtable):
+        rows = (yield Select(
+            [Count(Constant(1)),],
+            From=dbtable,
+        ).on(txn))
+        returnValue(rows[0][0])
+
+
 class CalendarHomes(Cmd):
     
     _name = "List Calendar Homes"
@@ -293,6 +330,7 @@
         ).on(txn))
         returnValue(tuple(rows))
 
+
 class Event(Cmd):
     
     _name = "Get Event Data by Resource-ID"
@@ -336,6 +374,7 @@
         ).on(txn, **{"ResourceID": rid}))
         returnValue(rows[0] if rows else None)
 
+
 class EventsByUID(Cmd):
     
     _name = "Get Event Data by iCalendar UID"
@@ -762,6 +801,7 @@
         super(DBInspectService, self).startService()
         
         # Register commands
+        self.registerCommand(TableSizes)
         self.registerCommand(CalendarHomes)
         self.registerCommand(CalendarHomesSummary)
         self.registerCommand(Calendars)
@@ -803,9 +843,9 @@
             yield cmd().doIt(txn)
             yield txn.commit()
         except Exception, e:
+            traceback.print_exc()
+            print "Command '%s' failed because of: %s" % (cmd.name(), e,)
             yield txn.abort()
-            print "Command '%s' failed because of: %s" % (cmd.name(), e,)
-            traceback.print_exc()
 
     def printCommands(self):
         
@@ -867,7 +907,6 @@
         # anyway).
 
 
-
 def main(argv=sys.argv, stderr=sys.stderr, reactor=None):
     """
     Do the export.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20120208/1d5487b4/attachment-0001.html>


More information about the calendarserver-changes mailing list