From: Mike Bayer Date: Mon, 24 Aug 2015 14:54:09 +0000 (-0400) Subject: - Fixed two issues in the "history_meta" example where history tracking X-Git-Tag: rel_1_1_0b1~84^2~70^2~131 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=d57e5edbcdf915168c613cdd6da0bd7bea877fa4;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - 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. --- diff --git a/doc/build/changelog/changelog_10.rst b/doc/build/changelog/changelog_10.rst index ad88052995..a9a3486c15 100644 --- a/doc/build/changelog/changelog_10.rst +++ b/doc/build/changelog/changelog_10.rst @@ -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 diff --git a/examples/versioned_history/history_meta.py b/examples/versioned_history/history_meta.py index 6d7b137eb9..866f2d473a 100644 --- a/examples/versioned_history/history_meta.py +++ b/examples/versioned_history/history_meta.py @@ -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: diff --git a/examples/versioned_history/test_versioning.py b/examples/versioned_history/test_versioning.py index dde73a5ae6..3ea240e113 100644 --- a/examples/versioned_history/test_versioning.py +++ b/examples/versioned_history/test_versioning.py @@ -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')