From d36fd56e4b91e714aec9900d03b575cc4233290f 2020-10-13 17:02:57 From: Lance Edgar Date: 2020-10-13 17:02:57 Subject: [PATCH] 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 --- diff --git a/rattail/datasync/rattail.py b/rattail/datasync/rattail.py index 42c16e924e637a3cf8f847eb383b59067f72c234..dccd3cc7f4e3b6fd377f82c5cee47b55e978940b 100644 --- a/rattail/datasync/rattail.py +++ b/rattail/datasync/rattail.py @@ -321,8 +321,11 @@ class FromRattailToRattailExportConsumer(NewDataSyncImportConsumer): 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) diff --git a/rattail/db/__init__.py b/rattail/db/__init__.py index e36cdc41055bedd75b75cf029962b0baf9d5cc84..02293e729ad5fa7a9ade3b03e0322b45545c53de 100644 --- a/rattail/db/__init__.py +++ b/rattail/db/__init__.py @@ -45,7 +45,7 @@ if sqlalchemy: 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 @@ -55,7 +55,9 @@ if sqlalchemy: 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: @@ -87,7 +89,7 @@ if sqlalchemy: 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 diff --git a/rattail/tests/db/test_init.py b/rattail/tests/db/test_init.py index ffc14f652d829bafeae8111b2928eb68182fab20..6a08b46034e431ea6c8b490ac5c049b63fb98fc2 100644 --- a/rattail/tests/db/test_init.py +++ b/rattail/tests/db/test_init.py @@ -28,7 +28,7 @@ class TestSession(TestCase): 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)