Clean up tools.
--- a/bigtables.py Wed Jul 20 10:51:18 2011 +0200
+++ b/bigtables.py Wed Jul 20 10:54:07 2011 +0200
@@ -2,12 +2,12 @@
from pgtools import pgbrowser
from common import prettysize
-from toolbase import ToolBase
+from toolbase import SimpleTool
-class BigTablesTool(ToolBase):
+class BigTablesTool(SimpleTool):
def __init__(self):
- ToolBase.__init__(self, name='bigtables', desc='List largest tables.')
+ SimpleTool.__init__(self, name='bigtables', desc='List largest tables.')
self.init()
def main(self):
--- a/diff.py Wed Jul 20 10:51:18 2011 +0200
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,50 +0,0 @@
-#!/usr/bin/env python3.2
-
-import sys
-import argparse
-
-from pgtools import pgmanager, pgbrowser, pgdiff
-from common import config
-
-parser = argparse.ArgumentParser(description='Database schema diff.')
-parser.add_argument('source', metavar='source', type=str, help='Source database')
-parser.add_argument('destination', metavar='destination', type=str, help='Destination database')
-parser.add_argument('-s', dest='schema', nargs='*', help='Schema filter')
-parser.add_argument('-t', dest='table', nargs='*', help='Table filter')
-args = parser.parse_args()
-
-config = config.ConfigParser()
-config.add_argument('databases', type=dict)
-config.add_argument('meta_db')
-config.add_argument('meta_query')
-config.load('pgtoolkit.conf')
-
-pgm = pgmanager.get_instance()
-
-pgm.create_conn(name='meta', dns=config.meta_db)
-
-def build_conn(name, targetname):
- with pgm.cursor('meta') as curs:
- curs.execute(config.meta_query, [targetname])
- row = curs.fetchone_dict()
- curs.connection.commit()
-
- pgm.create_conn(name=name, **row)
-
-build_conn('src', args.source)
-build_conn('dst', args.destination)
-
-pgm.close_conn('meta')
-
-srcbrowser = pgbrowser.PgBrowser(pgm.get_conn('src'))
-dstbrowser = pgbrowser.PgBrowser(pgm.get_conn('dst'))
-
-pgd = pgdiff.PgDiff(srcbrowser, dstbrowser)
-
-if args.schema:
- pgd.filter_schemas(include=args.schema)
-
-if args.table:
- pgd.filter_tables(include=args.table)
-
-pgd.print_diff()
--- a/longqueries.py Wed Jul 20 10:51:18 2011 +0200
+++ b/longqueries.py Wed Jul 20 10:54:07 2011 +0200
@@ -1,12 +1,12 @@
#!/usr/bin/env python3.2
from pgtools import pgstats
-from toolbase import ToolBase
+from toolbase import SimpleTool
-class LongQueriesTool(ToolBase):
+class LongQueriesTool(SimpleTool):
def __init__(self):
- ToolBase.__init__(self, name='longqueries', desc='Lis long queries.')
+ SimpleTool.__init__(self, name='longqueries', desc='List long queries.')
self.init()
def main(self):
@@ -15,6 +15,7 @@
for ln in stats.list_long_queries():
print(ln['query'])
print(' ', 'query_start:', ln['query_start'].strftime('%F %T'))
+ print()
tool = LongQueriesTool()
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/schemadiff.py Wed Jul 20 10:54:07 2011 +0200
@@ -0,0 +1,32 @@
+#!/usr/bin/env python3.2
+
+from pgtools import pgmanager, pgbrowser, pgdiff
+from toolbase import SrcDstTool
+
+
+class SchemaDiffTool(SrcDstTool):
+ def __init__(self):
+ SrcDstTool.__init__(self, name='schemadiff', desc='Database schema diff.')
+
+ self.parser.add_argument('-s', dest='schema', nargs='*', help='Schema filter')
+ self.parser.add_argument('-t', dest='table', nargs='*', help='Table filter')
+
+ self.init()
+
+ def main(self):
+ srcbrowser = pgbrowser.PgBrowser(self.pgm.get_conn('src'))
+ dstbrowser = pgbrowser.PgBrowser(self.pgm.get_conn('dst'))
+
+ pgd = pgdiff.PgDiff(srcbrowser, dstbrowser)
+
+ if self.args.schema:
+ pgd.filter_schemas(include=self.args.schema)
+
+ if self.args.table:
+ pgd.filter_tables(include=self.args.table)
+
+ pgd.print_diff()
+
+
+tool = SchemaDiffTool()
+tool.main()
--- a/toolbase.py Wed Jul 20 10:51:18 2011 +0200
+++ b/toolbase.py Wed Jul 20 10:54:07 2011 +0200
@@ -7,7 +7,6 @@
class ToolBase:
def __init__(self, name, desc):
self.parser = argparse.ArgumentParser(description=desc)
- self.parser.add_argument('target', metavar='target', type=str, help='Target database')
self.config = config.ConfigParser()
self.config.add_argument('databases', type=dict)
@@ -19,11 +18,6 @@
def init(self):
self.config.load('pgtoolkit.conf')
self.args = self.parser.parse_args()
-
- if self.config.meta_db:
- self.pgm.create_conn(name='meta', dsn=self.config.meta_db)
- self.buildconn('target', self.args.target)
- self.pgm.close_conn('meta')
def buildconn(self, name, targetname):
with self.pgm.cursor('meta') as curs:
@@ -36,3 +30,31 @@
self.pgm.create_conn(name=name, **row)
+ def prepareconns(self, *args):
+ if self.config.meta_db:
+ self.pgm.create_conn(name='meta', dsn=self.config.meta_db)
+ for name in args:
+ self.buildconn(name, self.args.__dict__[name])
+ self.pgm.close_conn('meta')
+
+
+class SimpleTool(ToolBase):
+ def __init__(self, name, desc):
+ ToolBase.__init__(self, name, desc)
+ self.parser.add_argument('target', metavar='target', type=str, help='Target database')
+
+ def init(self):
+ ToolBase.init(self)
+ self.prepareconns('target')
+
+
+class SrcDstTool(ToolBase):
+ def __init__(self, name, desc):
+ ToolBase.__init__(self, name, desc)
+ self.parser.add_argument('src', metavar='source', type=str, help='Source database')
+ self.parser.add_argument('dst', metavar='destination', type=str, help='Destination database')
+
+ def init(self):
+ ToolBase.init(self)
+ self.prepareconns('src', 'dst')
+