__init__.py :  » Database » SQLAlchemy » SQLAlchemy-0.6.0 » examples » versioning » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Database » SQLAlchemy 
SQLAlchemy » SQLAlchemy 0.6.0 » examples » versioning » __init__.py
"""
Illustrates an extension which creates version tables for entities and stores records for each change.  The same idea as Elixir's versioned extension, but more efficient (uses attribute API to get history) and handles class inheritance.  The given extensions generate an anonymous "history" class which represents historical versions of the target object.   

Usage is illustrated via a unit test module ``test_versioning.py``, which can be run via nose::

    nosetests -w examples/versioning/

A fragment of example usage, using declarative::

    from history_meta import VersionedMeta, VersionedListener

    Base = declarative_base(metaclass=VersionedMeta, bind=engine)
    Session = sessionmaker(extension=VersionedListener())

    class SomeClass(Base):
        __tablename__ = 'sometable'
    
        id = Column(Integer, primary_key=True)
        name = Column(String(50))
    
        def __eq__(self, other):
            assert type(other) is SomeClass and other.id == self.id
        
    sess = Session()
    sc = SomeClass(name='sc1')
    sess.add(sc)
    sess.commit()

    sc.name = 'sc1modified'
    sess.commit()

    assert sc.version == 2

    SomeClassHistory = SomeClass.__history_mapper__.class_

    assert sess.query(SomeClassHistory).\\
                filter(SomeClassHistory.version == 1).\\
                all() \\
                == [SomeClassHistory(version=1, name='sc1')]

To apply ``VersionedMeta`` to a subset of classes (probably more typical), the metaclass can be applied on a per-class basis::

    from history_meta import VersionedMeta, VersionedListener

    Base = declarative_base(bind=engine)

    class SomeClass(Base):
        __tablename__ = 'sometable'

        # ...

    class SomeVersionedClass(Base):
        __metaclass__ = VersionedMeta
        __tablename__ = 'someothertable'

        # ...

The ``VersionedMeta`` is a declarative metaclass - to use the extension with plain mappers, the ``_history_mapper`` function can be applied::

    from history_meta import _history_mapper

    m = mapper(SomeClass, sometable)
    _history_mapper(m)

    SomeHistoryClass = SomeClass.__history_mapper__.class_

"""
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.