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

Setting variables on connection #776

Open
mpdevilleres opened this issue Mar 29, 2021 · 3 comments
Open

Setting variables on connection #776

mpdevilleres opened this issue Mar 29, 2021 · 3 comments
Labels
question A community question, closed when inactive.

Comments

@mpdevilleres
Copy link

Greetings,

I have a use case where I need to set the variable for each session or connection.
and I am able to accomplish it using the asyncpg directly

import asyncpg
con = await asyncpg.connect('postgresql://tenants@localhost:5433/internal?application_name=app_name')
r = await con.fetchrow("SELECT current_setting('application_name')")
Out[1]: <Record current_setting='app_name'>

con = await asyncpg.connect('postgresql://postgres@localhost:5433/internal?options=-c%20app.name%3Dapp_name')
r = await con.fetchrow("SELECT current_setting('app.name')")
r
Out[2]: <Record current_setting='app_name'>

but doing it with gino as shown below doesn't work.

from gino import Gino
db = Gino()
await db.set_bind('postgresql://tenants@localhost:5433/internal?application_name=app_name')
r = await db.all("SELECT current_setting('application_name')")
print(r)
Out[3]: [('',)]

db = Gino()
await db.set_bind('postgresql://postgres@localhost:5433/internal?options=-c%20app.name%3Dapp_name')
r = await db.all("SELECT current_setting('app.name')")
print(r)
Out[4]: asyncpg.exceptions.UndefinedObjectError: unrecognized configuration parameter "app.name"

I also tried passing connect_args={"application_name":"myapp"} as recommended by zzzeek on set_bind. but I still fail to make it work.

Is there anyway for gino to achieve my use case?

@mpdevilleres mpdevilleres added the question A community question, closed when inactive. label Mar 29, 2021
@Chaostheorie
Copy link
Contributor

Chaostheorie commented May 24, 2021

Have you tried using an approach similar to this answer from SO?

You might just be able to use something like:

# Get your db instance yada

# Set value
await db.one_or_none("select context('application_name', 'my awesome gino application');")

# Get value
await db.one_or_none("select context('application_name');")

Can't test this code ATM, but maybe context vars are the way to go for psql.

You might otherwise need to refer to #683

@mpdevilleres
Copy link
Author

Hi @Chaostheorie,

Thank you for your response, I am not sure how to do this as a wrapper.
the reason I wanted to pass the parameter in the url so that I don't need to change any of my existing queries.

If you have any idea i would appreciate discussing it and trying it out.

Thanks,

@Chaostheorie
Copy link
Contributor

@mpdevilleres I understand the intention to not alter existing queries.

I'm gonna assume you're either using gino with an adapter, such as gino-quart, or directly, which should allow you to manipulate the individual connection.

I'm not sure if you're able to get away without changing queries completely. You might either need to prepend a statement to you queries (no idea how but you could try to play with SQLalchemy) or set a session related variable. I have found on related question on SO.

I would love to further discuss on this but I'm currently busy with studying and my responses might take some time. I hope the above mentioned articles will be helpful. You might also want to open question in asyncpg's repo about this, since it's used under the hood.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question A community question, closed when inactive.
Projects
None yet
Development

No branches or pull requests

2 participants