Пример #1
0
    def test_explicit_expunge_deleted(self):
        users, User = self.tables.users, self.classes.User

        mapper(User, users)
        sess = Session()
        sess.add(User(name='x'))
        sess.commit()

        u1 = sess.query(User).first()
        sess.delete(u1)

        sess.flush()

        assert was_deleted(u1)
        assert u1 not in sess
        assert object_session(u1) is sess

        sess.expunge(u1)
        assert was_deleted(u1)
        assert u1 not in sess
        assert object_session(u1) is None

        sess.rollback()
        assert was_deleted(u1)
        assert u1 not in sess
        assert object_session(u1) is None
Пример #2
0
    def test_explicit_expunge_deleted(self):
        users, User = self.tables.users, self.classes.User

        mapper(User, users)
        sess = Session()
        sess.add(User(name='x'))
        sess.commit()

        u1 = sess.query(User).first()
        sess.delete(u1)

        sess.flush()

        assert was_deleted(u1)
        assert u1 not in sess
        assert object_session(u1) is sess

        sess.expunge(u1)
        assert was_deleted(u1)
        assert u1 not in sess
        assert object_session(u1) is None

        sess.rollback()
        assert was_deleted(u1)
        assert u1 not in sess
        assert object_session(u1) is None
Пример #3
0
    def get_unique(cls, **kwargs):
        g._unique_cache = cache = getattr(g, '_unique_cache', {})
        key = (cls, tuple(kwargs.items()))
        o = cache.get(key)

        if o is not None and was_deleted(o):
            o = None

        if o is None:
            session = current_app.extensions['sqlalchemy'].db.session

            with session.no_autoflush:
                o = session.query(cls).filter_by(**kwargs).first()

            if o is None:
                o = cls.create_unique(session, **kwargs)

            cache[key] = o

        return o
Пример #4
0
    def get_unique(cls, **kwargs):
        g._unique_cache = cache = getattr(g, '_unique_cache', {})
        key = (cls, tuple(kwargs.items()))
        o = cache.get(key)

        if o is not None and was_deleted(o):
            o = None

        if o is None:
            session = current_app.extensions['sqlalchemy'].db.session

            with session.no_autoflush:
                o = session.query(cls).filter_by(**kwargs).first()

            if o is None:
                o = cls(**kwargs)
                session.add(o)

            cache[key] = o

        return o
Пример #5
0
    def get_unique(cls, **kwargs):
        """Keep a cache of unique instances in memory so new instances
        can be safely created in bulk before they are committed.
        """
        g._unique_cache = cache = getattr(g, '_unique_cache', {})
        key = (cls, tuple(kwargs.items()))
        o = cache.get(key)

        if o is not None and was_deleted(o):
            o = None

        if o is None:
            session = current_app.extensions['sqlalchemy'].db.session

            with session.no_autoflush:
                o = session.query(cls).filter_by(**kwargs).first()

            if o is None:
                o = cls.create_unique(session, **kwargs)

            cache[key] = o

        return o