provision.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # dialects/mysql/provision.py
  2. # Copyright (C) 2005-2024 the SQLAlchemy authors and contributors
  3. # <see AUTHORS file>
  4. #
  5. # This module is part of SQLAlchemy and is released under
  6. # the MIT License: https://www.opensource.org/licenses/mit-license.php
  7. from ... import exc
  8. from ...testing.provision import configure_follower
  9. from ...testing.provision import create_db
  10. from ...testing.provision import drop_db
  11. from ...testing.provision import generate_driver_url
  12. from ...testing.provision import temp_table_keyword_args
  13. @generate_driver_url.for_db("mysql", "mariadb")
  14. def generate_driver_url(url, driver, query_str):
  15. backend = url.get_backend_name()
  16. # NOTE: at the moment, tests are running mariadbconnector
  17. # against both mariadb and mysql backends. if we want this to be
  18. # limited, do the decision making here to reject a "mysql+mariadbconnector"
  19. # URL. Optionally also re-enable the module level
  20. # MySQLDialect_mariadbconnector.is_mysql flag as well, which must include
  21. # a unit and/or functional test.
  22. # all the Jenkins tests have been running mysqlclient Python library
  23. # built against mariadb client drivers for years against all MySQL /
  24. # MariaDB versions going back to MySQL 5.6, currently they can talk
  25. # to MySQL databases without problems.
  26. if backend == "mysql":
  27. dialect_cls = url.get_dialect()
  28. if dialect_cls._is_mariadb_from_url(url):
  29. backend = "mariadb"
  30. new_url = url.set(
  31. drivername="%s+%s" % (backend, driver)
  32. ).update_query_string(query_str)
  33. try:
  34. new_url.get_dialect()
  35. except exc.NoSuchModuleError:
  36. return None
  37. else:
  38. return new_url
  39. @create_db.for_db("mysql", "mariadb")
  40. def _mysql_create_db(cfg, eng, ident):
  41. with eng.begin() as conn:
  42. try:
  43. _mysql_drop_db(cfg, conn, ident)
  44. except Exception:
  45. pass
  46. with eng.begin() as conn:
  47. conn.exec_driver_sql(
  48. "CREATE DATABASE %s CHARACTER SET utf8mb4" % ident
  49. )
  50. conn.exec_driver_sql(
  51. "CREATE DATABASE %s_test_schema CHARACTER SET utf8mb4" % ident
  52. )
  53. conn.exec_driver_sql(
  54. "CREATE DATABASE %s_test_schema_2 CHARACTER SET utf8mb4" % ident
  55. )
  56. @configure_follower.for_db("mysql", "mariadb")
  57. def _mysql_configure_follower(config, ident):
  58. config.test_schema = "%s_test_schema" % ident
  59. config.test_schema_2 = "%s_test_schema_2" % ident
  60. @drop_db.for_db("mysql", "mariadb")
  61. def _mysql_drop_db(cfg, eng, ident):
  62. with eng.begin() as conn:
  63. conn.exec_driver_sql("DROP DATABASE %s_test_schema" % ident)
  64. conn.exec_driver_sql("DROP DATABASE %s_test_schema_2" % ident)
  65. conn.exec_driver_sql("DROP DATABASE %s" % ident)
  66. @temp_table_keyword_args.for_db("mysql", "mariadb")
  67. def _mysql_temp_table_keyword_args(cfg, eng):
  68. return {"prefixes": ["TEMPORARY"]}