mysql.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import json
  2. import MySQLdb
  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 = MySQLdb.connect(config.database_host,
  17. "benchmarkdbuser", "benchmarkdbpass",
  18. "hello_world")
  19. cursor = db.cursor()
  20. cursor.execute("SELECT * FROM World")
  21. results = cursor.fetchall()
  22. results_json.append(json.loads(json.dumps(dict(results))))
  23. db.close()
  24. except Exception:
  25. tb = traceback.format_exc()
  26. log("ERROR: Unable to load current MySQL World table.",
  27. color=Fore.RED)
  28. log(tb)
  29. return results_json
  30. @staticmethod
  31. def test_connection(config):
  32. try:
  33. db = MySQLdb.connect(config.database_host, "benchmarkdbuser",
  34. "benchmarkdbpass", "hello_world")
  35. cursor = db.cursor()
  36. cursor.execute("SELECT 1")
  37. cursor.fetchall()
  38. db.close()
  39. return True
  40. except:
  41. return False