Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- """
- Postgresql config: local auth-> password
- table_schema = public
- """
- import psycopg2
- import sys
- DB_NAME = "git"
- USER_NAME = "hub"
- PASSWORD = "gist"
- try:
- conn = psycopg2.connect("dbname=%s user=%s password=%s" % (DB_NAME, USER_NAME, PASSWORD))
- conn.set_isolation_level(0)
- except psycopg2.OperationalError:
- print "Error: Cannot connect to DB."
- sys.exit(1)
- # Here go all the string for LIKE
- # Example: 'gith%'
- like_dict = {}
- # Here go exact matches
- # Example: 'github'
- exact_dict = {
- "github"
- }
- cur = conn.cursor()
- if not like_dict and not exact_dict:
- print "Error: Empty sets."
- sys.exit(1)
- else:
- try:
- execute_string = "SELECT " \
- "table_schema,table_name " \
- "FROM information_schema.tables " \
- "WHERE table_schema = 'public' AND ( %s ) " \
- "ORDER BY table_schema,table_name"
- table_string = ''
- if like_dict:
- for el in like_dict:
- table_string += " OR table_name LIKE '%s'" % el
- if exact_dict:
- for el in exact_dict:
- table_string += " OR table_name = '%s'" % el
- table_string = table_string.replace(" OR ", "", 1)
- cur.execute(execute_string % table_string)
- rows = cur.fetchall()
- for row in rows:
- print "Cascading delete: ", row[1]
- cur.execute("drop table " + row[1] + " cascade")
- cur.close()
- conn.close()
- except:
- print "Error: ", sys.exc_info()[1]
- sys.exit(1)
- print "OK."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement