def _index_query(self, obj):
        """
        Returns the query needed for fetching the index of this record relative
        to version history.
        """
        alias = sa.orm.aliased(obj)

        subquery = (
            sa.select([sa.func.count('1')], from_obj=[alias.__table__])
            .where(
                getattr(alias, tx_column_name(obj))
                <
                getattr(obj, tx_column_name(obj))
            )
            .correlate(alias.__table__)
            .label('position')
        )
        query = (
            sa.select([subquery], from_obj=[obj.__table__])
            .where(
                sa.and_(
                    *map(eq, zip(identity(obj.__class__), identity(obj)))
                )
            )
            .order_by(
                getattr(obj.__class__, tx_column_name(obj))
            )
        )
        return query
    def get_or_create_version_object(self, target):
        """
        Return version object for given parent object. If no version object
        exists for given parent object, create one.

        :param target: Parent object to create the version object for
        """
        version_cls = version_class(target.__class__)
        version_id = identity(target) + (self.current_transaction.id, )
        version_key = (version_cls, version_id)

        if version_key not in self.version_objs:
            version_obj = version_cls()
            self.version_objs[version_key] = version_obj
            self.version_session.add(version_obj)
            tx_column = self.manager.option(
                target,
                'transaction_column_name'
            )
            setattr(
                version_obj,
                tx_column,
                self.current_transaction.id
            )
            return version_obj
        else:
            return self.version_objs[version_key]
    def get_or_create_version_object(self, target):
        """
        Return version object for given parent object. If no version object
        exists for given parent object, create one.

        :param target: Parent object to create the version object for
        """
        version_cls = version_class(target.__class__)
        version_id = identity(target) + (self.current_transaction.id, )
        version_key = (version_cls, version_id)

        if version_key not in self.version_objs:
            version_obj = version_cls()
            self.version_objs[version_key] = version_obj
            self.version_session.add(version_obj)
            tx_column = self.manager.option(
                target,
                'transaction_column_name'
            )
            setattr(
                version_obj,
                tx_column,
                self.current_transaction.id
            )
            return version_obj
        else:
            return self.version_objs[version_key]
 def format_key(self, target):
     # We cannot use target._sa_instance_state.identity here since object's
     # identity is not yet updated at this phase
     return (target.__class__, identity(target))
Exemple #5
0
 def format_key(self, target):
     # We cannot use target._sa_instance_state.identity here since object's
     # identity is not yet updated at this phase
     return (target.__class__, identity(target))