Changeset - 0a94d1215d2b
[Not reviewed]
0 3 0
Lance Edgar (lance) - 12 years ago 2012-09-28 09:52:52
lance@edbob.org
tweak db sync
3 files changed with 17 insertions and 7 deletions:
0 comments (0 inline, 0 general)
rattail/db/__init__.py
Show inline comments
 
@@ -46,12 +46,20 @@ def before_flush(session, flush_context, instances):
 
    """
 

	
 
    def record_change(instance, deleted=False):
 
        if instance.__class__ is rattail.Change:
 

	
 
        # No need to record changes for Change. :)
 
        if isinstance(instance, rattail.Change):
 
            return
 

	
 
        # Ignore instances which don't use UUID.
 
        if not hasattr(instance, 'uuid'):
 
            return
 

	
 
        # Provide an UUID value, if necessary.
 
        if not instance.uuid:
 
            instance.uuid = edbob.get_uuid()
 

	
 
        # Record the change.
 
        change = session.query(rattail.Change).get(
 
            (instance.__class__.__name__, instance.uuid))
 
        if not change:
rattail/db/sync/__init__.py
Show inline comments
 
@@ -94,7 +94,7 @@ def synchronize_changes(engines):
 
        local_changes = local_session.query(rattail.Change)
 

	
 
        if local_changes.count():
 
            log.debug('synchronize_changes: found %d changes' % local_changes.count())
 
            log.debug("synchronize_changes: found %d changes" % local_changes.count())
 

	
 
            class_names = []
 
            for class_name in local_session.query(rattail.Change.class_name.distinct()):
 
@@ -109,6 +109,7 @@ def synchronize_changes(engines):
 
            for class_name in class_names:
 

	
 
                for change in local_changes.filter_by(class_name=class_name):
 
                    log.debug("synchronize_changes: processing change: %s" % change)
 
                    cls = getattr(edbob, change.class_name)
 

	
 
                    if change.deleted:
 
@@ -120,9 +121,10 @@ def synchronize_changes(engines):
 

	
 
                    else: # new/dirty
 
                        local_instance = local_session.query(cls).get(change.uuid)
 
                        for remote_session in remote_sessions:
 
                            remote_session.merge(local_instance)
 
                            remote_session.flush()
 
                        if local_instance:
 
                            for remote_session in remote_sessions:
 
                                remote_session.merge(local_instance)
 
                                remote_session.flush()
 

	
 
                    local_session.delete(change)
 
                    local_session.flush()
rattail/db/sync/win32.py
Show inline comments
 
@@ -60,12 +60,12 @@ class DatabaseSynchronizerService(Service):
 
        if not Service.Initialize(self):
 
            return False
 

	
 
        edbob.init_modules(['rattail.db'])
 

	
 
        engines = get_sync_engines()
 
        if not engines:
 
            return False
 

	
 
        edbob.init_modules(['rattail.db'])
 

	
 
        thread = threading.Thread(target=synchronize_changes,
 
                                  args=(engines,))
 
        thread.daemon = True
0 comments (0 inline, 0 general)