Here are the examples of the python api sqlalchemy.event.listen taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
153 Examples
0
Example 101
Project: maraschino Source File: strategies.py
def create(self, name_or_url, **kwargs):
# create url.URL object
u = url.make_url(name_or_url)
dialect_cls = u.get_dialect()
dialect_args = {}
# consume dialect arguments from kwargs
for k in util.get_cls_kwargs(dialect_cls):
if k in kwargs:
dialect_args[k] = kwargs.pop(k)
dbapi = kwargs.pop('module', None)
if dbapi is None:
dbapi_args = {}
for k in util.get_func_kwargs(dialect_cls.dbapi):
if k in kwargs:
dbapi_args[k] = kwargs.pop(k)
dbapi = dialect_cls.dbapi(**dbapi_args)
dialect_args['dbapi'] = dbapi
# create dialect
dialect = dialect_cls(**dialect_args)
# assemble connection arguments
(cargs, cparams) = dialect.create_connect_args(u)
cparams.update(kwargs.pop('connect_args', {}))
# look for existing pool or create
pool = kwargs.pop('pool', None)
if pool is None:
def connect():
try:
return dialect.connect(*cargs, **cparams)
except Exception, e:
# Py3K
#raise exc.DBAPIError.instance(None, None,
# e, dialect.dbapi.Error,
# connection_invalidated=
# dialect.is_disconnect(e, None, None)
# ) from e
# Py2K
import sys
raise exc.DBAPIError.instance(
None, None, e, dialect.dbapi.Error,
connection_invalidated=
dialect.is_disconnect(e, None, None)), \
None, sys.exc_info()[2]
# end Py2K
creator = kwargs.pop('creator', connect)
poolclass = kwargs.pop('poolclass', None)
if poolclass is None:
poolclass = dialect_cls.get_pool_class(u)
pool_args = {}
# consume pool arguments from kwargs, translating a few of
# the arguments
translate = {'logging_name': 'pool_logging_name',
'echo': 'echo_pool',
'timeout': 'pool_timeout',
'recycle': 'pool_recycle',
'events':'pool_events',
'use_threadlocal':'pool_threadlocal'}
for k in util.get_cls_kwargs(poolclass):
tk = translate.get(k, k)
if tk in kwargs:
pool_args[k] = kwargs.pop(tk)
pool = poolclass(creator, **pool_args)
else:
if isinstance(pool, poollib._DBProxy):
pool = pool.get_pool(*cargs, **cparams)
else:
pool = pool
# create engine.
engineclass = self.engine_cls
engine_args = {}
for k in util.get_cls_kwargs(engineclass):
if k in kwargs:
engine_args[k] = kwargs.pop(k)
_initialize = kwargs.pop('_initialize', True)
# all kwargs should be consumed
if kwargs:
raise TypeError(
"Invalid argument(s) %s sent to create_engine(), "
"using configuration %s/%s/%s. Please check that the "
"keyword arguments are appropriate for this combination "
"of components." % (','.join("'%s'" % k for k in kwargs),
dialect.__class__.__name__,
pool.__class__.__name__,
engineclass.__name__))
engine = engineclass(pool, dialect, u, **engine_args)
if _initialize:
do_on_connect = dialect.on_connect()
if do_on_connect:
def on_connect(dbapi_connection, connection_record):
conn = getattr(dbapi_connection, '_sqla_unwrap', dbapi_connection)
if conn is None:
return
do_on_connect(conn)
event.listen(pool, 'first_connect', on_connect)
event.listen(pool, 'connect', on_connect)
def first_connect(dbapi_connection, connection_record):
c = base.Connection(engine, connection=dbapi_connection)
# TODO: removing this allows the on connect activities
# to generate events. tests currently assume these aren't
# sent. do we want users to get all the initial connect
# activities as events ?
c._has_events = False
dialect.initialize(c)
event.listen(pool, 'first_connect', first_connect)
return engine
0
Example 102
Project: maraschino Source File: pool.py
def __init__(self,
creator, recycle=-1, echo=None,
use_threadlocal=False,
logging_name=None,
reset_on_return=True,
listeners=None,
events=None,
_dispatch=None):
"""
Construct a Pool.
:param creator: a callable function that returns a DB-API
connection object. The function will be called with
parameters.
:param recycle: If set to non -1, number of seconds between
connection recycling, which means upon checkout, if this
timeout is surpassed the connection will be closed and
replaced with a newly opened connection. Defaults to -1.
:param logging_name: String identifier which will be used within
the "name" field of logging records generated within the
"sqlalchemy.pool" logger. Defaults to a hexstring of the object's
id.
:param echo: If True, connections being pulled and retrieved
from the pool will be logged to the standard output, as well
as pool sizing information. Echoing can also be achieved by
enabling logging for the "sqlalchemy.pool"
namespace. Defaults to False.
:param use_threadlocal: If set to True, repeated calls to
:meth:`connect` within the same application thread will be
guaranteed to return the same connection object, if one has
already been retrieved from the pool and has not been
returned yet. Offers a slight performance advantage at the
cost of individual transactions by default. The
:meth:`unique_connection` method is provided to bypass the
threadlocal behavior installed into :meth:`connect`.
:param reset_on_return: If true, reset the database state of
connections returned to the pool. This is typically a
ROLLBACK to release locks and transaction resources.
Disable at your own peril. Defaults to True.
:param events: a list of 2-tuples, each of the form
``(callable, target)`` which will be passed to event.listen()
upon construction. Provided here so that event listeners
can be assigned via ``create_engine`` before dialect-level
listeners are applied.
:param listeners: Deprecated. A list of
:class:`~sqlalchemy.interfaces.PoolListener`-like objects or
dictionaries of callables that receive events when DB-API
connections are created, checked out and checked in to the
pool. This has been superseded by
:func:`~sqlalchemy.event.listen`.
"""
if logging_name:
self.logging_name = self._orig_logging_name = logging_name
else:
self._orig_logging_name = None
log.instance_logger(self, echoflag=echo)
self._threadconns = threading.local()
self._creator = creator
self._recycle = recycle
self._use_threadlocal = use_threadlocal
self._reset_on_return = reset_on_return
self.echo = echo
if _dispatch:
self.dispatch._update(_dispatch, only_propagate=False)
if events:
for fn, target in events:
event.listen(self, target, fn)
if listeners:
util.warn_deprecated(
"The 'listeners' argument to Pool (and "
"create_engine()) is deprecated. Use event.listen().")
for l in listeners:
self.add_listener(l)
0
Example 103
Project: ggrc-core Source File: __init__.py
def init_session_monitor_cache():
from sqlalchemy.orm.session import Session
from sqlalchemy import event
from ggrc.services.common import get_cache
def update_cache_before_flush(session, flush_context, objects):
cache = get_cache(create=True)
if cache:
cache.update_before_flush(session, flush_context)
def update_cache_after_flush(session, flush_context):
cache = get_cache(create=False)
if cache:
cache.update_after_flush(session, flush_context)
def clear_cache(session):
cache = get_cache()
if cache:
cache.clear()
event.listen(Session, 'before_flush', update_cache_before_flush)
event.listen(Session, 'after_flush', update_cache_after_flush)
event.listen(Session, 'after_commit', clear_cache)
event.listen(Session, 'after_rollback', clear_cache)
0
Example 104
Project: sqlalchemy-i18n Source File: __init__.py
def make_translatable(
mapper=sa.orm.mapper,
session=sa.orm.session.Session,
manager=translation_manager,
options={}
):
"""
Assigns translation listeners for given mapper and session.
:param mapper:
SQLAlchemy declarative class or mapper to apply translation listeners
into.
:param session:
SQLAlchemy session class.
:param manager:
SQLAlchemy-i18n TranslationManager instance
:param options:
TranslationManager options
"""
manager.options.update(options)
sa.event.listen(
mapper, 'instrument_class', manager.instrument_translation_classes
)
sa.event.listen(
mapper, 'after_configured', manager.configure_translatable_classes
)
sa.event.listen(
session, 'before_flush', manager.auto_create_translations
)
0
Example 105
Project: indico Source File: models.py
@classmethod
def register_versioned_resource_events(cls):
"""Register SQLAlchemy events. Should be called
right after class definition."""
listen(cls.file, 'set', cls._add_file_to_relationship)
0
Example 106
@classmethod
def __declare_last__(cls):
event.listen(cls, 'before_update', cls._updated_at)
0
Example 107
Project: maraschino Source File: interfaces.py
@classmethod
def _adapt_listener(cls, self, listener):
def adapt_execute(conn, clauseelement, multiparams, params):
def execute_wrapper(clauseelement, *multiparams, **params):
return clauseelement, multiparams, params
return listener.execute(conn, execute_wrapper,
clauseelement, *multiparams,
**params)
event.listen(self, 'before_execute', adapt_execute)
def adapt_cursor_execute(conn, cursor, statement,
parameters,context, executemany, ):
def execute_wrapper(
cursor,
statement,
parameters,
context,
):
return statement, parameters
return listener.cursor_execute(
execute_wrapper,
cursor,
statement,
parameters,
context,
executemany,
)
event.listen(self, 'before_cursor_execute', adapt_cursor_execute)
def do_nothing_callback(*arg, **kw):
pass
def adapt_listener(fn):
def go(conn, *arg, **kw):
fn(conn, do_nothing_callback, *arg, **kw)
return util.update_wrapper(go, fn)
event.listen(self, 'begin', adapt_listener(listener.begin))
event.listen(self, 'rollback',
adapt_listener(listener.rollback))
event.listen(self, 'commit', adapt_listener(listener.commit))
event.listen(self, 'savepoint',
adapt_listener(listener.savepoint))
event.listen(self, 'rollback_savepoint',
adapt_listener(listener.rollback_savepoint))
event.listen(self, 'release_savepoint',
adapt_listener(listener.release_savepoint))
event.listen(self, 'begin_twophase',
adapt_listener(listener.begin_twophase))
event.listen(self, 'prepare_twophase',
adapt_listener(listener.prepare_twophase))
event.listen(self, 'rollback_twophase',
adapt_listener(listener.rollback_twophase))
event.listen(self, 'commit_twophase',
adapt_listener(listener.commit_twophase))
0
Example 108
Project: sqlalchemy Source File: test_transaction.py
def test_accounting_commit_fails_delete(self):
User = self.classes.User
sess = create_session(autocommit=True)
fail = False
def fail_fn(*arg, **kw):
if fail:
raise Exception("commit fails")
event.listen(sess, "after_flush_postexec", fail_fn)
u1 = User(name='ed')
sess.add(u1)
sess.flush()
sess.delete(u1)
fail = True
assert_raises(
Exception,
sess.flush
)
fail = False
assert u1 in sess
assert u1 not in sess.deleted
sess.delete(u1)
sess.flush()
assert u1 not in sess
eq_(
sess.query(User.name).order_by(User.name).all(),
[]
)
0
Example 109
Project: oslo.db Source File: engines.py
def create_engine(sql_connection, sqlite_fk=False, mysql_sql_mode=None,
idle_timeout=3600,
connection_debug=0, max_pool_size=None, max_overflow=None,
pool_timeout=None, sqlite_synchronous=True,
connection_trace=False, max_retries=10, retry_interval=10,
thread_checkin=True, logging_name=None,
json_serializer=None,
json_deserializer=None):
"""Return a new SQLAlchemy engine."""
url = sqlalchemy.engine.url.make_url(sql_connection)
engine_args = {
"pool_recycle": idle_timeout,
'convert_unicode': True,
'connect_args': {},
'logging_name': logging_name
}
_setup_logging(connection_debug)
_init_connection_args(
url, engine_args,
max_pool_size=max_pool_size,
max_overflow=max_overflow,
pool_timeout=pool_timeout,
json_serializer=json_serializer,
json_deserializer=json_deserializer,
)
engine = sqlalchemy.create_engine(url, **engine_args)
_init_events(
engine,
mysql_sql_mode=mysql_sql_mode,
sqlite_synchronous=sqlite_synchronous,
sqlite_fk=sqlite_fk,
thread_checkin=thread_checkin,
connection_trace=connection_trace
)
# register alternate exception handler
exc_filters.register_engine(engine)
# register engine connect handler
event.listen(engine, "engine_connect", _connect_ping_listener)
# initial connect + test
# NOTE(viktors): the current implementation of _test_connection()
# does nothing, if max_retries == 0, so we can skip it
if max_retries:
test_conn = _test_connection(engine, max_retries, retry_interval)
test_conn.close()
return engine
0
Example 110
Project: pittsburgh-purchasing-suite Source File: utils.py
def turn_on_sqlalchemy_events():
models = get_all_refresh_mixin_models()
for model in models:
for event in LISTEN_FOR_EVENTS:
sqlalchemy.event.listen(model, event, model.event_handler)
0
Example 111
Project: postgresql-audit Source File: base.py
def attach_listeners(self):
self.attach_table_listeners()
for listener in self.listeners:
sa.event.listen(*listener)
0
Example 112
@pytest.yield_fixture
@pytest.mark.usefixtures('db')
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)
0
Example 113
def register(cls, dct):
"""Register events defined on a class during metaclass creation."""
events = []
# append class attribute defined events
if dct.get('__events__'):
# Events defined on __events__ can have many forms (e.g. string based,
# list of tuples, etc). So we need to iterate over them and parse into
# standardized Event object.
for event_name, listeners in iteritems(dct['__events__']):
if not isinstance(listeners, list):
listeners = [listeners]
for listener in listeners:
if isinstance(listener, tuple):
# listener definition includes event.listen keyword args
listener, kargs = listener
else:
kargs = {}
if not callable(listener):
# assume listener is a string reference to class method
listener = dct[listener]
events.append(Event(event_name,
kargs.pop('attribute', None),
listener,
kargs))
# add events which were added via @event decorator
for value in dct.values():
if hasattr(value, '__event__'):
if not isinstance(value.__event__, list): # pragma: no cover
value.__event__ = [value.__event__]
events.extend(value.__event__)
if events:
# Reassemble events dict into consistent form using Event objects as
# values.
events_dict = {}
for evt in events:
if evt.attribute is None:
obj = cls
else:
obj = getattr(cls, evt.attribute)
if evt.name.startswith('on_'):
event_name = evt.name.replace('on_', '', 1)
else:
event_name = evt.name
sqlalchemy.event.listen(obj, event_name, evt.listener, **evt.kargs)
events_dict.setdefault(evt.name, []).append(evt)
dct['__events__'].update(events_dict)
0
Example 114
def register(self):
event.listen(self.engine, 'before_cursor_execute', self.before_cursor_execute)
event.listen(self.engine, 'after_cursor_execute', self.after_cursor_execute)
0
Example 115
Project: sqlalchemy Source File: test_instrumentation.py
def test_basic(self):
import pickle
global A
class A(object):
pass
def canary(instance):
assert False
try:
instrumentation.register_class(A)
manager = instrumentation.manager_of_class(A)
event.listen(manager, 'load', canary)
a = A()
p_a = pickle.dumps(a)
re_a = pickle.loads(p_a)
finally:
del A
0
Example 116
Project: maraschino Source File: mutable.py
@classmethod
def associate_with(cls, sqltype):
"""Associate this wrapper with all future mapped columns
of the given type.
This is a convenience method that calls ``associate_with_attribute`` automatically.
.. warning:: The listeners established by this method are *global*
to all mappers, and are *not* garbage collected. Only use
:meth:`.associate_with` for types that are permanent to an application,
not with ad-hoc types else this will cause unbounded growth
in memory usage.
"""
def listen_for_type(mapper, class_):
for prop in mapper.iterate_properties:
if hasattr(prop, 'columns'):
if isinstance(prop.columns[0].type, sqltype):
cls.associate_with_attribute(getattr(class_, prop.key))
event.listen(mapper, 'mapper_configured', listen_for_type)
0
Example 117
def __enter__(self):
event.listen(*self._sql_listen_args)
return self
0
Example 118
Project: maraschino Source File: descriptor_props.py
def _setup_event_handlers(self):
"""Establish events that populate/expire the composite attribute."""
def load_handler(state, *args):
dict_ = state.dict
if self.key in dict_:
return
# if column elements aren't loaded, skip.
# __get__() will initiate a load for those
# columns
for k in self._attribute_keys:
if k not in dict_:
return
#assert self.key not in dict_
dict_[self.key] = self.composite_class(
*[state.dict[key] for key in
self._attribute_keys]
)
def expire_handler(state, keys):
if keys is None or set(self._attribute_keys).intersection(keys):
state.dict.pop(self.key, None)
def insert_update_handler(mapper, connection, state):
"""After an insert or update, some columns may be expired due
to server side defaults, or re-populated due to client side
defaults. Pop out the composite value here so that it
recreates.
"""
state.dict.pop(self.key, None)
event.listen(self.parent, 'after_insert',
insert_update_handler, raw=True)
event.listen(self.parent, 'after_update',
insert_update_handler, raw=True)
event.listen(self.parent, 'load', load_handler, raw=True, propagate=True)
event.listen(self.parent, 'refresh', load_handler, raw=True, propagate=True)
event.listen(self.parent, "expire", expire_handler, raw=True, propagate=True)
0
Example 119
Project: sqlalchemy Source File: test_ddlevents.py
def test_table_by_metadata(self):
metadata, users, engine = self.metadata, self.users, self.engine
event.listen(users, 'before_create', DDL('mxyzptlk'))
event.listen(users, 'after_create', DDL('klptzyxm'))
event.listen(users, 'before_drop', DDL('xyzzy'))
event.listen(users, 'after_drop', DDL('fnord'))
metadata.create_all()
strings = [str(x) for x in engine.mock]
assert 'mxyzptlk' in strings
assert 'klptzyxm' in strings
assert 'xyzzy' not in strings
assert 'fnord' not in strings
del engine.mock[:]
metadata.drop_all()
strings = [str(x) for x in engine.mock]
assert 'mxyzptlk' not in strings
assert 'klptzyxm' not in strings
assert 'xyzzy' in strings
assert 'fnord' in strings
0
Example 120
Project: group-based-policy Source File: patch.py
def get_session(autocommit=True, expire_on_commit=False, use_slave=False):
# The folowing are declared as global so that they can
# used in the inner functions that follow.
global LOCAL_API_NOTIFICATION_QUEUE
global PUSH_NOTIFICATIONS_METHOD
global DISCARD_NOTIFICATIONS_METHOD
# This conditional logic is to ensure that local_api
# is imported only once.
if 'local_api' not in locals():
from gbpservice.network.neutronv2 import local_api
LOCAL_API_NOTIFICATION_QUEUE = local_api.NOTIFICATION_QUEUE
PUSH_NOTIFICATIONS_METHOD = (
local_api.post_notifications_from_queue)
DISCARD_NOTIFICATIONS_METHOD = (
local_api.discard_notifications_after_rollback)
# The following two lines are copied from the original
# implementation of db_api.get_session() and should be updated
# if the original implementation changes.
facade = db_api._create_facade_lazily()
new_session = facade.get_session(autocommit=autocommit,
expire_on_commit=expire_on_commit,
use_slave=use_slave)
def gbp_after_transaction(session, transaction):
global LOCAL_API_NOTIFICATION_QUEUE
if transaction and not transaction._parent and (
not transaction.is_active and not transaction.nested):
if transaction in LOCAL_API_NOTIFICATION_QUEUE:
# push the queued notifications only when the
# outermost transaction completes
PUSH_NOTIFICATIONS_METHOD(transaction)
def gbp_after_rollback(session):
# We discard all queued notifiactions if the transaction fails.
DISCARD_NOTIFICATIONS_METHOD(session.transaction)
if local_api.BATCH_NOTIFICATIONS:
event.listen(new_session, "after_transaction_end",
gbp_after_transaction)
event.listen(new_session, "after_rollback",
gbp_after_rollback)
return new_session
0
Example 121
Project: montage Source File: server.py
def create_app(env_name='prod'):
# rendering is handled by MessageMiddleware
routes = PUBLIC_ROUTES + JUROR_ROUTES + ADMIN_ROUTES + META_ROUTES
print '== creating WSGI app using env name: %s' % (env_name,)
config_file_name = 'config.%s.yaml' % env_name
config_file_path = os.path.join(PROJ_PATH, config_file_name)
print '== loading config file: %s' % (config_file_path,)
config = yaml.load(open(config_file_path))
logging.basicConfig()
logging.getLogger('sqlalchemy.engine').setLevel(logging.WARN)
engine = create_engine(config.get('db_url', DEFAULT_DB_URL), pool_recycle=60)
session_type = sessionmaker()
session_type.configure(bind=engine)
tmp_rdb_session = session_type()
schema_errors = get_schema_errors(Base, tmp_rdb_session)
if not schema_errors:
print '++ schema validated ok'
else:
for err in schema_errors:
print '!! ', err
print '!! recreate the database and update the code, then try again'
sys.exit(2)
# create maintainer users if they don't exist yet
musers = bootstrap_maintainers(tmp_rdb_session)
if musers:
print '++ created new users for maintainers: %r' % (musers,)
tmp_rdb_session.commit()
engine.echo = config.get('db_echo', False)
if not config.get('db_disable_ping'):
event.listen(engine, 'engine_connect', ping_connection)
cookie_secret = config['cookie_secret']
assert cookie_secret
root_path = config.get('root_path', '/')
scm_secure = env_name == 'prod' # https only in prod
scm_mw = SignedCookieMiddleware(secret_key=cookie_secret,
path=root_path,
http_only=True,
secure=scm_secure)
if not scm_secure:
scm_mw.data_expiry = NEVER
def get_engine():
engine = create_engine(config.get('db_url', DEFAULT_DB_URL), pool_recycle=60)
engine.echo = config.get('db_echo', False)
if not config.get('db_disable_ping'):
event.listen(engine, 'engine_connect', ping_connection)
return engine
blank_session_type = sessionmaker()
middlewares = [MessageMiddleware(),
TimingMiddleware(),
scm_mw,
DBSessionMiddleware(blank_session_type, get_engine),
UserMiddleware()]
api_log_path = config.get('api_log_path')
if api_log_path:
log_mw = LoggingMiddleware(api_log_path)
middlewares.insert(0, log_mw)
# hack
config['api_exc_log_path'] = getattr(log_mw, 'exc_log_path', None)
replay_log_path = config.get('replay_log_path')
if replay_log_path:
replay_log_mw = ReplayLogMiddleware(replay_log_path)
middlewares.append(replay_log_mw)
consumer_token = ConsumerToken(config['oauth_consumer_token'],
config['oauth_secret_token'])
resources = {'config': config,
'consumer_token': consumer_token,
'root_path': root_path}
app = Application(routes, resources, middlewares=middlewares)
static_app = StaticApplication(STATIC_PATH)
root_app = Application([StaticFileRoute('/', STATIC_PATH + '/index.html'),
('/', static_app),
('/', app),
('/meta', MetaApplication())])
return root_app
0
Example 122
@classmethod
def register_link_events(cls):
"""Registers sqlalchemy events needed by this mixin.
Call this method after the definition of a model which uses
this mixin class.
"""
event_mapping = {cls.session: lambda x: x.event_new,
cls.contribution: lambda x: x.event_new,
cls.subcontribution: lambda x: x.contribution.event_new,
cls.linked_event: lambda x: x}
type_mapping = {cls.category: LinkType.category,
cls.linked_event: LinkType.event,
cls.session: LinkType.session,
cls.contribution: LinkType.contribution,
cls.subcontribution: LinkType.subcontribution}
def _set_link_type(link_type, target, value, *unused):
if value is not None:
target.link_type = link_type
def _set_event_obj(fn, target, value, *unused):
if value is not None:
event = fn(value)
assert event is not None
target.event_new = event
for rel, fn in event_mapping.iteritems():
if rel is not None:
listen(rel, 'set', partial(_set_event_obj, fn))
for rel, link_type in type_mapping.iteritems():
if rel is not None:
listen(rel, 'set', partial(_set_link_type, link_type))
0
Example 123
Project: sqlalchemy Source File: test_ddlevents.py
def test_metadata(self):
metadata, engine = self.metadata, self.engine
event.listen(metadata, 'before_create', DDL('mxyzptlk'))
event.listen(metadata, 'after_create', DDL('klptzyxm'))
event.listen(metadata, 'before_drop', DDL('xyzzy'))
event.listen(metadata, 'after_drop', DDL('fnord'))
metadata.create_all()
strings = [str(x) for x in engine.mock]
assert 'mxyzptlk' in strings
assert 'klptzyxm' in strings
assert 'xyzzy' not in strings
assert 'fnord' not in strings
del engine.mock[:]
metadata.drop_all()
strings = [str(x) for x in engine.mock]
assert 'mxyzptlk' not in strings
assert 'klptzyxm' not in strings
assert 'xyzzy' in strings
assert 'fnord' in strings
0
Example 124
@classmethod
def register_link_events(cls):
event_mapping = {cls.linked_block: lambda x: x.event_new,
cls.linked_contrib: lambda x: x.event_new,
cls.linked_event: lambda x: x}
type_mapping = {cls.linked_event: VCRoomLinkType.event,
cls.linked_block: VCRoomLinkType.block,
cls.linked_contrib: VCRoomLinkType.contribution}
def _set_link_type(link_type, target, value, *unused):
if value is not None:
target.link_type = link_type
def _set_event_obj(fn, target, value, *unused):
if value is not None:
event = fn(value)
assert event is not None
target.event_new = event
for rel, fn in event_mapping.iteritems():
if rel is not None:
listen(rel, 'set', partial(_set_event_obj, fn))
for rel, link_type in type_mapping.iteritems():
if rel is not None:
listen(rel, 'set', partial(_set_link_type, link_type))
0
Example 125
Project: alchemist Source File: _engine.py
@utils.memoize
def __getitem__(self, name):
if 'DATABASES' not in settings:
raise exceptions.ImproperlyConfigured(
'DATABASES not configured in project settings.')
if name not in settings['DATABASES']:
raise exceptions.ImproperlyConfigured(
'%r not present in DATABASES configuration.' % name)
config = settings['DATABASES'][name]
if isinstance(config, six.string_types):
url = make_url(config)
options = {}
else:
config = dict(map(lambda i: (i[0].lower(), i[1]), config.items()))
options = config.get('options', {})
url = URL(
config['engine'],
username=config.get('username', config.get('user')),
password=config.get('password', config.get('pass')),
host=config.get('hostname', config.get('host')),
port=config.get('port'),
database=config.get('name', config.get('database')))
# If alchemist is invoked by a test runner we should switch to using
# testing databases.
if settings['TESTING']:
if url.drivername.startswith('sqlite'):
# Switch to using an in-memory database for sqlite.
url.database = ':memory:'
else:
# Switch to using a named testing database for other dialects.
ident = threading.current_thread().ident
url.database = 'test_%s_%s' % (url.database, ident)
# Apply MySQL hacks to make MySQL play nice.
pool_size = None
pool_recycle = None
if url.drivername.startswith('mysql'):
pool_size = 10
pool_recycle = 7200
# Get "global" options for the database engine.
pool_size = settings.get('DATABASE_POOL_SIZE', pool_size)
if pool_size:
options.setdefault('pool_size', pool_size)
pool_recycle = settings.get('DATABASE_POOL_RECYCLE', pool_recycle)
if pool_recycle:
options.setdefault('pool_recycle', pool_recycle)
pool_timeout = settings.get('DATABASE_POOL_TIMEOUT')
if pool_timeout:
options.setdefault('pool_timeout', pool_timeout)
# Forward configuration to sqlalchemy and create the engine.
engine = sa.create_engine(url, **options)
if settings["DEBUG"]:
# Create a no-op listener if we're in debug mode.
from sqlalchemy.event import listen
listen(engine, "after_cursor_execute", lambda *a, **kw: None)
# Return the created engine.
return engine
0
Example 126
Project: postgresql-audit Source File: base.py
def attach_table_listeners(self):
for values in self.table_listeners['transaction']:
sa.event.listen(self.transaction_cls.__table__, *values)
for values in self.table_listeners['activity']:
sa.event.listen(self.activity_cls.__table__, *values)
0
Example 127
def register(self):
listen(self.engine, 'before_cursor_execute', self.before_cursor_execute)
listen(self.engine, 'after_cursor_execute', self.after_cursor_execute)
0
Example 128
Project: Perspectives-Server Source File: notary_db.py
def __actual_init(self, dburl=False,
dbname=SUPPORTED_DBS[DEFAULT_DB_TYPE]['defaultdbname'],
dbuser=SUPPORTED_DBS[DEFAULT_DB_TYPE]['defaultusername'],
dbhost=SUPPORTED_DBS[DEFAULT_DB_TYPE]['defaulthostname'],
dbtype=DEFAULT_DB_TYPE,
dbecho=DEFAULT_ECHO,
write_config_file=False, read_config_file=False,
metricsdb=False, metricslog=False):
"""
Initialize a new ndb object.
The actual initialization work is done here to hide the details
of the extra steps we take inside __init__.
"""
connstr = ''
self._Session = None
self.metricsdb = metricsdb
self.metricslog = metricslog
if (dbecho):
dbecho = True
# TODO: ALL INPUT IS EVIL
# regex check these variables
if (dburl):
try:
connstr = os.environ[self.DB_URL_FIELD]
except KeyError:
raise KeyError("There is no environment variable named '%s'" % (self.DB_URL_FIELD))
elif (dbtype in self.SUPPORTED_DBS):
self.DB_PASSWORD = ''
# sqlite doesn't support usernames, passwords, or host.
# since our connstr takes four parameters,
# strip out username, password, and host for sqlite databases;
# otherwise sqlite will create a new db file named
# 'dbuserpassworddbhost' rather than connecting
# to the intended database.
if (dbtype == 'sqlite'):
dbuser = ''
dbhost = ''
else:
try:
self.DB_PASSWORD = os.environ[self.DB_PASSWORD_FIELD]
except KeyError:
# maybe the db has no password.
# let the caller decide and handle it.
pass
connstr = self.SUPPORTED_DBS[dbtype]['connstr'] % (dbuser, self.DB_PASSWORD, dbhost, dbname)
else:
errmsg = "'%s' is not a supported database type" % dbtype
logging.error(errmsg)
raise Exception(errmsg)
# set up sqlalchemy objects
self.db = create_engine(connstr, echo=dbecho)
self._Session = scoped_session(sessionmaker(bind=self.db))
# in most cases we expect callers to handle any exceptions that get thrown here.
# we still want to make sure the error is logged, however.
# for now we only check that (self._Session != None) in a few places that may be called accidentally,
# since callers really shouldn't be calling methods if the database couldn't connect.
# we could add more checks if that's warranted.
try:
ORMBase.metadata.create_all(self.db)
except Exception as e:
logging.error("Database error: '%s'. Could not connect to database! Please check your database status. " % (str(e)))
if (self.DB_PASSWORD_FIELD not in os.environ):
logging.error("The environment variable '{0}' does not exist. Did you mean to specify a database password?".format(self.DB_PASSWORD_FIELD))
raise
listen(Pool, 'checkout', self._on_connection_checkout)
listen(Pool, 'checkin', self._on_connection_checkin)
# cache data used when logging metrics
self.__init_event_types()
if (write_config_file):
self._write_db_config(locals())
0
Example 129
Project: sqlalchemy-continuum Source File: manager.py
def apply_class_configuration_listeners(self, mapper):
"""
Applies class configuration listeners for given mapper.
The listener work in two phases:
1. Class instrumentation phase
The first listeners listens to class instrumentation event and
handles the collecting of versioned models and adds them to
the pending_classes list.
2. After class configuration phase
The second listener listens to after class configuration event and
handles the actual history model generation based on list that
was collected during class instrumenation phase.
:param mapper:
SQLAlchemy mapper to apply the class configuration listeners to
"""
for event_name, listener in self.class_config_listeners.items():
sa.event.listen(mapper, event_name, listener)
0
Example 130
Project: sqlalchemy Source File: test_defaults.py
@classmethod
def define_tables(cls, metadata):
dt = Table('dt', metadata,
Column('id', Integer, primary_key=True, test_needs_autoincrement=True),
Column('col1', String(20)),
Column('col2', String(20),
server_default=sa.schema.FetchedValue()),
Column('col3', String(20),
sa.schema.FetchedValue(for_update=True)),
Column('col4', String(20),
sa.schema.FetchedValue(),
sa.schema.FetchedValue(for_update=True)))
for ins in (
sa.DDL("CREATE TRIGGER dt_ins AFTER INSERT ON dt "
"FOR EACH ROW BEGIN "
"UPDATE dt SET col2='ins', col4='ins' "
"WHERE dt.id = NEW.id; END",
on='sqlite'),
sa.DDL("CREATE TRIGGER dt_ins ON dt AFTER INSERT AS "
"UPDATE dt SET col2='ins', col4='ins' "
"WHERE dt.id IN (SELECT id FROM inserted);",
on='mssql'),
sa.DDL("CREATE TRIGGER dt_ins BEFORE INSERT "
"ON dt "
"FOR EACH ROW "
"BEGIN "
":NEW.col2 := 'ins'; :NEW.col4 := 'ins'; END;",
on='oracle'),
sa.DDL("CREATE TRIGGER dt_ins BEFORE INSERT ON dt "
"FOR EACH ROW BEGIN "
"SET NEW.col2='ins'; SET NEW.col4='ins'; END",
on=lambda ddl, event, target, bind, **kw:
bind.engine.name not in ('oracle', 'mssql', 'sqlite')
),
):
event.listen(dt, 'after_create', ins)
event.listen(dt, 'before_drop', sa.DDL("DROP TRIGGER dt_ins"))
for up in (
sa.DDL("CREATE TRIGGER dt_up AFTER UPDATE ON dt "
"FOR EACH ROW BEGIN "
"UPDATE dt SET col3='up', col4='up' "
"WHERE dt.id = OLD.id; END",
on='sqlite'),
sa.DDL("CREATE TRIGGER dt_up ON dt AFTER UPDATE AS "
"UPDATE dt SET col3='up', col4='up' "
"WHERE dt.id IN (SELECT id FROM deleted);",
on='mssql'),
sa.DDL("CREATE TRIGGER dt_up BEFORE UPDATE ON dt "
"FOR EACH ROW BEGIN "
":NEW.col3 := 'up'; :NEW.col4 := 'up'; END;",
on='oracle'),
sa.DDL("CREATE TRIGGER dt_up BEFORE UPDATE ON dt "
"FOR EACH ROW BEGIN "
"SET NEW.col3='up'; SET NEW.col4='up'; END",
on=lambda ddl, event, target, bind, **kw:
bind.engine.name not in ('oracle', 'mssql', 'sqlite')
),
):
event.listen(dt, 'after_create', up)
event.listen(dt, 'before_drop', sa.DDL("DROP TRIGGER dt_up"))
0
Example 131
def add_listener(self, args):
self.listeners.append(args)
event.listen(*args)
0
Example 132
Project: anitya Source File: __init__.py
def init(db_url, alembic_ini=None, debug=False, create=False):
""" Create the tables in the database using the information from the
url obtained.
:arg db_url, URL used to connect to the database. The URL contains
information with regards to the database engine, the host to
connect to, the user and password and the database name.
ie: <engine>://<user>:<password>@<host>/<dbname>
:kwarg alembic_ini, path to the alembic ini file. This is necessary
to be able to use alembic correctly, but not for the unit-tests.
:kwarg debug, a boolean specifying wether we should have the verbose
output of sqlalchemy or not.
:return a session that can be used to query the database.
"""
engine = create_engine(db_url, echo=debug)
if create:
anitya.lib.model.BASE.metadata.create_all(engine)
# Source: http://docs.sqlalchemy.org/en/latest/dialects/sqlite.html
# see section 'sqlite-foreign-keys'
if db_url.startswith('sqlite:'):
def _fk_pragma_on_connect(dbapi_con, con_record):
dbapi_con.execute("PRAGMA foreign_keys=ON")
sa.event.listen(engine, 'connect', _fk_pragma_on_connect)
if alembic_ini is not None: # pragma: no cover
# then, load the Alembic configuration and generate the
# version table, "stamping" it with the most recent rev:
from alembic.config import Config
from alembic import command
alembic_cfg = Config(alembic_ini)
command.stamp(alembic_cfg, "head")
scopedsession = scoped_session(sessionmaker(bind=engine))
if create:
anitya.lib.plugins.load_plugins(scopedsession)
return scopedsession
0
Example 133
Project: flask-sqlalchemy Source File: test_sqlalchemy.py
def test_listen_to_session_event(self):
app = flask.Flask(__name__)
app.config['TESTING'] = True
db = fsa.SQLAlchemy(app)
sa.event.listen(db.session, 'after_commit', lambda session: None)
0
Example 134
Project: sqlalchemy Source File: test_ddlevents.py
def test_table_standalone(self):
users, engine = self.users, self.engine
event.listen(users, 'before_create', DDL('mxyzptlk'))
event.listen(users, 'after_create', DDL('klptzyxm'))
event.listen(users, 'before_drop', DDL('xyzzy'))
event.listen(users, 'after_drop', DDL('fnord'))
users.create()
strings = [str(x) for x in engine.mock]
assert 'mxyzptlk' in strings
assert 'klptzyxm' in strings
assert 'xyzzy' not in strings
assert 'fnord' not in strings
del engine.mock[:]
users.drop()
strings = [str(x) for x in engine.mock]
assert 'mxyzptlk' not in strings
assert 'klptzyxm' not in strings
assert 'xyzzy' in strings
assert 'fnord' in strings
0
Example 135
@classmethod
def _listen_on_attribute(cls, attribute, coerce, parent_cls):
"""Establish this type as a mutation listener for the given
mapped descriptor.
"""
key = attribute.key
if parent_cls is not attribute.class_:
return
# rely on "propagate" here
parent_cls = attribute.class_
def load(state, *args):
"""Listen for objects loaded or refreshed.
Wrap the target data member's value with
``Mutable``.
"""
val = state.dict.get(key, None)
if val is not None:
if coerce:
val = cls.coerce(key, val)
state.dict[key] = val
val._parents[state.obj()] = key
def set(target, value, oldvalue, initiator):
"""Listen for set/replace events on the target
data member.
Establish a weak reference to the parent object
on the incoming value, remove it for the one
outgoing.
"""
if not isinstance(value, cls):
value = cls.coerce(key, value)
if value is not None:
value._parents[target.obj()] = key
if isinstance(oldvalue, cls):
oldvalue._parents.pop(target.obj(), None)
return value
def pickle(state, state_dict):
val = state.dict.get(key, None)
if val is not None:
if 'ext.mutable.values' not in state_dict:
state_dict['ext.mutable.values'] = []
state_dict['ext.mutable.values'].append(val)
def unpickle(state, state_dict):
if 'ext.mutable.values' in state_dict:
for val in state_dict['ext.mutable.values']:
val._parents[state.obj()] = key
event.listen(parent_cls, 'load', load, raw=True, propagate=True)
event.listen(parent_cls, 'refresh', load, raw=True, propagate=True)
event.listen(attribute, 'set', set, raw=True, retval=True, propagate=True)
event.listen(parent_cls, 'pickle', pickle, raw=True, propagate=True)
event.listen(parent_cls, 'unpickle', unpickle, raw=True, propagate=True)
0
Example 136
Project: kokoropy Source File: strategies.py
def create(self, name_or_url, **kwargs):
# create url.URL object
u = url.make_url(name_or_url)
dialect_cls = u.get_dialect()
if kwargs.pop('_coerce_config', False):
def pop_kwarg(key, default=None):
value = kwargs.pop(key, default)
if key in dialect_cls.engine_config_types:
value = dialect_cls.engine_config_types[key](value)
return value
else:
pop_kwarg = kwargs.pop
dialect_args = {}
# consume dialect arguments from kwargs
for k in util.get_cls_kwargs(dialect_cls):
if k in kwargs:
dialect_args[k] = pop_kwarg(k)
dbapi = kwargs.pop('module', None)
if dbapi is None:
dbapi_args = {}
for k in util.get_func_kwargs(dialect_cls.dbapi):
if k in kwargs:
dbapi_args[k] = pop_kwarg(k)
dbapi = dialect_cls.dbapi(**dbapi_args)
dialect_args['dbapi'] = dbapi
# create dialect
dialect = dialect_cls(**dialect_args)
# assemble connection arguments
(cargs, cparams) = dialect.create_connect_args(u)
cparams.update(pop_kwarg('connect_args', {}))
# look for existing pool or create
pool = pop_kwarg('pool', None)
if pool is None:
def connect():
try:
return dialect.connect(*cargs, **cparams)
except dialect.dbapi.Error as e:
invalidated = dialect.is_disconnect(e, None, None)
util.raise_from_cause(
exc.DBAPIError.instance(
None, None, e, dialect.dbapi.Error,
connection_invalidated=invalidated
)
)
creator = pop_kwarg('creator', connect)
poolclass = pop_kwarg('poolclass', None)
if poolclass is None:
poolclass = dialect_cls.get_pool_class(u)
pool_args = {}
# consume pool arguments from kwargs, translating a few of
# the arguments
translate = {'logging_name': 'pool_logging_name',
'echo': 'echo_pool',
'timeout': 'pool_timeout',
'recycle': 'pool_recycle',
'events': 'pool_events',
'use_threadlocal': 'pool_threadlocal',
'reset_on_return': 'pool_reset_on_return'}
for k in util.get_cls_kwargs(poolclass):
tk = translate.get(k, k)
if tk in kwargs:
pool_args[k] = pop_kwarg(tk)
pool = poolclass(creator, **pool_args)
else:
if isinstance(pool, poollib._DBProxy):
pool = pool.get_pool(*cargs, **cparams)
else:
pool = pool
# create engine.
engineclass = self.engine_cls
engine_args = {}
for k in util.get_cls_kwargs(engineclass):
if k in kwargs:
engine_args[k] = pop_kwarg(k)
_initialize = kwargs.pop('_initialize', True)
# all kwargs should be consumed
if kwargs:
raise TypeError(
"Invalid argument(s) %s sent to create_engine(), "
"using configuration %s/%s/%s. Please check that the "
"keyword arguments are appropriate for this combination "
"of components." % (','.join("'%s'" % k for k in kwargs),
dialect.__class__.__name__,
pool.__class__.__name__,
engineclass.__name__))
engine = engineclass(pool, dialect, u, **engine_args)
if _initialize:
do_on_connect = dialect.on_connect()
if do_on_connect:
def on_connect(dbapi_connection, connection_record):
conn = getattr(
dbapi_connection, '_sqla_unwrap', dbapi_connection)
if conn is None:
return
do_on_connect(conn)
event.listen(pool, 'first_connect', on_connect)
event.listen(pool, 'connect', on_connect)
def first_connect(dbapi_connection, connection_record):
c = base.Connection(engine, connection=dbapi_connection,
_has_events=False)
c._execution_options = util.immutabledict()
dialect.initialize(c)
event.listen(pool, 'first_connect', first_connect, once=True)
return engine
0
Example 137
Project: maraschino Source File: mutable.py
@classmethod
def as_mutable(cls, sqltype):
"""Associate a SQL type with this mutable Python type.
This establishes listeners that will detect ORM mappings against
the given type, adding mutation event trackers to those mappings.
The type is returned, unconditionally as an instance, so that
:meth:`.as_mutable` can be used inline::
Table('mytable', metadata,
Column('id', Integer, primary_key=True),
Column('data', MyMutableType.as_mutable(PickleType))
)
Note that the returned type is always an instance, even if a class
is given, and that only columns which are declared specifically with that
type instance receive additional instrumentation.
To associate a particular mutable type with all occurrences of a
particular type, use the :meth:`.Mutable.associate_with` classmethod
of the particular :meth:`.Mutable` subclass to establish a global
association.
.. warning:: The listeners established by this method are *global*
to all mappers, and are *not* garbage collected. Only use
:meth:`.as_mutable` for types that are permanent to an application,
not with ad-hoc types else this will cause unbounded growth
in memory usage.
"""
sqltype = types.to_instance(sqltype)
def listen_for_type(mapper, class_):
for prop in mapper.iterate_properties:
if hasattr(prop, 'columns'):
if prop.columns[0].type is sqltype:
cls.associate_with_attribute(getattr(class_, prop.key))
event.listen(mapper, 'mapper_configured', listen_for_type)
return sqltype
0
Example 138
Project: sqlalchemy Source File: test_transaction.py
def test_accounting_commit_fails_add(self):
User = self.classes.User
sess = create_session(autocommit=True)
fail = False
def fail_fn(*arg, **kw):
if fail:
raise Exception("commit fails")
event.listen(sess, "after_flush_postexec", fail_fn)
u1 = User(name='ed')
sess.add(u1)
fail = True
assert_raises(
Exception,
sess.flush
)
fail = False
assert u1 not in sess
u1new = User(id=2, name='fred')
sess.add(u1new)
sess.add(u1)
sess.flush()
assert u1 in sess
eq_(
sess.query(User.name).order_by(User.name).all(),
[('ed', ), ('fred',)]
)
0
Example 139
Project: maraschino Source File: deprecated_interfaces.py
@classmethod
def _adapt_listener_methods(cls, self, listener, methods):
for meth in methods:
me_meth = getattr(MapperExtension, meth)
ls_meth = getattr(listener, meth)
if not util.methods_equivalent(me_meth, ls_meth):
if meth == 'reconstruct_instance':
def go(ls_meth):
def reconstruct(instance, ctx):
ls_meth(self, instance)
return reconstruct
event.listen(self.class_manager, 'load',
go(ls_meth), raw=False, propagate=True)
elif meth == 'init_instance':
def go(ls_meth):
def init_instance(instance, args, kwargs):
ls_meth(self, self.class_,
self.class_manager.original_init,
instance, args, kwargs)
return init_instance
event.listen(self.class_manager, 'init',
go(ls_meth), raw=False, propagate=True)
elif meth == 'init_failed':
def go(ls_meth):
def init_failed(instance, args, kwargs):
util.warn_exception(ls_meth, self, self.class_,
self.class_manager.original_init,
instance, args, kwargs)
return init_failed
event.listen(self.class_manager, 'init_failure',
go(ls_meth), raw=False, propagate=True)
else:
event.listen(self, "%s" % meth, ls_meth,
raw=False, retval=True, propagate=True)
0
Example 140
Project: ggrc-core Source File: track_object_state.py
def track_state_for_class(object_class):
event.listen(object_class, 'before_insert', state_before_insert_listener)
event.listen(object_class, 'before_update', state_before_update_listener)
0
Example 141
Project: maraschino Source File: unitofwork.py
def track_cascade_events(descriptor, prop):
"""Establish event listeners on object attributes which handle
cascade-on-set/append.
"""
key = prop.key
def append(state, item, initiator):
# process "save_update" cascade rules for when
# an instance is appended to the list of another instance
sess = session._state_session(state)
if sess:
prop = state.manager.mapper._props[key]
item_state = attributes.instance_state(item)
if prop.cascade.save_update and \
(prop.cascade_backrefs or key == initiator.key) and \
not sess._contains_state(item_state):
sess._save_or_update_state(item_state)
return item
def remove(state, item, initiator):
sess = session._state_session(state)
if sess:
prop = state.manager.mapper._props[key]
# expunge pending orphans
item_state = attributes.instance_state(item)
if prop.cascade.delete_orphan and \
item_state in sess._new and \
prop.mapper._is_orphan(item_state):
sess.expunge(item)
def set_(state, newvalue, oldvalue, initiator):
# process "save_update" cascade rules for when an instance
# is attached to another instance
if oldvalue is newvalue:
return newvalue
sess = session._state_session(state)
if sess:
prop = state.manager.mapper._props[key]
if newvalue is not None:
newvalue_state = attributes.instance_state(newvalue)
if prop.cascade.save_update and \
(prop.cascade_backrefs or key == initiator.key) and \
not sess._contains_state(newvalue_state):
sess._save_or_update_state(newvalue_state)
if oldvalue is not None and prop.cascade.delete_orphan:
oldvalue_state = attributes.instance_state(oldvalue)
if oldvalue_state in sess._new and \
prop.mapper._is_orphan(oldvalue_state):
sess.expunge(oldvalue)
return newvalue
event.listen(descriptor, 'append', append, raw=True, retval=True)
event.listen(descriptor, 'remove', remove, raw=True, retval=True)
event.listen(descriptor, 'set', set_, raw=True, retval=True)
0
Example 142
Project: sqlalchemy Source File: test_ddlevents.py
def test_metadata_drop_both(self):
metadata, bind = self.metadata, self.bind
canary = mock.Mock()
event.listen(metadata, 'before_drop', canary.before_drop)
event.listen(metadata, 'after_drop', canary.after_drop)
metadata.create_all(bind)
metadata.drop_all(bind)
eq_(
canary.mock_calls,
[
mock.call.before_drop(
metadata, self.bind, checkfirst=False,
tables=list(metadata.tables.values()),
_ddl_runner=mock.ANY),
mock.call.after_drop(
metadata, self.bind, checkfirst=False,
tables=list(metadata.tables.values()),
_ddl_runner=mock.ANY),
]
)
0
Example 143
Project: sync-engine Source File: ignition.py
def init_db(engine, key=0):
"""
Make the tables.
This is called only from bin/create-db, which is run during setup.
Previously we allowed this to run everytime on startup, which broke some
alembic revisions by creating new tables before a migration was run.
From now on, we should ony be creating tables+columns via SQLalchemy *once*
and all subsequent changes done via migration scripts.
"""
from inbox.models.base import MailSyncBase
from sqlalchemy import event, DDL
# Hopefully setting auto_increment via an event listener will make it safe
# to execute this function multiple times.
# STOPSHIP(emfree): verify
increment = (key << 48) + 1
for table in MailSyncBase.metadata.tables.values():
event.listen(table, 'after_create',
DDL('ALTER TABLE {tablename} AUTO_INCREMENT={increment}'.
format(tablename=table, increment=increment)))
with disabled_dubiously_many_queries_warning():
MailSyncBase.metadata.create_all(engine)
0
Example 144
def before_all(context):
context.base_url = 'http://localhost:9000'
create_db(use_migrations)
app.debug = False
app.testing = True
if getattr(settings, 'MEMCACHE_MECHANISM', False) is True:
from google.appengine.api import memcache
from google.appengine.ext import testbed
context.testbed = testbed.Testbed()
context.testbed.activate()
context.testbed.init_memcache_stub()
context.query_count = 0
def increment_query_count(conn, clauseelement, multiparams, params):
context.query_count += 1
from sqlalchemy import event
event.listen(db.engine, "before_execute", increment_query_count)
context.server = make_server('', 9000, app)
context.thread = threading.Thread(target=context.server.serve_forever)
context.thread.start()
0
Example 145
@classmethod
def __declare_last__(cls):
for event_name in LISTEN_FOR_EVENTS:
sqlalchemy.event.listen(cls, event_name, cls.event_handler, propagate=False)
0
Example 146
Project: sqlalchemy Source File: test_ddlevents.py
def test_metadata_create_both(self):
metadata, bind = self.metadata, self.bind
canary = mock.Mock()
event.listen(metadata, 'before_create', canary.before_create)
event.listen(metadata, 'after_create', canary.after_create)
metadata.create_all(bind)
metadata.drop_all(bind)
eq_(
canary.mock_calls,
[
mock.call.before_create(
metadata, self.bind, checkfirst=False,
tables=list(metadata.tables.values()),
_ddl_runner=mock.ANY),
mock.call.after_create(
metadata, self.bind, checkfirst=False,
tables=list(metadata.tables.values()),
_ddl_runner=mock.ANY),
]
)
0
Example 147
Project: sqlalchemy Source File: test_ddlevents.py
def test_metadata_table_isolation(self):
metadata, table = self.metadata, self.table
table_canary = mock.Mock()
metadata_canary = mock.Mock()
event.listen(table, 'before_create', table_canary.before_create)
event.listen(metadata, 'before_create', metadata_canary.before_create)
self.table.create(self.bind)
eq_(
table_canary.mock_calls,
[
mock.call.before_create(
table, self.bind, checkfirst=False,
_ddl_runner=mock.ANY, _is_metadata_operation=mock.ANY),
]
)
eq_(
metadata_canary.mock_calls,
[]
)
0
Example 148
Project: sqlalchemy Source File: test_ddlevents.py
def test_conditional_constraint(self):
metadata, users, engine = self.metadata, self.users, self.engine
nonpg_mock = engines.mock_engine(dialect_name='sqlite')
pg_mock = engines.mock_engine(dialect_name='postgresql')
constraint = CheckConstraint('a < b', name='my_test_constraint',
table=users)
# by placing the constraint in an Add/Drop construct, the
# 'inline_ddl' flag is set to False
event.listen(
users,
'after_create',
AddConstraint(constraint).execute_if(dialect='postgresql'),
)
event.listen(
users,
'before_drop',
DropConstraint(constraint).execute_if(dialect='postgresql'),
)
metadata.create_all(bind=nonpg_mock)
strings = ' '.join(str(x) for x in nonpg_mock.mock)
assert 'my_test_constraint' not in strings
metadata.drop_all(bind=nonpg_mock)
strings = ' '.join(str(x) for x in nonpg_mock.mock)
assert 'my_test_constraint' not in strings
metadata.create_all(bind=pg_mock)
strings = ' '.join(str(x) for x in pg_mock.mock)
assert 'my_test_constraint' in strings
metadata.drop_all(bind=pg_mock)
strings = ' '.join(str(x) for x in pg_mock.mock)
assert 'my_test_constraint' in strings
0
Example 149
@classmethod
def __declare_last__(self):
event.listen(self, 'after_insert', self.bitmapist_after_insert)
event.listen(self, 'before_update', self.bitmapist_before_update)
event.listen(self, 'before_delete', self.bitmapist_before_delete)
0
Example 150
Project: DataGristle Source File: metadata.py
def __init__(self, db_dir=None, db_name='metadata.db'):
""" Gets datagristle config, and creates db objects if necessary.
"""
logging.basicConfig(filename='/tmp/datagristle_metadata.log')
logging.getLogger('sqlalchemy.engine').setLevel(logging.DEBUG)
if db_dir is None:
user_data_dir = appdirs.user_data_dir('datagristle')
else:
user_data_dir = db_dir
if not os.path.exists(user_data_dir):
print 'data dir (%s) missing - it will be created' % user_data_dir
os.makedirs(user_data_dir)
self.fqdb_name = os.path.join(user_data_dir, db_name)
self.engine = create_engine('sqlite:////%s' % self.fqdb_name)
def _fk_pragma_on_connect(dbapi_con, con_record):
""" turns foreign key enforcement on"""
dbapi_con.execute('pragma foreign_keys=ON')
event.listen(self.engine, 'connect', _fk_pragma_on_connect)
self.engine.echo = False
self.metadata = MetaData(self.engine)
#-------------------------------------------------------------------
# This explicit connection was not initially needed - originally all
# work was performed through implicit connections. But the need to
# run methods like:
# existing_views = engine.dialect.get_view_names(connect)
# requires the explicit connection.
#-------------------------------------------------------------------
self.connect = self.engine.connect()
self.create_db_tables_declaratively()