provision.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. from ... import create_engine
  2. from ... import exc
  3. from ...engine import url as sa_url
  4. from ...testing.provision import configure_follower
  5. from ...testing.provision import create_db
  6. from ...testing.provision import drop_db
  7. from ...testing.provision import follower_url_from_main
  8. from ...testing.provision import log
  9. from ...testing.provision import post_configure_engine
  10. from ...testing.provision import run_reap_dbs
  11. from ...testing.provision import set_default_schema_on_connection
  12. from ...testing.provision import stop_test_class_outside_fixtures
  13. from ...testing.provision import temp_table_keyword_args
  14. @create_db.for_db("oracle")
  15. def _oracle_create_db(cfg, eng, ident):
  16. # NOTE: make sure you've run "ALTER DATABASE default tablespace users" or
  17. # similar, so that the default tablespace is not "system"; reflection will
  18. # fail otherwise
  19. with eng.begin() as conn:
  20. conn.exec_driver_sql("create user %s identified by xe" % ident)
  21. conn.exec_driver_sql("create user %s_ts1 identified by xe" % ident)
  22. conn.exec_driver_sql("create user %s_ts2 identified by xe" % ident)
  23. conn.exec_driver_sql("grant dba to %s" % (ident,))
  24. conn.exec_driver_sql("grant unlimited tablespace to %s" % ident)
  25. conn.exec_driver_sql("grant unlimited tablespace to %s_ts1" % ident)
  26. conn.exec_driver_sql("grant unlimited tablespace to %s_ts2" % ident)
  27. @configure_follower.for_db("oracle")
  28. def _oracle_configure_follower(config, ident):
  29. config.test_schema = "%s_ts1" % ident
  30. config.test_schema_2 = "%s_ts2" % ident
  31. def _ora_drop_ignore(conn, dbname):
  32. try:
  33. conn.exec_driver_sql("drop user %s cascade" % dbname)
  34. log.info("Reaped db: %s", dbname)
  35. return True
  36. except exc.DatabaseError as err:
  37. log.warning("couldn't drop db: %s", err)
  38. return False
  39. @drop_db.for_db("oracle")
  40. def _oracle_drop_db(cfg, eng, ident):
  41. with eng.begin() as conn:
  42. # cx_Oracle seems to occasionally leak open connections when a large
  43. # suite it run, even if we confirm we have zero references to
  44. # connection objects.
  45. # while there is a "kill session" command in Oracle,
  46. # it unfortunately does not release the connection sufficiently.
  47. _ora_drop_ignore(conn, ident)
  48. _ora_drop_ignore(conn, "%s_ts1" % ident)
  49. _ora_drop_ignore(conn, "%s_ts2" % ident)
  50. @stop_test_class_outside_fixtures.for_db("oracle")
  51. def stop_test_class_outside_fixtures(config, db, cls):
  52. try:
  53. with db.begin() as conn:
  54. # run magic command to get rid of identity sequences
  55. # https://floo.bar/2019/11/29/drop-the-underlying-sequence-of-an-identity-column/ # noqa E501
  56. conn.exec_driver_sql("purge recyclebin")
  57. except exc.DatabaseError as err:
  58. log.warning("purge recyclebin command failed: %s", err)
  59. # clear statement cache on all connections that were used
  60. # https://github.com/oracle/python-cx_Oracle/issues/519
  61. for cx_oracle_conn in _all_conns:
  62. try:
  63. sc = cx_oracle_conn.stmtcachesize
  64. except db.dialect.dbapi.InterfaceError:
  65. # connection closed
  66. pass
  67. else:
  68. cx_oracle_conn.stmtcachesize = 0
  69. cx_oracle_conn.stmtcachesize = sc
  70. _all_conns.clear()
  71. _all_conns = set()
  72. @post_configure_engine.for_db("oracle")
  73. def _oracle_post_configure_engine(url, engine, follower_ident):
  74. from sqlalchemy import event
  75. @event.listens_for(engine, "checkout")
  76. def checkout(dbapi_con, con_record, con_proxy):
  77. _all_conns.add(dbapi_con)
  78. @event.listens_for(engine, "checkin")
  79. def checkin(dbapi_connection, connection_record):
  80. # work around cx_Oracle issue:
  81. # https://github.com/oracle/python-cx_Oracle/issues/530
  82. # invalidate oracle connections that had 2pc set up
  83. if "cx_oracle_xid" in connection_record.info:
  84. connection_record.invalidate()
  85. @run_reap_dbs.for_db("oracle")
  86. def _reap_oracle_dbs(url, idents):
  87. log.info("db reaper connecting to %r", url)
  88. eng = create_engine(url)
  89. with eng.begin() as conn:
  90. log.info("identifiers in file: %s", ", ".join(idents))
  91. to_reap = conn.exec_driver_sql(
  92. "select u.username from all_users u where username "
  93. "like 'TEST_%' and not exists (select username "
  94. "from v$session where username=u.username)"
  95. )
  96. all_names = {username.lower() for (username,) in to_reap}
  97. to_drop = set()
  98. for name in all_names:
  99. if name.endswith("_ts1") or name.endswith("_ts2"):
  100. continue
  101. elif name in idents:
  102. to_drop.add(name)
  103. if "%s_ts1" % name in all_names:
  104. to_drop.add("%s_ts1" % name)
  105. if "%s_ts2" % name in all_names:
  106. to_drop.add("%s_ts2" % name)
  107. dropped = total = 0
  108. for total, username in enumerate(to_drop, 1):
  109. if _ora_drop_ignore(conn, username):
  110. dropped += 1
  111. log.info(
  112. "Dropped %d out of %d stale databases detected", dropped, total
  113. )
  114. @follower_url_from_main.for_db("oracle")
  115. def _oracle_follower_url_from_main(url, ident):
  116. url = sa_url.make_url(url)
  117. return url.set(username=ident, password="xe")
  118. @temp_table_keyword_args.for_db("oracle")
  119. def _oracle_temp_table_keyword_args(cfg, eng):
  120. return {
  121. "prefixes": ["GLOBAL TEMPORARY"],
  122. "oracle_on_commit": "PRESERVE ROWS",
  123. }
  124. @set_default_schema_on_connection.for_db("oracle")
  125. def _oracle_set_default_schema_on_connection(
  126. cfg, dbapi_connection, schema_name
  127. ):
  128. cursor = dbapi_connection.cursor()
  129. cursor.execute("ALTER SESSION SET CURRENT_SCHEMA=%s" % schema_name)
  130. cursor.close()