Skip to content

Commit

Permalink
fib bug not tracking through table on insert and filtering on inner q…
Browse files Browse the repository at this point in the history
…ubquery
  • Loading branch information
toluaina committed Dec 8, 2024
1 parent 3ba10a3 commit d2b8ecd
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 39 deletions.
35 changes: 8 additions & 27 deletions pgsync/querybuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ def _root(
< txmax
)

# NB: only apply filters to the root node
if node._filters:
node._subquery = node._subquery.where(sa.and_(*node._filters))
node._subquery = node._subquery.alias()
Expand Down Expand Up @@ -420,11 +421,6 @@ def _children(self, node: Node) -> None:

if child._filters:

self.isouter = not any(
column in child.table_columns
for column in child.relationship.foreign_key.parent
)

for _filter in child._filters:
if isinstance(_filter, sa.sql.elements.BinaryExpression):
for column in _filter._orig:
Expand Down Expand Up @@ -575,11 +571,6 @@ def _through(self, node: Node) -> None: # noqa: C901

if child._filters:

self.isouter = not any(
column in child.table_columns
for column in child.relationship.foreign_key.parent
)

for _filter in child._filters:
if isinstance(_filter, sa.sql.elements.BinaryExpression):
for column in _filter._orig:
Expand Down Expand Up @@ -698,12 +689,6 @@ def _through(self, node: Node) -> None: # noqa: C901
== through.model.c[right_foreign_keys[i]]
)

if node._filters:
self.isouter = not any(
column in node.table_columns
for column in node.relationship.foreign_key.parent
)

op = sa.and_
if node.table == node.parent.table:
op = sa.or_
Expand All @@ -715,8 +700,10 @@ def _through(self, node: Node) -> None: # noqa: C901
)

node._subquery = inner_subquery.select_from(from_obj)
if node._filters:
node._subquery = node._subquery.where(sa.and_(*node._filters))

# NB do not apply filters to the child node as they are applied to the parent
# if node._filters:
# node._subquery = node._subquery.where(sa.and_(*node._filters))

node._subquery = node._subquery.group_by(
*[through.model.c[column] for column in foreign_keys[through.name]]
Expand Down Expand Up @@ -759,13 +746,6 @@ def _non_through(self, node: Node) -> None: # noqa: C901

if child._filters:

# NB:sometimes the FK is in the parent not the child
# if none of child.relationship.foreign_key.parent is in child.table_columns then isouter = True
self.isouter = not any(
column in child.table_columns
for column in child.relationship.foreign_key.parent
)

for _filter in child._filters:
if isinstance(_filter, sa.sql.elements.BinaryExpression):
for column in _filter._orig:
Expand Down Expand Up @@ -882,8 +862,9 @@ def _non_through(self, node: Node) -> None: # noqa: C901
)
node._subquery = node._subquery.where(sa.and_(*where))

if node._filters:
node._subquery = node._subquery.where(sa.and_(*node._filters))
# NB do not apply filters to the child node as they are applied to the parent
# if node._filters:
# node._subquery = node._subquery.where(sa.and_(*node._filters))

if node.relationship.type == ONE_TO_MANY:
node._subquery = node._subquery.group_by(
Expand Down
9 changes: 4 additions & 5 deletions pgsync/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -577,11 +577,10 @@ def _insert_op(
node, payload, foreign_keys, _filters
)

# through table with a direct references to root
if not _filters:
_filters = self._through_node_resolver(
node, payload, _filters
)
# also check through table with a direct references to root
_filters = self._through_node_resolver(
node, payload, _filters
)

if _filters:
filters[self.tree.root.table].extend(_filters)
Expand Down
6 changes: 3 additions & 3 deletions requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
#
async-timeout==5.0.1
# via redis
boto3==1.35.75
boto3==1.35.76
# via -r requirements/base.in
botocore==1.35.75
botocore==1.35.76
# via
# boto3
# s3transfer
Expand Down Expand Up @@ -63,7 +63,7 @@ python-dotenv==1.0.1
# via
# -r requirements/base.in
# environs
redis==5.2.0
redis==5.2.1
# via -r requirements/base.in
requests==2.32.3
# via
Expand Down
8 changes: 4 additions & 4 deletions requirements/dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ async-timeout==5.0.1
# via redis
black==24.10.0
# via -r requirements/dev.in
boto3==1.35.75
boto3==1.35.76
# via -r /Users/tolu/pgsync/requirements/base.in
botocore==1.35.75
botocore==1.35.76
# via
# boto3
# s3transfer
Expand All @@ -27,7 +27,7 @@ click==8.1.7
# via
# -r /Users/tolu/pgsync/requirements/base.in
# black
coverage[toml]==7.6.8
coverage[toml]==7.6.9
# via
# -r requirements/dev.in
# pytest-cov
Expand Down Expand Up @@ -129,7 +129,7 @@ python-dotenv==1.0.1
# environs
pyyaml==6.0.2
# via pre-commit
redis==5.2.0
redis==5.2.1
# via -r /Users/tolu/pgsync/requirements/base.in
requests==2.32.3
# via
Expand Down

0 comments on commit d2b8ecd

Please sign in to comment.