- Fixed two issues in the "history_meta" example where history tracking

could encounter empty history, and where a column keyed to an alternate
attribute name would fail to track properly.  Fixes courtesy
Alex Fraser.

(cherry picked from commit d57e5edbcd)
This commit is contained in:
Mike Bayer
2015-08-24 10:54:09 -04:00
parent 2759ec3e8a
commit 4bd1c615af
3 changed files with 78 additions and 4 deletions
+9
View File
@@ -18,6 +18,15 @@
.. changelog::
:version: 1.0.9
.. change::
:tags: bug, examples
:versions: 1.1.0b1
Fixed two issues in the "history_meta" example where history tracking
could encounter empty history, and where a column keyed to an alternate
attribute name would fail to track properly. Fixes courtesy
Alex Fraser.
.. change::
:tags: bug, orm
:tickets: 3510
+4 -4
View File
@@ -210,13 +210,13 @@ def create_version(obj, session, deleted=False):
a, u, d = attributes.get_history(obj, prop.key)
if d:
attr[hist_col.key] = d[0]
attr[prop.key] = d[0]
obj_changed = True
elif u:
attr[hist_col.key] = u[0]
else:
attr[prop.key] = u[0]
elif a:
# if the attribute had no value.
attr[hist_col.key] = a[0]
attr[prop.key] = a[0]
obj_changed = True
if not obj_changed:
@@ -614,3 +614,68 @@ class TestVersioning(TestCase, AssertsCompiledSQL):
sess.commit()
assert sc.version == 1
def test_create_double_flush(self):
class SomeClass(Versioned, self.Base, ComparableEntity):
__tablename__ = 'sometable'
id = Column(Integer, primary_key=True)
name = Column(String(30))
other = Column(String(30))
self.create_tables()
sc = SomeClass()
self.session.add(sc)
self.session.flush()
sc.name = 'Foo'
self.session.flush()
assert sc.version == 2
def test_mutate_plain_column(self):
class Document(self.Base, Versioned):
__tablename__ = 'document'
id = Column(Integer, primary_key=True, autoincrement=True)
name = Column(String, nullable=True)
description_ = Column('description', String, nullable=True)
self.create_tables()
document = Document()
self.session.add(document)
document.name = 'Foo'
self.session.commit()
document.name = 'Bar'
self.session.commit()
DocumentHistory = Document.__history_mapper__.class_
v2 = self.session.query(Document).one()
v1 = self.session.query(DocumentHistory).one()
self.assertEqual(v1.id, v2.id)
self.assertEqual(v2.name, 'Bar')
self.assertEqual(v1.name, 'Foo')
def test_mutate_named_column(self):
class Document(self.Base, Versioned):
__tablename__ = 'document'
id = Column(Integer, primary_key=True, autoincrement=True)
name = Column(String, nullable=True)
description_ = Column('description', String, nullable=True)
self.create_tables()
document = Document()
self.session.add(document)
document.description_ = 'Foo'
self.session.commit()
document.description_ = 'Bar'
self.session.commit()
DocumentHistory = Document.__history_mapper__.class_
v2 = self.session.query(Document).one()
v1 = self.session.query(DocumentHistory).one()
self.assertEqual(v1.id, v2.id)
self.assertEqual(v2.description_, 'Bar')
self.assertEqual(v1.description_, 'Foo')