From 395d155109ec767408d656381545b884866f55ab Mon Sep 17 00:00:00 2001 From: Harris Jordan Date: Tue, 27 Jul 2021 14:36:50 -0400 Subject: [PATCH 1/4] merge the before commit and after begin hooks due to the fact that they can happen out of order --- sqlalchemy_continuum/manager.py | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/sqlalchemy_continuum/manager.py b/sqlalchemy_continuum/manager.py index 99ace2c1..525cd323 100644 --- a/sqlalchemy_continuum/manager.py +++ b/sqlalchemy_continuum/manager.py @@ -138,7 +138,6 @@ def reset(self): 'after_commit': self.clear, 'after_rollback': self.clear, # The below are only used by native versioning - 'after_begin': self.after_begin, 'before_commit': self.before_commit, } self.mapper_listeners = { @@ -159,17 +158,6 @@ def reset(self): self.metadata = None - def after_begin(self, session, tx, conn): - if not self.options['versioning'] or not self.options['native_versioning']: - return - - tx_table = self.transaction_cls.__table__ - - stmt = tx_table.insert().values(issued_at=datetime.utcnow()).returning(tx_table.c.id) - - # result of fetchone() is a Tuple[int] - self.native_transaction_id = session.execute(stmt).fetchone()[0] - def transaction_args(self, session): args = {} for plugin in self.plugins: @@ -182,11 +170,17 @@ def before_commit(self, session): tx_table = self.transaction_cls.__table__ + fetch_transaction_id_statement = tx_table.insert().values(issued_at=datetime.utcnow()).returning(tx_table.c.id) + + # result of fetchone() is a Tuple[int] + self.native_transaction_id = session.execute(fetch_transaction_id_statement).fetchone()[0] + stmt = sa.update( table=tx_table, whereclause=(tx_table.c.id == self.native_transaction_id), values={key: value for (key, value) in list(self.transaction_args(session).items())} ) + session.execute(stmt) def create_transaction_model(self): From b67c1f1038a69f278acf527d11005b062524fbf1 Mon Sep 17 00:00:00 2001 From: Harris Jordan Date: Tue, 27 Jul 2021 16:14:08 -0400 Subject: [PATCH 2/4] check if native_transaction_id exists in before_commit hook --- sqlalchemy_continuum/manager.py | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/sqlalchemy_continuum/manager.py b/sqlalchemy_continuum/manager.py index 525cd323..fe50f8ea 100644 --- a/sqlalchemy_continuum/manager.py +++ b/sqlalchemy_continuum/manager.py @@ -138,6 +138,7 @@ def reset(self): 'after_commit': self.clear, 'after_rollback': self.clear, # The below are only used by native versioning + 'after_begin': self.after_begin, 'before_commit': self.before_commit, } self.mapper_listeners = { @@ -158,6 +159,19 @@ def reset(self): self.metadata = None + def after_begin(self, session, _tx, _conn): + if not self.options['versioning'] or not self.options['native_versioning']: + return + + tx_table = self.transaction_cls.__table__ + + stmt = tx_table.insert().values(issued_at=datetime.utcnow()).returning(tx_table.c.id) + + # result of fetchone() is a Tuple[int] + self.native_transaction_id = session.execute(stmt).fetchone()[0] + + self.after_begin_called = True + def transaction_args(self, session): args = {} for plugin in self.plugins: @@ -170,19 +184,20 @@ def before_commit(self, session): tx_table = self.transaction_cls.__table__ - fetch_transaction_id_statement = tx_table.insert().values(issued_at=datetime.utcnow()).returning(tx_table.c.id) - # result of fetchone() is a Tuple[int] - self.native_transaction_id = session.execute(fetch_transaction_id_statement).fetchone()[0] + if not self.native_transaction_id: + stmt = tx_table.insert().values(issued_at=datetime.utcnow()).returning(tx_table.c.id) + # result of fetchone() is a Tuple[int] + self.native_transaction_id = session.execute(stmt).fetchone()[0] stmt = sa.update( table=tx_table, whereclause=(tx_table.c.id == self.native_transaction_id), values={key: value for (key, value) in list(self.transaction_args(session).items())} ) - session.execute(stmt) + def create_transaction_model(self): """ Create Transaction class but only if it doesn't already exist in From 308f2037c59e64d384fb5b345fd2759ec6425fb5 Mon Sep 17 00:00:00 2001 From: Harris Jordan Date: Tue, 27 Jul 2021 16:33:09 -0400 Subject: [PATCH 3/4] remove old code --- sqlalchemy_continuum/manager.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/sqlalchemy_continuum/manager.py b/sqlalchemy_continuum/manager.py index fe50f8ea..d468e09d 100644 --- a/sqlalchemy_continuum/manager.py +++ b/sqlalchemy_continuum/manager.py @@ -159,7 +159,7 @@ def reset(self): self.metadata = None - def after_begin(self, session, _tx, _conn): + def after_begin(self, session, tx, conn): if not self.options['versioning'] or not self.options['native_versioning']: return @@ -170,8 +170,6 @@ def after_begin(self, session, _tx, _conn): # result of fetchone() is a Tuple[int] self.native_transaction_id = session.execute(stmt).fetchone()[0] - self.after_begin_called = True - def transaction_args(self, session): args = {} for plugin in self.plugins: From 70da20d86397fb66726345b7e5c5e663e17f84fb Mon Sep 17 00:00:00 2001 From: Harris Jordan Date: Wed, 28 Jul 2021 10:06:18 -0400 Subject: [PATCH 4/4] update version --- sqlalchemy_continuum/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sqlalchemy_continuum/__init__.py b/sqlalchemy_continuum/__init__.py index 2352c3db..c48e7fe1 100644 --- a/sqlalchemy_continuum/__init__.py +++ b/sqlalchemy_continuum/__init__.py @@ -20,7 +20,7 @@ ) -__version__ = '1.4.2' +__version__ = '1.4.3' versioning_manager = VersioningManager()