Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix issue with outerjoin when there are repeated tables #494

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions pgsync/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ class Node(object):
models: Callable
table: str
schema: str
table_count: Optional[int] = None
primary_key: Optional[list] = None
label: Optional[str] = None
transform: Optional[dict] = None
Expand Down Expand Up @@ -259,6 +260,7 @@ class Tree:

def __post_init__(self):
self.tables: Set[str] = set()
self.table_counts: Dict[str, int] = {}
self.__nodes: Dict[Node] = {}
self.root: Optional[Node] = None

Expand All @@ -271,7 +273,7 @@ def traverse_breadth_first(self) -> Generator:
def traverse_post_order(self) -> Generator:
return self.root.traverse_post_order()

def build(self, data: dict) -> Node:
def build(self, data: dict, is_root: bool = True) -> Node:
if not isinstance(data, dict):
raise SchemaError(
"Incompatible schema. Please run v2 schema migration"
Expand Down Expand Up @@ -302,13 +304,19 @@ def build(self, data: dict) -> Node:
self.root = node

self.tables.add(node.table)
if node.table not in self.table_counts:
self.table_counts[node.table] = 0
self.table_counts[node.table] += 1
for through in node.relationship.throughs:
self.tables.add(through.table)

for child in data.get("children", []):
node.add_child(self.build(child))
node.add_child(self.build(child, is_root=False))

self.__nodes[key] = node
if is_root:
for child_node in self.traverse_post_order():
child_node.table_count = self.table_counts[child_node.table]
return node

def get_node(self, table: str, schema: str) -> Node:
Expand Down
14 changes: 9 additions & 5 deletions pgsync/querybuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,9 +396,11 @@ def _children(self, node: Node) -> None:
self.from_obj = child.parent.model

if child._filters:
self.isouter = False
if child.table_count <= 1:
self.isouter = False
for _filter in child._filters:
if isinstance(_filter, sa.sql.elements.BinaryExpression):

for column in _filter._orig:
if hasattr(column, "value"):
_column = child._subquery.c
Expand Down Expand Up @@ -546,8 +548,8 @@ def _through(self, node: Node) -> None: # noqa: C901
from_obj = node.model

if child._filters:
self.isouter = False

if child.table_count <= 1:
self.isouter = False
for _filter in child._filters:
if isinstance(_filter, sa.sql.elements.BinaryExpression):
for column in _filter._orig:
Expand Down Expand Up @@ -667,7 +669,8 @@ def _through(self, node: Node) -> None: # noqa: C901
)

if node._filters:
self.isouter = False
if node.table_count <= 1:
self.isouter = False

op = sa.and_
if node.table == node.parent.table:
Expand Down Expand Up @@ -723,7 +726,8 @@ def _non_through(self, node: Node) -> None: # noqa: C901
from_obj = node.model

if child._filters:
self.isouter = False
if child.table_count <= 1:
self.isouter = False

for _filter in child._filters:
if isinstance(_filter, sa.sql.elements.BinaryExpression):
Expand Down
41 changes: 41 additions & 0 deletions tests/test_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,3 +340,44 @@ def test_init(self):
}
)
assert "Multiple through tables" in str(excinfo.value)


@pytest.mark.usefixtures("table_creator")
class TestNodeTableCount(object):
"""Node tests."""

@pytest.fixture(scope="function")
def nodes(self):
return {
"table": "book",
"columns": ["isbn", "title", "description"],
"children": [
{
"table": "author",
"columns": ["id", "name"],
"label": "authors",
"relationship": {
"type": "one_to_many",
"variant": "object",
"through_tables": ["book_author"],
},
"children": [
{
"table": "book",
"columns": ["isbn", "title", "description"],
"label": "favorite_book",
"relationship": {
"type": "one_to_many",
"variant": "object",
"through_tables": ["author_favorite_book"],
},
}
],
},
],
}

def test_tree_build_table_count(self, sync, nodes):
tree = Tree(sync.models).build(nodes)
assert tree.table_count == {"author": 1, "book": 2}
sync.search_client.close()