provision.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. # dialects/oracle/provision.py
  2. # Copyright (C) 2005-2025 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. # mypy: ignore-errors
  8. from ... import create_engine
  9. from ... import exc
  10. from ... import inspect
  11. from ...engine import url as sa_url
  12. from ...testing.provision import configure_follower
  13. from ...testing.provision import create_db
  14. from ...testing.provision import drop_all_schema_objects_post_tables
  15. from ...testing.provision import drop_all_schema_objects_pre_tables
  16. from ...testing.provision import drop_db
  17. from ...testing.provision import follower_url_from_main
  18. from ...testing.provision import log
  19. from ...testing.provision import post_configure_engine
  20. from ...testing.provision import run_reap_dbs
  21. from ...testing.provision import set_default_schema_on_connection
  22. from ...testing.provision import stop_test_class_outside_fixtures
  23. from ...testing.provision import temp_table_keyword_args
  24. from ...testing.provision import update_db_opts
  25. @create_db.for_db("oracle")
  26. def _oracle_create_db(cfg, eng, ident):
  27. # NOTE: make sure you've run "ALTER DATABASE default tablespace users" or
  28. # similar, so that the default tablespace is not "system"; reflection will
  29. # fail otherwise
  30. with eng.begin() as conn:
  31. conn.exec_driver_sql("create user %s identified by xe" % ident)
  32. conn.exec_driver_sql("create user %s_ts1 identified by xe" % ident)
  33. conn.exec_driver_sql("create user %s_ts2 identified by xe" % ident)
  34. conn.exec_driver_sql("grant dba to %s" % (ident,))
  35. conn.exec_driver_sql("grant unlimited tablespace to %s" % ident)
  36. conn.exec_driver_sql("grant unlimited tablespace to %s_ts1" % ident)
  37. conn.exec_driver_sql("grant unlimited tablespace to %s_ts2" % ident)
  38. # these are needed to create materialized views
  39. conn.exec_driver_sql("grant create table to %s" % ident)
  40. conn.exec_driver_sql("grant create table to %s_ts1" % ident)
  41. conn.exec_driver_sql("grant create table to %s_ts2" % ident)
  42. @configure_follower.for_db("oracle")
  43. def _oracle_configure_follower(config, ident):
  44. config.test_schema = "%s_ts1" % ident
  45. config.test_schema_2 = "%s_ts2" % ident
  46. def _ora_drop_ignore(conn, dbname):
  47. try:
  48. conn.exec_driver_sql("drop user %s cascade" % dbname)
  49. log.info("Reaped db: %s", dbname)
  50. return True
  51. except exc.DatabaseError as err:
  52. log.warning("couldn't drop db: %s", err)
  53. return False
  54. @drop_all_schema_objects_pre_tables.for_db("oracle")
  55. def _ora_drop_all_schema_objects_pre_tables(cfg, eng):
  56. _purge_recyclebin(eng)
  57. _purge_recyclebin(eng, cfg.test_schema)
  58. @drop_all_schema_objects_post_tables.for_db("oracle")
  59. def _ora_drop_all_schema_objects_post_tables(cfg, eng):
  60. with eng.begin() as conn:
  61. for syn in conn.dialect._get_synonyms(conn, None, None, None):
  62. conn.exec_driver_sql(f"drop synonym {syn['synonym_name']}")
  63. for syn in conn.dialect._get_synonyms(
  64. conn, cfg.test_schema, None, None
  65. ):
  66. conn.exec_driver_sql(
  67. f"drop synonym {cfg.test_schema}.{syn['synonym_name']}"
  68. )
  69. for tmp_table in inspect(conn).get_temp_table_names():
  70. conn.exec_driver_sql(f"drop table {tmp_table}")
  71. @drop_db.for_db("oracle")
  72. def _oracle_drop_db(cfg, eng, ident):
  73. with eng.begin() as conn:
  74. # cx_Oracle seems to occasionally leak open connections when a large
  75. # suite it run, even if we confirm we have zero references to
  76. # connection objects.
  77. # while there is a "kill session" command in Oracle Database,
  78. # it unfortunately does not release the connection sufficiently.
  79. _ora_drop_ignore(conn, ident)
  80. _ora_drop_ignore(conn, "%s_ts1" % ident)
  81. _ora_drop_ignore(conn, "%s_ts2" % ident)
  82. @stop_test_class_outside_fixtures.for_db("oracle")
  83. def _ora_stop_test_class_outside_fixtures(config, db, cls):
  84. try:
  85. _purge_recyclebin(db)
  86. except exc.DatabaseError as err:
  87. log.warning("purge recyclebin command failed: %s", err)
  88. # clear statement cache on all connections that were used
  89. # https://github.com/oracle/python-cx_Oracle/issues/519
  90. for cx_oracle_conn in _all_conns:
  91. try:
  92. sc = cx_oracle_conn.stmtcachesize
  93. except db.dialect.dbapi.InterfaceError:
  94. # connection closed
  95. pass
  96. else:
  97. cx_oracle_conn.stmtcachesize = 0
  98. cx_oracle_conn.stmtcachesize = sc
  99. _all_conns.clear()
  100. def _purge_recyclebin(eng, schema=None):
  101. with eng.begin() as conn:
  102. if schema is None:
  103. # run magic command to get rid of identity sequences
  104. # https://floo.bar/2019/11/29/drop-the-underlying-sequence-of-an-identity-column/ # noqa: E501
  105. conn.exec_driver_sql("purge recyclebin")
  106. else:
  107. # per user: https://community.oracle.com/tech/developers/discussion/2255402/how-to-clear-dba-recyclebin-for-a-particular-user # noqa: E501
  108. for owner, object_name, type_ in conn.exec_driver_sql(
  109. "select owner, object_name,type from "
  110. "dba_recyclebin where owner=:schema and type='TABLE'",
  111. {"schema": conn.dialect.denormalize_name(schema)},
  112. ).all():
  113. conn.exec_driver_sql(f'purge {type_} {owner}."{object_name}"')
  114. _all_conns = set()
  115. @post_configure_engine.for_db("oracle")
  116. def _oracle_post_configure_engine(url, engine, follower_ident):
  117. from sqlalchemy import event
  118. @event.listens_for(engine, "checkout")
  119. def checkout(dbapi_con, con_record, con_proxy):
  120. _all_conns.add(dbapi_con)
  121. @event.listens_for(engine, "checkin")
  122. def checkin(dbapi_connection, connection_record):
  123. # work around cx_Oracle issue:
  124. # https://github.com/oracle/python-cx_Oracle/issues/530
  125. # invalidate oracle connections that had 2pc set up
  126. if "cx_oracle_xid" in connection_record.info:
  127. connection_record.invalidate()
  128. @run_reap_dbs.for_db("oracle")
  129. def _reap_oracle_dbs(url, idents):
  130. log.info("db reaper connecting to %r", url)
  131. eng = create_engine(url)
  132. with eng.begin() as conn:
  133. log.info("identifiers in file: %s", ", ".join(idents))
  134. to_reap = conn.exec_driver_sql(
  135. "select u.username from all_users u where username "
  136. "like 'TEST_%' and not exists (select username "
  137. "from v$session where username=u.username)"
  138. )
  139. all_names = {username.lower() for (username,) in to_reap}
  140. to_drop = set()
  141. for name in all_names:
  142. if name.endswith("_ts1") or name.endswith("_ts2"):
  143. continue
  144. elif name in idents:
  145. to_drop.add(name)
  146. if "%s_ts1" % name in all_names:
  147. to_drop.add("%s_ts1" % name)
  148. if "%s_ts2" % name in all_names:
  149. to_drop.add("%s_ts2" % name)
  150. dropped = total = 0
  151. for total, username in enumerate(to_drop, 1):
  152. if _ora_drop_ignore(conn, username):
  153. dropped += 1
  154. log.info(
  155. "Dropped %d out of %d stale databases detected", dropped, total
  156. )
  157. @follower_url_from_main.for_db("oracle")
  158. def _oracle_follower_url_from_main(url, ident):
  159. url = sa_url.make_url(url)
  160. return url.set(username=ident, password="xe")
  161. @temp_table_keyword_args.for_db("oracle")
  162. def _oracle_temp_table_keyword_args(cfg, eng):
  163. return {
  164. "prefixes": ["GLOBAL TEMPORARY"],
  165. "oracle_on_commit": "PRESERVE ROWS",
  166. }
  167. @set_default_schema_on_connection.for_db("oracle")
  168. def _oracle_set_default_schema_on_connection(
  169. cfg, dbapi_connection, schema_name
  170. ):
  171. cursor = dbapi_connection.cursor()
  172. cursor.execute("ALTER SESSION SET CURRENT_SCHEMA=%s" % schema_name)
  173. cursor.close()
  174. @update_db_opts.for_db("oracle")
  175. def _update_db_opts(db_url, db_opts, options):
  176. """Set database options (db_opts) for a test database that we created."""
  177. if (
  178. options.oracledb_thick_mode
  179. and sa_url.make_url(db_url).get_driver_name() == "oracledb"
  180. ):
  181. db_opts["thick_mode"] = True