def _listener(self, engine, listener_func): try: event.listen(engine, 'before_execute', listener_func) yield finally: event.remove(engine, 'before_execute', listener_func)
def attach_ddl_listeners(self): # Remove all previously added listeners, so that same listener don't # get added twice in situations where class configuration happens in # multiple phases (issue #31). for listener in self.listeners: event.remove(*listener) self.listeners = [] for column in self.processed_columns: # This sets up the trigger that keeps the tsvector column up to # date. if column.type.columns: table = column.table if self.option(column, 'remove_symbols'): self.add_listener(( table, 'after_create', self.search_function_ddl(column) )) self.add_listener(( table, 'after_drop', DDL(str(DropSearchFunctionSQL(column))) )) self.add_listener(( table, 'after_create', self.search_trigger_ddl(column) ))
def count_queries(): """Provides a query counter. Usage:: with count_queries() as count: do_stuff() assert count() == number_of_queries """ def _after_cursor_execute(*args, **kwargs): if active_counter[0]: active_counter[0][0] += 1 @contextmanager def _counter(): if active_counter[0]: raise RuntimeError('Cannot nest count_queries calls') active_counter[0] = counter = [0] try: yield lambda: counter[0] finally: active_counter[0] = None active_counter = [None] event.listen(Engine, 'after_cursor_execute', _after_cursor_execute) try: yield _counter finally: event.remove(Engine, 'after_cursor_execute', _after_cursor_execute)
def execute_counter(connection, zsa_savepoints, check_constraints): """ Count calls to execute """ from contextlib import contextmanager from sqlalchemy import event class Counter(object): def __init__(self): self.reset() self.conn = connection def reset(self): self.count = 0 @contextmanager def expect(self, count): start = self.count yield difference = self.count - start assert difference == count counter = Counter() @event.listens_for(connection, 'after_cursor_execute') def after_cursor_execute(conn, cursor, statement, parameters, context, executemany): # Ignore the testing savepoints if zsa_savepoints.state != 'begun' or check_constraints.state == 'checking': return counter.count += 1 yield counter event.remove(connection, 'after_cursor_execute', after_cursor_execute)
def ro_session_tracker(ro_session): """ This install an event handler into the active session, which tracks all SQL statements that are send via the session. The yielded checker can be called with an integer argument, representing the number of expected SQL statements, for example:: ro_session_tracker(0) would only succeed if no SQL statements where made. """ db_events = [] def scoped_conn_event_handler(calls): def conn_event_handler(**kw): calls.append((kw['statement'], kw['parameters'])) return conn_event_handler handler = scoped_conn_event_handler(db_events) event.listen(ro_session.bind, 'before_cursor_execute', handler, named=True) def checker(num=None): if num is not None: assert len(db_events) == num yield checker event.remove(ro_session.bind, 'before_cursor_execute', handler)
def test_propagate(self): Target = self._fixture() m1 = Mock() t1 = Target() t2 = Target() event.listen(t1, "event_one", m1, propagate=True) event.listen(t1, "event_two", m1, propagate=False) t2.dispatch._update(t1.dispatch) t1.dispatch.event_one("t1e1x") t1.dispatch.event_two("t1e2x") t2.dispatch.event_one("t2e1x") t2.dispatch.event_two("t2e2x") event.remove(t1, "event_one", m1) event.remove(t1, "event_two", m1) t1.dispatch.event_one("t1e1y") t1.dispatch.event_two("t1e2y") t2.dispatch.event_one("t2e1y") t2.dispatch.event_two("t2e2y") eq_(m1.mock_calls, [call('t1e1x'), call('t1e2x'), call('t2e1x')])
def test_external_tables_not_changed(self): def block_external_tables(conn, clauseelement, multiparams, params): if isinstance(clauseelement, sqlalchemy.sql.selectable.Select): return if (isinstance(clauseelement, six.string_types) and any(name in clauseelement for name in external.TABLES)): self.fail("External table referenced by neutron core " "migration.") if hasattr(clauseelement, 'element'): if (clauseelement.element.name in external.TABLES or (hasattr(clauseelement, 'table') and clauseelement.element.table.name in external.TABLES)): self.fail("External table referenced by neutron core " "migration.") engine = self.get_engine() cfg.CONF.set_override('connection', engine.url, group='database') migration.do_alembic_command(self.alembic_config, 'upgrade', 'kilo') event.listen(engine, 'before_execute', block_external_tables) migration.do_alembic_command(self.alembic_config, 'upgrade', 'heads') event.remove(engine, 'before_execute', block_external_tables)
def test_instance(self): Target = self._fixture() class Foo(object): def __init__(self): self.mock = Mock() def evt(self, arg): self.mock(arg) f1 = Foo() f2 = Foo() event.listen(Target, "event_one", f1.evt) event.listen(Target, "event_one", f2.evt) t1 = Target() t1.dispatch.event_one("x") event.remove(Target, "event_one", f1.evt) t1.dispatch.event_one("y") eq_(f1.mock.mock_calls, [call("x")]) eq_(f2.mock.mock_calls, [call("x"), call("y")])
def unregister_fsa_session_signals(): """Unregisters Flask-SQLAlchemy session commit and rollback signal handlers. When a Flask-SQLAlchemy object is created, it registers signal handlers for ``before_commit``, ``after_commit``, and ``after_rollback`` signals. In case of using both a plain SQLAlchemy session and a Flask-SQLAlchemy session (as is happening in the tests in this package), we need to unregister handlers or there will be some exceptions during test executions like:: AttributeError: 'Session' object has no attribute '_model_changes' """ # We don't need to do this if Flask-SQLAlchemy is not installed. if not has_flask_sqlalchemy: return # We don't need to do this if Flask-SQLAlchemy version 2.0 or # greater is installed. version = parse_version(flask_sqlalchemy.__version__) if version >= (2, 0): return events = flask_sqlalchemy._SessionSignalEvents signal_names = ('before_commit', 'after_commit', 'after_rollback') for signal_name in signal_names: # For Flask-SQLAlchemy version less than 3.0. signal = getattr(events, 'session_signal_{0}'.format(signal_name)) event.remove(SessionBase, signal_name, signal)
def register(cls, encryptor: Encryptor): """ Register this encryptable with an encryptor. Instances of this encryptor will be encrypted on initialization and decrypted on load. """ # save the current encryptor statically cls.__encryptor__ = encryptor # NB: we cannot use the before_insert listener in conjunction with a foreign key relationship # for encrypted data; SQLAlchemy will warn about using 'related attribute set' operation so # late in its insert/flush process. listeners = dict( init=on_init, load=on_load, ) for name, func in listeners.items(): # If we initialize the graph multiple times (as in many unit testing scenarios), # we will accumulate listener functions -- with unpredictable results. As protection, # we need to remove existing listeners before adding new ones; this solution only # works if the id (e.g. memory address) of the listener does not change, which means # they cannot be closures around the `encryptor` reference. # # Hence the `__encryptor__` hack above... if contains(cls, name, func): remove(cls, name, func) listen(cls, name, func)
def remove_sqlalchemy_known_event(self): for e, namespace, method in self._sqlalchemy_known_events: try: event.remove(e.mapper(self, namespace), e.event, method.get_attribute(self)) except InvalidRequestError: pass
def tearDown(self): """Roll back the session """ super(BlokTestCase, self).tearDown() self.case_savepoint.rollback() self.registry.System.Cache.invalidate_all() event.remove(self.registry.session, "after_transaction_end", self.savepoint_restarter) self._transaction_case_teared_down = True
def make_symmetrical(target, value, initiator): if value in target.friends: raise ValueError if target not in value.friends: event.remove(User.friends, 'append', make_symmetrical) value.friends.append(target) event.listen(User.friends, 'append', make_symmetrical)
def sqla_remove(*args): """Remove SQLA listeners. :param args: The args to pass onto remove. :returns: None. """ event.remove(*args) _REGISTERED_SQLA_EVENTS.remove(args)
def drop_db(database_uri=None): """ Convenience method for dropping the related tables in the database """ drop_fnc = DropSchema(SCHEMA_NAME) engine = get_engine(database_uri) event.listen(Base.metadata, 'after_drop', drop_fnc) Base.metadata.drop_all(engine) event.remove(Base.metadata, 'after_drop', drop_fnc)
def sqla_remove_all(): for args in _REGISTERED_SQLA_EVENTS: try: event.remove(*args) except sql_exc.InvalidRequestError: # already removed pass del _REGISTERED_SQLA_EVENTS[:]
def before_compile_nobake_fixture(self): @event.listens_for(Query, "before_compile", retval=True) def _modify_query(query): query = query.enable_assertions(False) return query yield event.remove(Query, "before_compile", _modify_query)
def unregister(cls, session): if hasattr(session, '_model_changes'): del session._model_changes event.remove(session, 'before_flush', cls.record_ops) event.remove(session, 'before_commit', cls.record_ops) event.remove(session, 'before_commit', cls.before_commit) event.remove(session, 'after_commit', cls.after_commit) event.remove(session, 'after_rollback', cls.after_rollback)
def unregister_events(self): try: event.remove(self._model_class, 'after_insert', self._db_event_handler) event.remove(self._model_class, 'after_delete', self._db_event_handler) except sql_exc.InvalidRequestError: LOG.warning(_LW("No sqlalchemy event for resource %s found"), self.name)
def count_queries(self): self.statements = [] def catch_queries(conn, cursor, statement, *args): self.statements.append(statement) event.listen(self.connection, 'before_cursor_execute', catch_queries) yield event.remove(self.connection, 'before_cursor_execute', catch_queries)
def __exit__(self, exc_type, exc_value, traceback): event.remove(self.engine, "handle_error", handler_bulk_insert_error) if self.enable_fast_executemany is True: event.remove(self.engine, "before_cursor_execute", handler_fast_executemany) if traceback is None: duration = time() - self.start_time logger.info( f'{self.table_name} insert took {duration:.2f} seconds')
def test_remove_instancelevel(self): listen_one = Mock() t1 = self.Target() event.listen(t1, "event_one", listen_one, add=True) t1.dispatch.event_one(5, 7) eq_(listen_one.mock_calls, [call(12)]) event.remove(t1, "event_one", listen_one) t1.dispatch.event_one(10, 5) eq_(listen_one.mock_calls, [call(12)])
def stop(self): """Stops the ``before_cursor_execute`` event listener""" if self._query_hook: sqla_event.remove(self._connectable, 'before_cursor_execute', self._query_hook) self._query_hook = None if self._replace_new_patch_aliases_config: self._replace_new_patch_aliases_config.__exit__(None, None, None) self._replace_new_patch_aliases_config = None
def unregister(cls, session): if hasattr(session, "_model_changes"): del session._model_changes event.remove(session, "before_flush", cls.record_ops) event.remove(session, "before_commit", cls.record_ops) event.remove(session, "before_commit", cls.before_commit) event.remove(session, "after_commit", cls.after_commit) event.remove(session, "after_rollback", cls.after_rollback)
def on_dispose(self): assert self.linked self.linked = False event.remove(self.child_rel_prop, 'set', self.on_key_update) self.parent_obj = None self.child_cls = None self.child_rel_name = None self.child_rel_prop = None
def create_db(database_uri=None): """ Creates the schema in the database """ create_fnc = CreateSchema(SCHEMA_NAME) engine = get_engine(database_uri) event.listen(Base.metadata, 'before_create', create_fnc) Base.metadata.create_all(engine) event.remove(Base.metadata, 'before_create', create_fnc) return engine
def query_recorder(app_config): recorder = QueryRecorder() engine = app_config.registry["sqlalchemy.engine"] event.listen(engine, "before_cursor_execute", recorder.record) try: yield recorder finally: event.remove(engine, "before_cursor_execute", recorder.record)
def __declare_last__(cls): # Unconfigure the event set in _SQLAMutationTracker, we have _save_data mapper = cls._sa_class_manager.mapper args = (mapper.attrs['data'], 'set', _SQLAMutationTracker._field_set) if event.contains(*args): event.remove(*args) # Declaring the event on the class attribute instead of mapper property # enables proper registration on its subclasses event.listen(cls.data, 'set', cls._set_data, retval=True)
def __declare_last__(cls): # Unconfigure the event set in _SQLAMutationTracker, we have _save_data mapper = cls._sa_class_manager.mapper args = (mapper.attrs['data'], 'set', _SQLAMutationTracker._field_set) if event.contains(*args): event.remove(*args) # Declaring the event on the class attribute instead of mapper property # enables proper registration on its subclasses event.listen(cls.data, 'set', cls._save_data, retval=True)
def statements(db): stmts = [] def catch_queries(conn, cursor, statement, parameters, context, executemany): stmts.append(statement) event.listen(db.engine, "before_cursor_execute", catch_queries) yield stmts event.remove(db.engine, "before_cursor_execute", catch_queries)
def setUpPackage(): """Setup the plugin.""" from default import flask_app with flask_app.app_context(): plugin_dir = os.path.dirname(plugin.__file__) plugin.LibCrowdsStatistics(plugin_dir).setup() # Remove event listeners func = event_listeners.record_new_task_run_ip_event event.remove(TaskRun, 'before_insert', func)
def registered(target, event, callback): if not target: raise IllegalArgumentException("'target' is requried") if not event: raise IllegalArgumentException("'event' is requried") if not callback: raise IllegalArgumentException("'callback' is requried") event.remove(targe, event, callback)
def test_event_no_native_float(self): def _remove_type(inputsizes, cursor, statement, parameters, context): for param, dbapitype in list(inputsizes.items()): if dbapitype is testing.db.dialect.dbapi.NATIVE_FLOAT: del inputsizes[param] event.listen(testing.db, "do_setinputsizes", _remove_type) try: self._test_setinputsizes(oracle.BINARY_FLOAT, 25.34534, None) finally: event.remove(testing.db, "do_setinputsizes", _remove_type)
def test_event_no_native_float(self): def _remove_type(inputsizes, cursor, statement, parameters, context): for param, dbapitype in list(inputsizes.items()): if dbapitype is testing.db.dialect.dbapi.NATIVE_FLOAT: del inputsizes[param] event.listen(testing.db, "do_setinputsizes", _remove_type) try: self.test_setinputsizes(oracle.BINARY_FLOAT, 25.34534, None, False) finally: event.remove(testing.db, "do_setinputsizes", _remove_type)
def remove_flush_listeners(self): """ remove all before_flush listeners, currently this is only use by a test, that switched between proxied and normal mode. """ for session in self.__db_session.values(): if event.contains(session, "before_flush", before_proxy_flush): event.remove(session, "before_flush", before_proxy_flush) self.__db_session = {} for factory in self.__db_factory.values(): factory.reset_events()
def unfreeze(self): """Stop modifying timestamps in queries.""" if self._query_hook: sa_event.remove(self._connectable, 'before_cursor_execute', self._query_hook) self._query_hook = None if self._freeze_time is not None: self._freeze_time.stop() self._freeze_time = None self._freezer_factory = None
def test_remove_invoke_clslevel(self): canary = Mock() event.listen(self.BaseTarget, "event_one", canary) s1 = self.SubTarget(self.BaseTarget()) event.remove(self.BaseTarget, "event_one", canary) s1.dispatch.event_one() eq_(canary.mock_calls, [])
def register_events(self, remove=False): for e, h in ( ('before_insert', self.before_insert), ('before_update', self.before_update), ('before_delete', self.before_delete), ): is_event_exist = event.contains(self.base_class, e, h) if remove and is_event_exist: event.remove(self.base_class, e, h) elif not is_event_exist: event.listen(self.base_class, e, h, propagate=True) return self
def sqla_remove_all(): """Removes all SQLA listeners. :returns: None. """ for args in _REGISTERED_SQLA_EVENTS: try: event.remove(*args) except sql_exc.InvalidRequestError: # already removed pass del _REGISTERED_SQLA_EVENTS[:]
def test_remove_wrapped_named(self): Target = self._wrapped_fixture() listen_one = Mock() t1 = Target() event.listen(t1, "event_one", listen_one, named=True) t1.dispatch.event_one("t1") eq_(listen_one.mock_calls, [call(x="adapted t1")]) event.remove(t1, "event_one", listen_one) t1.dispatch.event_one("t2") eq_(listen_one.mock_calls, [call(x="adapted t1")])
def commit(self): """Commit profiling session. :raises AssertionError: When the session is not alive. """ if not self.alive: raise AssertionError("Profiling session is already committed") self.alive = False self._get_stats() event.remove(self.engine, self._before, self._before_cursor_execute) event.remove(self.engine, self._after, self._after_cursor_execute)
def close(self): ''' Overrides the QWidget.close. ''' # Unsubscribe to database events. for cls in [model.ArchitectureBlock, model.View, model.Requirement, model.Bug, model.FunctionPoint]: try: event.remove(cls, 'after_update', self.onDetailUpdate) event.remove(cls, 'after_insert', self.onDetailInsert) except sqlalchemy.exc.InvalidRequestError: # No event handlers were found: ignore. pass self.hasEvents = False ViewerWithTreeBase.close(self)
def test_clslevel(self): Target = self._fixture() m1 = Mock() event.listen(Target, "event_two", m1) t1 = Target() t1.dispatch.event_two("x") event.remove(Target, "event_two", m1) t1.dispatch.event_two("y") eq_(m1.mock_calls, [call("x")])
def untrace_sqlalchemy(): try: args = [] for key in event.registry._key_to_collection: identifier = key[1] if identifier not in ('before_cursor_execute', 'after_cursor_execute', 'handle_error'): continue fn = ctypes.cast(key[2], ctypes.py_object).value args.append([identifier, fn]) for arg in args: event.remove(Engine, *arg) except Exception: pass
def connection(self, sync_engine, setup_schema): def restart(session, transaction): nonlocal nested if not nested.is_active: nested = conn.begin_nested() conn = sync_engine.connect() tx = conn.begin() nested = conn.begin_nested() event.listen(Session, "after_transaction_end", restart) yield conn event.remove(Session, "after_transaction_end", restart) nested.rollback() tx.rollback()
def no_deps(conn, DBSession): from sqlalchemy import event session = DBSession() @event.listens_for(session, 'after_flush') def check_dependencies(session, flush_context): assert not flush_context.cycles @event.listens_for(conn, "before_execute", retval=True) def before_execute(conn, clauseelement, multiparams, params): return clauseelement, multiparams, params yield event.remove(session, 'before_flush', check_dependencies)
def test_remove_not_listened(self): Target = self._fixture() m1 = Mock() t1 = Target() event.listen(t1, "event_one", m1, propagate=True) event.listen(t1, "event_three", m1) event.remove(t1, "event_one", m1) assert_raises_message( exc.InvalidRequestError, r"No listeners found for event <.*Target.*> / 'event_two' / <Mock.*> ", event.remove, t1, "event_two", m1) event.remove(t1, "event_three", m1)
def __exit__(self, exc_type, exc_value, exc_traceback): event.remove(*self._sql_listen_args) if exc_type is not None: return False if self.test_id: limit_msg = ((' Limit: %d.' % self.max_count) if self.max_count is not None else '') logging.info('%s executed %d queries.%s', self.test_id, len(self.queries), limit_msg) if self.max_count is None: return if len(self.queries) > self.max_count: message = ('Maximum query count exceeded: limit %d, executed %d.\n' '----QUERIES----\n%s\n----END----') % ( self.max_count, len(self.queries), '\n'.join( self.queries)) raise AssertionError(message)
def clear(self): logger.info( f"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ clearing Syned Models" ) for nr, ev in enumerate( ['after_insert', 'after_update', 'after_delete']): for model in self.models: listener = self.model_handlers[model] logger.info(f"remove listener {listener} for {model} on {ev}") if event.contains(model, ev, listener[nr]): event.remove(model, ev, listener[nr]) self.models.clear() self.model_names.clear() self.tables.clear() self.ids.clear()