Add batchupdate tool. PgBrowser: add constraints to list_column_usage().
authorRadek Brich <radek.brich@devl.cz>
Fri, 23 Mar 2012 14:54:04 +0100
changeset 32 d59c473c9ad7
parent 31 c2e6e24b83d9
child 33 bd0beda49bcb
Add batchupdate tool. PgBrowser: add constraints to list_column_usage().
batchupdate.py
pgtoolkit/pgbrowser.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/batchupdate.py	Fri Mar 23 14:54:04 2012 +0100
@@ -0,0 +1,32 @@
+#!/usr/bin/env python3.2
+
+import time
+
+from pgtoolkit import toolbase
+from pgtoolkit.highlight import highlight
+
+
+class BatchUpdateTool(toolbase.SimpleTool):
+    def __init__(self):
+        toolbase.SimpleTool.__init__(self, name='batchupdate', desc='Run an UPDATE query until it does not update any rows.')
+        self.parser.add_argument('--query', dest='query', type=str, help='Query to run. It should be constructed using subquery with LIMIT.')
+        self.parser.add_argument('--sleep', dest='sleep', default=0, type=int, help='Sleep SLEEP seconds between subsequent queries.')
+        self.init()
+
+    def main(self):
+        # connect DB
+        with self.pgm.cursor('target') as curs:
+            rowcount = 1
+            while rowcount > 0:
+                print('query:', self.args.query)
+                curs.execute(self.args.query, [])
+                rowcount = curs.rowcount
+                print('updated', rowcount)
+                curs.connection.commit()
+                print('sleep %s seconds' % self.args.sleep)
+                time.sleep(self.args.sleep)
+
+
+tool = BatchUpdateTool()
+tool.main()
+
--- a/pgtoolkit/pgbrowser.py	Mon Mar 05 18:36:46 2012 +0100
+++ b/pgtoolkit/pgbrowser.py	Fri Mar 23 14:54:04 2012 +0100
@@ -260,7 +260,9 @@
     def list_column_usage(self, table, column, schema='public'):
         '''List objects using the column.
         
-        These objects may block alteration of column. Currently only views are listed.
+        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('''
@@ -268,5 +270,12 @@
                 '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
+            WHERE table_schema=%(schema)s AND table_name=%(table)s AND column_name=%(column)s
             ''', {'schema':schema, 'table':table, 'column':column})