# HG changeset patch # User Radek Brich # Date 1355219382 -3600 # Node ID 26121a8fe78bdd655109cbb41b059ed4a4bafb65 # Parent bdc44f96cb0b77386e8b4a00f2ada72064e4b3d5 Update analyzeall tool: add REINDEX option. Add ibrowser tool (useful for PgBrowser testing). Fix PgBrowser.list_columns default value. diff -r bdc44f96cb0b -r 26121a8fe78b analyzeall.py --- a/analyzeall.py Fri Oct 05 14:31:25 2012 +0200 +++ b/analyzeall.py Tue Dec 11 10:49:42 2012 +0100 @@ -18,17 +18,20 @@ self.parser.add_argument('-s', dest='schema', nargs='*', help='Schema filter') self.parser.add_argument('--vacuum', action='store_true', help='Call VACUUM ANALYZE') self.parser.add_argument('--full', action='store_true', help='Call VACUUM FULL ANALYZE') + self.parser.add_argument('--reindex', action='store_true', help='Call REINDEX TABLE') self.target_isolation_level = 'autocommit' self.init() def main(self): browser = pgbrowser.PgBrowser(self.pgm.get_conn('target')) - + query_pattern = 'ANALYZE %s.%s;' if self.args.vacuum: query_pattern = 'VACUUM ANALYZE %s.%s;' if self.args.full: query_pattern = 'VACUUM FULL ANALYZE %s.%s;' + if self.args.reindex: + query_pattern = 'REINDEX TABLE %s.%s;' schema_list = self.args.schema if not schema_list: @@ -39,7 +42,7 @@ with self.pgm.cursor('target') as curs: for table in tables: query = query_pattern % (schema, table['name']) - print(query) + self.log.info(query) curs.execute(query, []) diff -r bdc44f96cb0b -r 26121a8fe78b ibrowser.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ibrowser.py Tue Dec 11 10:49:42 2012 +0100 @@ -0,0 +1,34 @@ +#!/usr/bin/env python3 +""" +interactive schema browser + +Connect to specified database and open interactive shell. + +PgBrowser instance is "browser". + +Example: + +>>> browser.schemas['myschema'].tables['mytable'].columns['id'].default +"nextval('mytable_id_seq'::regclass)" + +""" + +from pgtoolkit import pgbrowser, toolbase + +import code + + +class InteractiveBrowserTool(toolbase.SimpleTool): + def __init__(self): + toolbase.SimpleTool.__init__(self, name='ibrowser', desc='Interactive schema browser.') + self.init() + + def main(self): + browser = pgbrowser.PgBrowser(self.pgm.get_conn('target')) + shell = code.InteractiveConsole(locals()) + shell.interact() + + +tool = InteractiveBrowserTool() +tool.main() + diff -r bdc44f96cb0b -r 26121a8fe78b pgtoolkit/pgbrowser.py --- a/pgtoolkit/pgbrowser.py Fri Oct 05 14:31:25 2012 +0200 +++ b/pgtoolkit/pgbrowser.py Tue Dec 11 10:49:42 2012 +0100 @@ -39,7 +39,7 @@ self.hasdefault = hasdefault self.default = default self.description = description - + class Constraint: def __init__(self, browser, table, name, type, fname, fschema, definition): @@ -89,7 +89,7 @@ def refresh_constraints(self): rows = self.browser.list_constraints(self.name, self.schema.name) self._constraints = OrderedDict([(x['name'], Constraint(self.browser, self, **x)) for x in rows]) - + def refresh_indexes(self): rows = self.browser.list_indexes(self.name, self.schema.name) self._indexes = OrderedDict([(x['name'], Index(self.browser, self, **x)) for x in rows]) @@ -99,13 +99,13 @@ self.refresh_columns() return self._columns columns = property(getcolumns) - + def getconstraints(self): if self._constraints is None: self.refresh_constraints() return self._constraints constraints = property(getconstraints) - + def getindexes(self): if self._indexes is None: self.refresh_indexes() @@ -124,7 +124,7 @@ def refresh(self): rows = self.browser.list_tables(self.name) - self._tables = OrderedDict([(x['name'], Table(self.browser, self, **x)) for x in rows]) + self._tables = OrderedDict([(x['name'], Table(self.browser, self, **x)) for x in rows]) def gettables(self): if self._tables is None: @@ -137,10 +137,10 @@ def __init__(self, conn=None): self._schemas = None self.conn = conn - + def setconn(self, conn=None): self.conn = conn - + def refresh(self): rows = self.list_schemas() self._schemas = OrderedDict([(x['name'], Schema(self, **x)) for x in rows]) @@ -150,7 +150,7 @@ self.refresh() return self._schemas schemas = property(getschemas) - + def _query(self, query, args): try: curs = self.conn.cursor() @@ -218,7 +218,7 @@ format_type(a.atttypid, a.atttypmod) AS "type", a.attnotnull as "notnull", a.atthasdef as "hasdefault", - d.adsrc as "default", + pg_catalog.pg_get_expr(d.adbin, d.adrelid) as "default", pg_catalog.col_description(a.attrelid, a.attnum) AS "description" FROM pg_catalog.pg_attribute a LEFT JOIN pg_catalog.pg_class c ON a.attrelid = c.oid @@ -283,20 +283,20 @@ def list_column_usage(self, table, column, schema='public'): '''List objects using the column. - + Currently shows views and constraints which use the column. - + This is useful to find which views block alteration of column type etc. - + ''' return self._query(''' SELECT 'view' AS type, view_schema AS schema, view_name AS name FROM information_schema.view_column_usage WHERE table_schema=%(schema)s AND table_name=%(table)s AND column_name=%(column)s - + UNION - + SELECT 'constraint' AS type, constraint_schema AS schema, constraint_name AS name FROM information_schema.constraint_column_usage