provision.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. # dialects/postgresql/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. import time
  8. from ... import exc
  9. from ... import inspect
  10. from ... import text
  11. from ...testing import warn_test_suite
  12. from ...testing.provision import create_db
  13. from ...testing.provision import drop_all_schema_objects_post_tables
  14. from ...testing.provision import drop_all_schema_objects_pre_tables
  15. from ...testing.provision import drop_db
  16. from ...testing.provision import log
  17. from ...testing.provision import prepare_for_drop_tables
  18. from ...testing.provision import set_default_schema_on_connection
  19. from ...testing.provision import temp_table_keyword_args
  20. @create_db.for_db("postgresql")
  21. def _pg_create_db(cfg, eng, ident):
  22. template_db = cfg.options.postgresql_templatedb
  23. with eng.execution_options(isolation_level="AUTOCOMMIT").begin() as conn:
  24. if not template_db:
  25. template_db = conn.exec_driver_sql(
  26. "select current_database()"
  27. ).scalar()
  28. attempt = 0
  29. while True:
  30. try:
  31. conn.exec_driver_sql(
  32. "CREATE DATABASE %s TEMPLATE %s" % (ident, template_db)
  33. )
  34. except exc.OperationalError as err:
  35. attempt += 1
  36. if attempt >= 3:
  37. raise
  38. if "accessed by other users" in str(err):
  39. log.info(
  40. "Waiting to create %s, URI %r, "
  41. "template DB %s is in use sleeping for .5",
  42. ident,
  43. eng.url,
  44. template_db,
  45. )
  46. time.sleep(0.5)
  47. except:
  48. raise
  49. else:
  50. break
  51. @drop_db.for_db("postgresql")
  52. def _pg_drop_db(cfg, eng, ident):
  53. with eng.connect().execution_options(isolation_level="AUTOCOMMIT") as conn:
  54. with conn.begin():
  55. conn.execute(
  56. text(
  57. "select pg_terminate_backend(pid) from pg_stat_activity "
  58. "where usename=current_user and pid != pg_backend_pid() "
  59. "and datname=:dname"
  60. ),
  61. dict(dname=ident),
  62. )
  63. conn.exec_driver_sql("DROP DATABASE %s" % ident)
  64. @temp_table_keyword_args.for_db("postgresql")
  65. def _postgresql_temp_table_keyword_args(cfg, eng):
  66. return {"prefixes": ["TEMPORARY"]}
  67. @set_default_schema_on_connection.for_db("postgresql")
  68. def _postgresql_set_default_schema_on_connection(
  69. cfg, dbapi_connection, schema_name
  70. ):
  71. existing_autocommit = dbapi_connection.autocommit
  72. dbapi_connection.autocommit = True
  73. cursor = dbapi_connection.cursor()
  74. cursor.execute("SET SESSION search_path='%s'" % schema_name)
  75. cursor.close()
  76. dbapi_connection.autocommit = existing_autocommit
  77. @drop_all_schema_objects_pre_tables.for_db("postgresql")
  78. def drop_all_schema_objects_pre_tables(cfg, eng):
  79. with eng.connect().execution_options(isolation_level="AUTOCOMMIT") as conn:
  80. for xid in conn.execute("select gid from pg_prepared_xacts").scalars():
  81. conn.execute("ROLLBACK PREPARED '%s'" % xid)
  82. @drop_all_schema_objects_post_tables.for_db("postgresql")
  83. def drop_all_schema_objects_post_tables(cfg, eng):
  84. from sqlalchemy.dialects import postgresql
  85. inspector = inspect(eng)
  86. with eng.begin() as conn:
  87. for enum in inspector.get_enums("*"):
  88. conn.execute(
  89. postgresql.DropEnumType(
  90. postgresql.ENUM(name=enum["name"], schema=enum["schema"])
  91. )
  92. )
  93. @prepare_for_drop_tables.for_db("postgresql")
  94. def prepare_for_drop_tables(config, connection):
  95. """Ensure there are no locks on the current username/database."""
  96. result = connection.exec_driver_sql(
  97. "select pid, state, wait_event_type, query "
  98. # "select pg_terminate_backend(pid), state, wait_event_type "
  99. "from pg_stat_activity where "
  100. "usename=current_user "
  101. "and datname=current_database() and state='idle in transaction' "
  102. "and pid != pg_backend_pid()"
  103. )
  104. rows = result.all() # noqa
  105. if rows:
  106. warn_test_suite(
  107. "PostgreSQL may not be able to DROP tables due to "
  108. "idle in transaction: %s"
  109. % ("; ".join(row._mapping["query"] for row in rows))
  110. )