Changeset - d36fd56e4b91
[Not reviewed]
0 3 0
Lance Edgar (lance) - 4 years ago 2020-10-13 17:02:57
lance@edbob.org
Allow datasync to export to rattail but *not* record changes

sometimes the "record changes" feature is desirable in all but 1 case, which is
real-time datasync, when such changes are "one way only" and have no need to
boomerang back to the originating system, and trying to do so can result in
misc. race conditions which are better avoided
3 files changed with 10 insertions and 5 deletions:
0 comments (0 inline, 0 general)
rattail/datasync/rattail.py
Show inline comments
 
@@ -318,14 +318,17 @@ class FromRattailToRattailExportConsumer(NewDataSyncImportConsumer):
 
        self.model = self.config.get_model()
 

	
 
    def setup(self):
 
        super(FromRattailToRattailExportConsumer, self).setup()
 
        self.topo_sortkey = make_topo_sortkey(self.model)
 

	
 
    def make_target_session(self):
 
        return Session(bind=self.target_engine)
 

	
 
    def process_changes(self, session, changes):
 
        target_session = Session(bind=self.target_engine)
 
        target_session = self.make_target_session()
 

	
 
        if self.runas_username:
 
            target_session.set_continuum_user(self.runas_username)
 

	
 
        # update all importers with current sessions
 
        for importer in self.importers.values():
rattail/db/__init__.py
Show inline comments
 
@@ -42,23 +42,25 @@ if sqlalchemy:
 
    class SessionBase(orm.Session):
 
        """
 
        Custom SQLAlchemy session class, which adds some convenience methods
 
        related to the SQLAlchemy-Continuum integration.
 
        """
 

	
 
        def __init__(self, rattail_config=None, rattail_record_changes=False, continuum_user=None, **kwargs):
 
        def __init__(self, rattail_config=None, rattail_record_changes=None, continuum_user=None, **kwargs):
 
            """
 
            Custom constructor, to allow specifying the Continuum user at session
 
            creation.  If ``continuum_user`` is specified, its value will be passed
 
            to :meth:`set_continuum_user()`.
 
            """
 
            super(SessionBase, self).__init__(**kwargs)
 
            self.rattail_config = rattail_config
 

	
 
            # maybe record changes
 
            if rattail_record_changes or getattr(self.bind, 'rattail_record_changes', False):
 
            if rattail_record_changes is None:
 
                rattail_record_changes = getattr(self.bind, 'rattail_record_changes', False)
 
            if rattail_record_changes:
 
                from rattail.db.changes import record_changes
 
                record_changes(self, config=self.rattail_config)
 
            else:
 
                self.rattail_record_changes = False
 

	
 
            if continuum_user is None:
 
@@ -84,13 +86,13 @@ if sqlalchemy:
 
                        user = self.query(model.User).filter_by(username=user_info).one()
 
                    except NoResultFound:
 
                        user = None
 
            self.continuum_user = user
 

	
 

	
 
    Session = orm.sessionmaker(class_=SessionBase, rattail_config=None, rattail_record_changes=False, expire_on_commit=False)
 
    Session = orm.sessionmaker(class_=SessionBase, rattail_config=None, expire_on_commit=False)
 

	
 

	
 
else: # no sqlalchemy
 
    Session = None
 

	
 

	
rattail/tests/db/test_init.py
Show inline comments
 
@@ -25,13 +25,13 @@ class TestSession(TestCase):
 
        session = db.Session(rattail_config=config)
 
        self.assertIs(session.rattail_config, config)
 
        session.close()
 

	
 
    def test_init_record_changes(self):
 
        if hasattr(db.Session, 'kw'):
 
            self.assertFalse(db.Session.kw['rattail_record_changes'])
 
            self.assertIsNone(db.Session.kw.get('rattail_record_changes'))
 

	
 
        session = db.Session()
 
        self.assertFalse(session.rattail_record_changes)
 
        session.close()
 

	
 
        session = db.Session(rattail_record_changes=True)
0 comments (0 inline, 0 general)