Advertisement
Guest User

Untitled

a guest
Mar 15th, 2016
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. """
  4. Postgresql config: local auth-> password
  5. table_schema = public
  6. """
  7.  
  8. import psycopg2
  9. import sys
  10.  
  11. DB_NAME = "git"
  12. USER_NAME = "hub"
  13. PASSWORD = "gist"
  14.  
  15. try:
  16. conn = psycopg2.connect("dbname=%s user=%s password=%s" % (DB_NAME, USER_NAME, PASSWORD))
  17. conn.set_isolation_level(0)
  18.  
  19. except psycopg2.OperationalError:
  20. print "Error: Cannot connect to DB."
  21. sys.exit(1)
  22.  
  23. # Here go all the string for LIKE
  24. # Example: 'gith%'
  25. like_dict = {}
  26.  
  27. # Here go exact matches
  28. # Example: 'github'
  29. exact_dict = {
  30. "github"
  31. }
  32.  
  33. cur = conn.cursor()
  34. if not like_dict and not exact_dict:
  35. print "Error: Empty sets."
  36. sys.exit(1)
  37. else:
  38. try:
  39. execute_string = "SELECT " \
  40. "table_schema,table_name " \
  41. "FROM information_schema.tables " \
  42. "WHERE table_schema = 'public' AND ( %s ) " \
  43. "ORDER BY table_schema,table_name"
  44. table_string = ''
  45. if like_dict:
  46. for el in like_dict:
  47. table_string += " OR table_name LIKE '%s'" % el
  48. if exact_dict:
  49. for el in exact_dict:
  50. table_string += " OR table_name = '%s'" % el
  51. table_string = table_string.replace(" OR ", "", 1)
  52. cur.execute(execute_string % table_string)
  53. rows = cur.fetchall()
  54. for row in rows:
  55. print "Cascading delete: ", row[1]
  56. cur.execute("drop table " + row[1] + " cascade")
  57. cur.close()
  58. conn.close()
  59. except:
  60. print "Error: ", sys.exc_info()[1]
  61. sys.exit(1)
  62.  
  63. print "OK."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement