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

ActionNetwork SQL Mirror querying support + general SSH util to query any db through ssh #1102

Merged
merged 69 commits into from
Jul 18, 2024

Conversation

NirTatcher
Copy link
Contributor

@NirTatcher NirTatcher commented Jul 16, 2024

  • Added SSH util function we can connect through SSH and query through, added a test for that.
  • Added SQL Mirror support for ActionNetwork which lets us query our data/connect to our data we have in ActionNetwork (all tables read only access).
    @austinweisgrau, @shaunagm I hope this won't break anything like my other PR, I recreated the utility and made it from a class to just a function we can use to query a DB through a SSH tunnel connection.
    Please let me know if there is any feedback or any change request :)
    Also, thank you again @austinweisgrau and @shaunagm for everything!

NirTatcher and others added 30 commits September 26, 2023 19:23
…dvocacy Campaigns, Attendances, Campaigns, Custom Fields, Donations, Embeds, Event Campaigns, Events, Forms, Fundraising Pages, Items, Lists, Messages, Metadata, Outreaches, People, Petitions, Queries, Signatures, Submissions, Tags, Taggings, Wrappers)
…ng with using the redshift conector there instead of just connecting with psycopg and SSHTunnelForwader
…ng with using the redshift conector there instead of just connecting with psycopg and SSHTunnelForwader2
@NirTatcher NirTatcher changed the title Nirs actionnetwork changes ActionNetwork SQL Mirror querying support + general SSH util to query any db through ssh Jul 16, 2024
@@ -8,3 +8,5 @@ pytest-mock==3.12.0
pytest==8.1.1
requests-mock==1.11.0
testfixtures==8.1.0
sshtunnel==0.4.0
Copy link
Collaborator

Choose a reason for hiding this comment

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

Remove this, it shouldn't be added to requirements-dev.txt

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment on lines +71 to +72
except Exception as e:
logging.error(f"Error during query execution: {e}")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Remove this except block - this will prevent exceptions from being raised. It's good to log the exceptions, but generally if an exception occurs here, it would be appropriate to raise it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, we are now raising the exception after logging it. Hope this is ok and please let me know if you prefer the code without the except block all together.

Comment on lines +73 to +79
finally:
if con:
con.close()
logging.info("Database connection closed.")
if server:
server.stop()
logging.info("SSH tunnel closed.")
Copy link
Collaborator

Choose a reason for hiding this comment

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

The finally block is good though

Copy link
Collaborator

@austinweisgrau austinweisgrau Jul 17, 2024

Choose a reason for hiding this comment

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

Technically, querying the ActionNetwork SQL mirror is a new connector. It has different dependencies, it queries data from a different source. I get the impulse to have it as a method on the existing ActionNetwork API connector, but I think that isn't the right move for a few reasons.

  • It adds two dependencies to the ActionNetwork connector that API users don't need: the postgres extra and the sshtunnel package
  • The method on the ActionNetwork connector to query the SSH mirror doesn't do anything - it wraps the query_through_ssh method without doing anything specific.

I think the query_through_ssh should be added as its own utility as you do here, but the ActionNetwork connector should be left unchanged. Anyone who wants to query the ActionNetwork SQL mirror can use the query_through_ssh method, and nothing needs to be done with the ActionNetwork connector.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes a lot of sense. Just to be sure I've added to the ActionNetwork docs an example of how you would call query_through_ssh to query your SQL Mirror.

Comment on lines 2002 to 2004
except Exception as e:
print(e)
return None
Copy link
Collaborator

Choose a reason for hiding this comment

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

Again just in general, avoid this pattern - it results in Exceptions that should be raised not being raised.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Like you said I deleted this function from the ActionNetwork connector, thank you so much for your time reviewing and giving me feedback I will for sure notice and avoid this pattern of not raising exceptions!

Copy link
Collaborator

Choose a reason for hiding this comment

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

Since the new method on the ActionNetwork connector doesn't do anything specific, it just calls the query_through_ssh method and passes all arguments to it, there's no need for an additional test. This test can be removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Copy link
Collaborator

@austinweisgrau austinweisgrau left a comment

Choose a reason for hiding this comment

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

One last thought is that since the SSHTunnel utility adds a new dependency, you'll need to include it as an extra in the setup.py file. I think it would be appropriate to add a new extra called ssh where the dependencies can be included. e.g. the new extra will be:
"ssh": ["sshtunnel", "psycopg2-binary>=2.9.9", "sqlalchemy >= 1.4.22, != 1.4.33, < 2.0.0"]
because you'll need to include the postgres connector's dependencies as well

@NirTatcher
Copy link
Contributor Author

NirTatcher commented Jul 18, 2024

One last thought is that since the SSHTunnel utility adds a new dependency, you'll need to include it as an extra in the setup.py file. I think it would be appropriate to add a new extra called ssh where the dependencies can be included. e.g. the new extra will be: "ssh": ["sshtunnel", "psycopg2-binary>=2.9.9", "sqlalchemy >= 1.4.22, != 1.4.33, < 2.0.0"] because you'll need to include the postgres connector's dependencies as well

Done.
In general thank you again @austinweisgrau for the review, feedbacks I really appreciate it!
Please let me know if there is anything else you think we should do.

Copy link
Collaborator

@austinweisgrau austinweisgrau 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 for making those changes! This looks much better to me.

@austinweisgrau austinweisgrau merged commit ee2a16d into move-coop:main Jul 18, 2024
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants