Skip to content
New issue

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

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

Already on GitHub? # to your account

fix(server): Update query to paginate correctly against filter #3012

Merged
merged 11 commits into from
Nov 20, 2024

Conversation

nalanj
Copy link
Contributor

@nalanj nalanj commented Nov 18, 2024

Describe your changes

Update the connections list query to filter errored connections appropriately. This was a little tricky! The key element though is the FILTER against json_agg. The query I'm targeting roughly looks like this:

SELECT row_to_json(_nango_connections.*) as connection,
  row_to_json(end_users.*) as end_user,
  _nango_configs.provider,
  COALESCE(
  	json_agg(
  		json_build_object('type', _nango_active_logs.type, 'log_id', _nango_active_logs.log_id)
  	) FILTER (WHERE _nango_active_logs.id IS NOT NULL)
  	, '[]'::json
  ) as active_logs,
  count(_nango_active_logs.id) as active_logs_count
FROM _nango_connections
	INNER JOIN _nango_configs ON "_nango_connections"."config_id" = "_nango_configs"."id"
	LEFT JOIN end_users ON end_users.id = _nango_connections.end_user_id
	LEFT JOIN _nango_active_logs ON _nango_connections.id = _nango_active_logs.connection_id
		AND _nango_active_logs.active = true
WHERE _nango_connections.environment_id = 2
	AND _nango_connections.deleted = false
GROUP BY _nango_connections.id, end_users.id, _nango_configs.provider
	HAVING count(_nango_active_logs.id) = 0

Issue ticket number and link

https://linear.app/nango/issue/NAN-2134/bug-with-connection-errors

Checklist before requesting a review (skip if just adding/editing APIs & templates)

  • I added tests, otherwise the reason is:
  • I added observability, otherwise the reason is:
  • I added analytics, otherwise the reason is:

Copy link

linear bot commented Nov 18, 2024

@nalanj nalanj self-assigned this Nov 19, 2024
@nalanj nalanj marked this pull request as ready for review November 19, 2024 17:16
@nalanj nalanj requested review from bodinsamuel and a team November 19, 2024 17:16
Copy link
Collaborator

@TBonnin TBonnin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we are due for adding test(s) for this function. The query was already quite involved and I am worried about breaking something here. I am happy to pair if you want to

Copy link
Collaborator

@bodinsamuel bodinsamuel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

.where({
'_nango_connections.environment_id': environmentId,
'_nango_connections.deleted': false
})
.orderBy('_nango_connections.created_at', 'desc');
.orderBy('_nango_connections.created_at', 'desc')
.groupBy('_nango_connections.id', 'end_users.id', '_nango_configs.provider')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL you could groupBy .id with a row_to_json, that was the main reason for the subQuery, thanks 🙏🏻

@nalanj nalanj requested a review from TBonnin November 20, 2024 13:11
Copy link
Collaborator

@TBonnin TBonnin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you very much for adding tests

});

const connectionIds = dbConnections.map((c) => c.connection.connection_id);
expect(connectionIds).toEqual([googleOK.connection_id]);
Copy link
Collaborator

@TBonnin TBonnin Nov 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you avoid the mapping with something like

expect(dbConnections).toEqual([
   expect.objectContaining({ 
      connection: { 
         connection_id: googleOK.connection_id 
      }
   })
]);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TBonnin It has to be this because of how connection id is nested:

expect(dbConnections).toEqual([
  expect.objectContaining({ connection: expect.objectContaining({ connection_id: notion.connection_id }) }),
  expect.objectContaining({ connection: expect.objectContaining({ connection_id: google.connection_id }) })
]);

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nvm then. this is ugly

@nalanj nalanj enabled auto-merge (squash) November 20, 2024 15:55
@nalanj nalanj merged commit 7794bd9 into master Nov 20, 2024
20 checks passed
@nalanj nalanj deleted the alan/nan-2134-bug-with-connection-errors/fix-filter branch November 20, 2024 16:02
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants