Commits

Rufus Pollock committed 53d27ba

[changeset,sqlalchemy/changeset][s]: make object_id and data on ChangeObject into json but handle the issue that we need lists in some case to be tuples so we can hash them (object_id is used a dictionary key).

  • Participants
  • Parent commits b089bef
  • Branches changeset

Comments (0)

Files changed (5)

File vdm/__init__.py

 '''
 __version__ = '0.8'
 __description__ = 'A versioned domain model framework.'
+
+try:
+    import json
+except ImportError:
+    import simplejson as json
+

File vdm/changeset.py

+from vdm import json
 import hashlib
 
 class Changeset(object):
         self.data_type = self.DataType.FULL
         self.operation_type = self.OperationType.CREATE
         self.data = None
+        self.object_id = None
 
     @property
     def hash(self):
-        data_to_hash = self.operation_type + (self.data or '')
+        data_to_hash = self.operation_type
+        data_to_hash += '::' + json.dumps(self.object_id, sort_keys=True)
+        data_to_hash += '::' + json.dumps(self.data, sort_keys=True)
         return hashlib.sha1(data_to_hash).hexdigest()
 

File vdm/sqlalchemy/changeset.py

     def copy(self):
         return JsonType(self.impl.length)
 
+class JsonTypeTuple(JsonType):
+    def process_result_value(self, value, engine):
+        if value is None:
+            return None
+        else:
+            out = json.loads(value)
+            if isinstance(out, list):
+                out = tuple(out)
+            return out
+
+    def copy(self):
+        return JsonTypeTuple(self.impl.length)
+
 
 class Changeset(_Changeset):
-    pass
+    @classmethod
+    def youngest(self, session):
+        '''Get the youngest (most recent) changeset.
+
+        If session is not provided assume there is a contextual session.
+        '''
+        q = session.query(self)
+        return q.first()
 
 class ChangeObject(_ChangeObject):
     pass
     change_object_table = Table('changeset_object', metadata,
             Column('changeset_id', String(40), ForeignKey('changeset.id'),
                 primary_key=True),
-            Column('object_id', UnicodeText, primary_key=True),
-            Column('operation_type', String(30), primary_key=True),
-            Column('data_type', String(30), primary_key=True),
-            Column('data', UnicodeText, primary_key=True),
+            Column('object_id', JsonTypeTuple, primary_key=True),
+            Column('operation_type', String(30)),
+            Column('data_type', String(30)),
+            Column('data', JsonType),
             )
 
     return (changeset_table, change_object_table)
 
 def setup_changeset(metadata, mapper):
     changeset_table, change_object_table = make_tables(metadata)
+
     mapper(Changeset, changeset_table, properties={
         'manifest': relationship(ChangeObject, backref='changeset',
             collection_class=column_mapped_collection(change_object_table.c.object_id))
-        })
+        },
+        order_by=changeset_table.c.timestamp.desc()
+        )
+
     mapper(ChangeObject, change_object_table, properties={
         })
 

File vdm/test/sqlalchemy/test_changeset.py

 
     def test_02(self):
         co = ChangeObject()
-        objectid = json.dumps((u'package', 1,2,3))
+        objectid = (u'package', 1,2,3)
         co.object_id = objectid
         co.operation = ChangeObject.OperationType.CREATE
-        co.data = json.dumps({
+        co.data = {
             'field1': 'aaaaaa',
             'field2': 'bbbbbb'
-            }, sort_keys=True)
+            }
         cs = Changeset()
         cs.manifest[co.object_id] = co
         cs.save()
         
-        cs_id = '148c918ee2b4b027eda28601f28a30138927f76f'
-        assert cs.id == cs_id
+        cs_id = '2a58c78bd6c17d49ffb1fe1afb2d09a93fdd5b2a'
+        assert cs.id == cs_id, cs.id
         assert len(cs.manifest) == 1
         assert cs.manifest[objectid] == co
         

File vdm/test/test_changeset.py

         cs.manifest[co.object_id] = co
         cs.save()
 
-        assert cs.id == '148c918ee2b4b027eda28601f28a30138927f76f', cs.id
+        assert cs.id == '0925a971dc9d0ad3fdf1f6fca7d26ce6bb5e6f95', cs.id
         assert len(cs.manifest) == 1
         assert cs.manifest[objectid] == co