Skip to content

Commit

Permalink
Kill _conjure_instance() method
Browse files Browse the repository at this point in the history
  • Loading branch information
epandurski committed Feb 10, 2019
1 parent eb16873 commit 84273d9
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 36 deletions.
30 changes: 0 additions & 30 deletions flask_signalbus/atomic.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,36 +45,6 @@ def _get_pk_values(cls, instance_or_pk):
instance_or_pk = inspect(cls).primary_key_from_instance(instance_or_pk)
return instance_or_pk if isinstance(instance_or_pk, tuple) else (instance_or_pk,)

@classmethod
def _conjure_instance(cls, *args, **kwargs):
"""Continuously try to create an instance, flush it to the database, and return it.
This is useful, for example, when a constructor is defined
that generates a random primary key, which is not guaranteed
to be unique.
Note: This method uses database savepoints to recover after
unsuccessful database flush. It will not work correctly on
databases that do not support savepoints. Also, on every
unsuccessful flush, the transaction will be rolled back to a
savepoint, which will expire all objects in the session.
"""

session = cls._flask_signalbus_sa.session
tries = kwargs.pop('__tries', 50)
for _ in range(tries):
instance = cls(*args, **kwargs)
session.begin_nested()
session.add(instance)
try:
session.commit()
except IntegrityError:
session.rollback()
continue
return instance
raise RuntimeError('Can not conjure a "{}" instance.'.format(cls.__name__))


class AtomicProceduresMixin(object):
"""Adds utility functions to :class:`~flask_sqlalchemy.SQLAlchemy` and the declarative base.
Expand Down
6 changes: 0 additions & 6 deletions tests/test_atomic.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,3 @@ def test_get_pk_values(atomic_db, AtomicModel):
assert AtomicModel._get_pk_values(o) == (o.id,)
assert AtomicModel._get_pk_values(1) == (1,)
assert AtomicModel._get_pk_values((1,)) == (1,)


@pytest.mark.skip('SQLite does not support savepoints')
def test_create_sharding_key(ShardingKey):
id_ = ShardingKey._conjure_instance().id
assert type(id_) is int

0 comments on commit 84273d9

Please sign in to comment.