Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Changed connection driver for ease of use. #2194

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

Merged
merged 11 commits into from
Jul 24, 2019

Conversation

pranaynanda
Copy link
Contributor

The current code does not work if tried to implement as is. There are bugs as described in the first comment of this StackOverflow question. https://stackoverflow.com/a/53784015/5056741. To be precise, it raises 'raise InterfaceError("communication error", e)' error which appears to be related to a bug in postgres+pg8000 driver

The current code does not work if tried to implement as is. There are bugs as described in the first comment of this StackOverflow question. https://stackoverflow.com/a/53784015/5056741
@googlebot
Copy link

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here (e.g. I signed it!) and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

ℹ️ Googlers: Go here for more info.

@googlebot googlebot added the cla: no This human has *not* signed the Contributor License Agreement. label Jun 5, 2019
@pranaynanda
Copy link
Contributor Author

I signed it!

@googlebot
Copy link

CLAs look good, thanks!

ℹ️ Googlers: Go here for more info.

@googlebot googlebot added cla: yes This human has signed the Contributor License Agreement. and removed cla: no This human has *not* signed the Contributor License Agreement. labels Jun 5, 2019
Copy link
Contributor

@gguuss gguuss left a comment

Choose a reason for hiding this comment

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

Looks like you need to push a commit removing the trailing whitespace for your PR to pass lint.

nox > flake8 --show-source --builtin gettext --max-complexity 20 --import-order-style google --exclude .nox,.cache,env,lib,generated_pb2,*_pb2.py,*_pb2_grpc.py --ignore=E121,E123,E126,E226,E24,E704,W503,W504,I100,I201,I202 --application-import-names main,templates .
./main.py:45:25: W291 trailing whitespace
        database=db_name 
                        ^
nox > Command flake8 --show-source --builtin gettext --max-complexity 20 --import-order-style google --exclude .nox,.cache,env,lib,generated_pb2,*_pb2.py,*_pb2_grpc.py --ignore=E121,E123,E126,E226,E24,E704,W503,W504,I100,I201,I202 --application-import-names main,templates . failed with exit code 1
nox > Session lint(sample='./cloud-sql/postgres/sqlalchemy') failed.
The command "./scripts/travis.sh" exited with 1.

Copy link
Contributor

@gguuss gguuss left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@gguuss gguuss left a comment

Choose a reason for hiding this comment

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

I'm less familiar with the psycop connector, will have to re-review

@gguuss
Copy link
Contributor

gguuss commented Jul 23, 2019

@kurtisvg Can you comment on whether we should be using pg8000 or psycopg2?

gguuss
gguuss previously requested changes Jul 23, 2019
@@ -1,3 +1,3 @@
Flask==1.0.2
SQLAlchemy==1.2.17
pg8000==1.13.1
psycopg2==2.8.2
Copy link
Contributor

@gguuss gguuss Jul 23, 2019

Choose a reason for hiding this comment

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

Update versions here.

Flask==1.1.1
SQLAlchemy==1.3.6
pg8000==1.13.2

@@ -36,16 +36,13 @@
# managing a pool of connections to your database
db = sqlalchemy.create_engine(
# Equivalent URL:
# postgres+pg8000://<db_user>:<db_pass>@/<db_name>?unix_socket=/cloudsql/<cloud_sql_instance_name>/.s.PGSQL.5432
# postgres+psycopg2://<db_user>:<db_pass>@/<db_name>?unix_socket=/cloudsql/<cloud_sql_instance_name>/.s.PGSQL.5432
Copy link
Contributor

Choose a reason for hiding this comment

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

Are you sure the parameter is unix_socket? I believe I've seen it as unix_sock in some adapters.

Copy link
Contributor

@gguuss gguuss Jul 23, 2019

Choose a reason for hiding this comment

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

I'd prefer to keep the pg8000 adapter and just show the correct parameter. If you are using the psycopg2 connector, it seems that you will need to install additional native dependencies for your platform.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's cool if it works with the pg8000 adapter. I only changed it because the pg8000 driver wasn't working as per demo and was raising eroors as mentioned at the top. I tested it with psycopg2 instead and it worked. Because the PR is over a month old, I can't find the exact piece of working code that I tested with but I double checked again and this piece works:

db = sqlalchemy.create_engine(
    # Equivalent URL:
    'postgres+psycopg2://{}:{}@/{}?host=/cloudsql/{}'.format(db_user, db_pass, db_name, cloud_sql_instance_name)
    )

Also StackOverflow says that 'unix_sock' should be 'host'.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'll investigate deeper.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

And found this in SQLAlchemy's documentation.
image

Moreover it appears that pg8000 does not support connecting over Cloud SQL proxy which is a recommended best practice.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm testing now, thank you btw for taking the time to make the PR :)

Copy link
Contributor

Choose a reason for hiding this comment

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

I think you are right, just will confirm.

Copy link
Contributor

Choose a reason for hiding this comment

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

The parameters get passed directly to the driver, and thus change depend on which driver you are using. pg8000 requires unix_sock: http://pybrary.net/pg8000/dbapi.html

@pranaynanda
Copy link
Contributor Author

@kurtisvg Can you comment on whether we should be using pg8000 or psycopg2?

I tried using pg8000 as is initially. It returned 'raise InterfaceError("communication error", e)'. Changing it to psycopg2 worked. That's when I opened the pull request.

@kurtisvg
Copy link
Contributor

We use pg8000 specifically because it's a pure python driver and thus it works in many situations where psycopg2 doesn't. The issue you are experiencing is because pg8000 requires the user to specify the suffix .s.PGSQL.5432, where as psycopg2 automatically adds this suffix for you.

The suffix was added to the sample in #2235. It looks like the comment is still incorrect however.

@kurtisvg
Copy link
Contributor

It looks like the parameters are backwards between mysql and postgres.

Copy link
Contributor

@kurtisvg kurtisvg left a comment

Choose a reason for hiding this comment

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

Please update the comments in the mysql and postgres files to correctly reflect the parameters below.

I would prefer to leave the driver as pg8000 since it is a pure python driver.

@pranaynanda
Copy link
Contributor Author

I've changed both main.py and requirements.txt to use pg8000 by default. I've also changed main.py to the correct query statements.

@gguuss
Copy link
Contributor

gguuss commented Jul 24, 2019

@pranaynanda Thanks again for the PR, I'm thinking it may make sense to leave as pg8000 to simplify getting the requirements installed.

Anecdotally, the sample as configured in Master is working for me and I'm unable to get psycopg2 installed on my dev machines as the sources for postgresql are missing. What's weird though is that I'm unable to install the dependency pg8000==1.13.2, only pg8000==1.12.5, potentially because I'm testing with Python2. When I first ran the sample, I had the wrong DB name which caused the same exception you were seeing.

@pranaynanda
Copy link
Contributor Author

pranaynanda commented Jul 24, 2019

Sure. Makes sense. The code might have changed against another PR after I created this PR. The one in master branch works now. I had an older copy that did not have the /.s.PGSQL.5432 suffix for the pg8000 driver.

Copy link
Contributor

@kurtisvg kurtisvg left a comment

Choose a reason for hiding this comment

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

Thanks for taking the time to PR this. Apologies it sat unattended for so long.

@kurtisvg kurtisvg dismissed gguuss’s stale review July 24, 2019 16:08

changes made

@kurtisvg kurtisvg merged commit 599d096 into GoogleCloudPlatform:master Jul 24, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes This human has signed the Contributor License Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants