Skip to content

Commit

Permalink
Fix native versioning trigger syncing
Browse files Browse the repository at this point in the history
  • Loading branch information
kvesteri committed Oct 7, 2014
1 parent e642863 commit ef92461
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 30 deletions.
9 changes: 8 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ Changelog
Here you can see the full list of changes between each SQLAlchemy-Continuum release.


1.1.0 (2014-09-xx)
1.1.1 (2014-10-07)
^^^^^^^^^^^^^^^^^^

- Fixed native versioning trigger syncing


1.1.0 (2014-10-02)
^^^^^^^^^^^^^^^^^^

- Added Python 3.4 to test suite
Expand All @@ -17,6 +23,7 @@ Here you can see the full list of changes between each SQLAlchemy-Continuum rele
- Fixed version model building when no versioned models were found (previously threw AttributeError)
- Replaced plugin template methods before_create_tx_object and after_create_tx_object with transaction_args to better cope with native versioning


1.0.3 (2014-07-16)
^^^^^^^^^^^^^^^^^^

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def run(self):

setup(
name='SQLAlchemy-Continuum',
version='1.1.0',
version='1.1.1',
url='https://github.com/kvesteri/sqlalchemy-continuum',
license='BSD',
author='Konsta Vesterinen',
Expand Down
14 changes: 1 addition & 13 deletions sqlalchemy_continuum/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
)


__version__ = '1.1.0'
__version__ = '1.1.1'


versioning_manager = VersioningManager()
Expand Down Expand Up @@ -69,12 +69,6 @@ def make_versioned(
'before_cursor_execute',
manager.track_association_operations
)
if manager.options['native_versioning']:
sa.event.listen(
sa.pool.Pool,
'connect',
manager.on_connect
)


def remove_versioning(
Expand Down Expand Up @@ -102,9 +96,3 @@ def remove_versioning(
'before_cursor_execute',
manager.track_association_operations
)
if manager.options['native_versioning']:
sa.event.remove(
sa.pool.Pool,
'connect',
manager.on_connect
)
10 changes: 7 additions & 3 deletions sqlalchemy_continuum/dialects/postgresql.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@
CREATE OR REPLACE FUNCTION {procedure_name}() RETURNS TRIGGER AS $$
DECLARE transaction_id_value INT;
BEGIN
transaction_id_value = (SELECT id FROM temporary_transaction);
BEGIN
transaction_id_value = (SELECT id FROM temporary_transaction);
EXCEPTION WHEN others THEN
RETURN NEW;
END;
IF transaction_id_value IS NULL THEN
RETURN NEW;
END IF;
Expand Down Expand Up @@ -483,7 +487,7 @@ def sync_trigger(conn, table_name):
set(c.name for c in parent_table.c) -
set(c.name for c in version_table.c if not c.name.endswith('_mod'))
)
drop_trigger(conn, table_name)
drop_trigger(conn, parent_table.name)
create_trigger(conn, table=parent_table, excluded_columns=excluded_columns)


Expand Down Expand Up @@ -512,7 +516,7 @@ def create_trigger(


def drop_trigger(conn, table_name):
conn.execute('DROP FUNCTION IF EXISTS %s_audit()' % table_name)
conn.execute(
'DROP TRIGGER IF EXISTS %s_trigger ON %s' % (table_name, table_name)
)
conn.execute('DROP FUNCTION IF EXISTS %s_audit()' % table_name)
8 changes: 0 additions & 8 deletions sqlalchemy_continuum/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,14 +313,6 @@ def unit_of_work(self, session):
self.units_of_work[conn] = uow
return uow

def on_connect(self, dbapi_conn, connection_record):
from .dialects.postgresql import CreateTemporaryTransactionTableSQL

cursor = dbapi_conn.cursor()
cursor.execute(str(CreateTemporaryTransactionTableSQL()))
dbapi_conn.commit()
cursor.close()

def before_flush(self, session, flush_context, instances):
"""
Before flush listener for SQLAlchemy sessions. If this manager has
Expand Down
9 changes: 5 additions & 4 deletions tests/dialects/test_triggers.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,13 @@ def teardown_method(self, method):

def test_sync_triggers(self):
sync_trigger(self.connection, 'article_version')
assert 'DROP FUNCTION ' in QueryPool.queries[-4]
assert 'DROP TRIGGER ' in QueryPool.queries[-3]
assert 'DROP TRIGGER ' in QueryPool.queries[-4]
assert 'DROP FUNCTION ' in QueryPool.queries[-3]
assert 'CREATE OR REPLACE FUNCTION ' in QueryPool.queries[-2]
assert 'CREATE TRIGGER ' in QueryPool.queries[-1]
sync_trigger(self.connection, 'article_version')

def test_drop_triggers(self):
drop_trigger(self.connection, 'article')
assert 'DROP FUNCTION ' in QueryPool.queries[-2]
assert 'DROP TRIGGER ' in QueryPool.queries[-1]
assert 'DROP TRIGGER ' in QueryPool.queries[-2]
assert 'DROP FUNCTION ' in QueryPool.queries[-1]

0 comments on commit ef92461

Please sign in to comment.