postgres.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import json
  2. import psycopg2
  3. import traceback
  4. from colorama import Fore
  5. from toolset.utils.output_helper import log
  6. class Database:
  7. @staticmethod
  8. def get_current_world_table(config):
  9. '''
  10. Return a JSON object containing all 10,000 World items as they currently
  11. exist in the database. This is used for verifying that entries in the
  12. database have actually changed during an Update verification test.
  13. '''
  14. results_json = []
  15. try:
  16. db = psycopg2.connect(
  17. host=config.database_host,
  18. port="5432",
  19. user="benchmarkdbuser",
  20. password="benchmarkdbpass",
  21. database="hello_world")
  22. cursor = db.cursor()
  23. cursor.execute("SELECT * FROM \"World\"")
  24. results = cursor.fetchall()
  25. results_json.append(json.loads(json.dumps(dict(results))))
  26. cursor = db.cursor()
  27. cursor.execute("SELECT * FROM \"world\"")
  28. results = cursor.fetchall()
  29. results_json.append(json.loads(json.dumps(dict(results))))
  30. db.close()
  31. except Exception:
  32. tb = traceback.format_exc()
  33. log("ERROR: Unable to load current Postgres World table.",
  34. color=Fore.RED)
  35. log(tb)
  36. return results_json
  37. @staticmethod
  38. def test_connection(config):
  39. try:
  40. db = psycopg2.connect(
  41. host=config.database_host,
  42. port="5432",
  43. user="benchmarkdbuser",
  44. password="benchmarkdbpass",
  45. database="hello_world")
  46. cursor = db.cursor()
  47. cursor.execute("SELECT 1")
  48. cursor.fetchall()
  49. db.close()
  50. return True
  51. except:
  52. return False