Skip to content

Commit

Permalink
fix: version_table unmapper orm object issue
Browse files Browse the repository at this point in the history
  • Loading branch information
indiVar0508 committed Aug 29, 2022
1 parent aa69cd8 commit ee3b86c
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 12 deletions.
4 changes: 2 additions & 2 deletions sqlalchemy_continuum/expression_reflector.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import sqlalchemy as sa
from sqlalchemy.sql.expression import bindparam

from .utils import version_table
from .utils import version_table, option


class VersionExpressionReflector(sa.sql.visitors.ReplacingCloningVisitor):
Expand All @@ -13,7 +13,7 @@ def replace(self, column):
if not isinstance(column, sa.Column):
return
try:
table = version_table(column.table)
table = version_table(column.table, option(self.parent, 'table_name'))
except KeyError:
reflected_column = column
else:
Expand Down
1 change: 1 addition & 0 deletions sqlalchemy_continuum/model_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ def inheritance_args(self, cls, version_table, table):
mapper = sa.inspect(self.model)

inherit_condition = adapt_columns(
self.model,
mapper.inherit_condition
)
tx_column_name = self.manager.options[
Expand Down
2 changes: 1 addition & 1 deletion sqlalchemy_continuum/relationship_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ def association_subquery(self, obj):
association_exists,
self.association_version_table.c.operation_type !=
Operation.DELETE,
adapt_columns(self.property.secondaryjoin),
adapt_columns(obj, self.property.secondaryjoin),
)
).correlate(self.local_cls, self.remote_cls)
)
Expand Down
14 changes: 8 additions & 6 deletions sqlalchemy_continuum/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,13 @@ def version_class(model):
return model


def version_table(table):
def version_table(table, version_suffix):
"""
Return associated version table for given SQLAlchemy Table object.
:param table: SQLAlchemy Table object
"""
table_model_class = get_mapper(table).class_ # get the table Model class name
suffixed_table_name = option(table_model_class, 'table_name') % table.name # get table_name attribute from option of version_manager
suffixed_table_name = version_suffix % table.name
if table.schema:
return table.metadata.tables[
table.schema + '.' + suffixed_table_name
Expand Down Expand Up @@ -445,11 +444,14 @@ def changeset(obj):


class VersioningClauseAdapter(sa.sql.visitors.ReplacingCloningVisitor):
def __init__(self, parent):
self.parent = parent # store the model CLS_or_Obj to have ability to query options attribute

def replace(self, col):
if isinstance(col, sa.Column):
table = version_table(col.table)
table = version_table(col.table, option(self.parent, 'table_name'))
return table.c.get(col.key)


def adapt_columns(expr):
return VersioningClauseAdapter().traverse(expr)
def adapt_columns(model, expr):
return VersioningClauseAdapter(parent=model).traverse(expr)
6 changes: 3 additions & 3 deletions tests/utils/test_version_table.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from tests import TestCase, uses_native_versioning

from sqlalchemy_continuum.utils import version_table
from sqlalchemy_continuum.utils import version_table, option

class TestVersionTableDefault(TestCase):

def test_version_table(self):
ArticleVersionTableName = version_table(self.Article.__table__)
ArticleVersionTableName = version_table(self.Article.__table__, option(self.Article, 'table_name'))
assert ArticleVersionTableName.fullname == 'article_version'

class TestVersionTableUserDefined(TestCase):
Expand All @@ -23,5 +23,5 @@ def options(self):
}

def test_version_table(self):
ArticleVersionTableName = version_table(self.Article.__table__)
ArticleVersionTableName = version_table(self.Article.__table__, option(self.Article, 'table_name'))
assert ArticleVersionTableName.fullname == 'article_user_defined'

0 comments on commit ee3b86c

Please sign in to comment.