Allow environment variables for sqlalchemy uri
See original GitHub issueIs your feature request related to a problem? Please describe. When deploying to multiple environments we’ll like to be able to switch database connection. Currently we have one script fixing database sqlalchemy uri
Describe the solution you’d like
Allow the use of environment variables as the value of sqlalchemy_uri
property of Database
Describe alternatives you’ve considered None yet
Additional context We deploy using docker
Proposal
class Database(Model, AuditMixinNullable, ImportMixin):
....
allow_env_for_uri = Column(Boolean, default=False)
....
@property
def expanded_sqlalchemy_uri(self):
if self.allow_env_for_uri:
return os.path.expandvars(self.sqlalchemy_uri)
return self.sqlalchemy_uri
@property
def sqlalchemy_uri_decrypted(self):
conn = sqla.engine.url.make_url(self.expanded_sqlalchemy_uri)
if custom_password_store:
conn.password = custom_password_store(conn)
else:
conn.password = self.password
return str(conn)
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Allow environment variables for sqlalchemy uri #9045 - GitHub
When deploying to multiple environments we'll like to be able to switch database connection. Currently we have one script fixing database ...
Read more >Engine Configuration — SQLAlchemy 2.0 Documentation
A select set of keyword arguments will be “coerced” to their expected type based on string values.
Read more >Connect to dynamic database parameters sqlalchemy
You want to set an environment variable for this. Since each environment is unique to the machine it's running on, each team member...
Read more >Configuration — Flask-SQLAlchemy Documentation (2.x)
A dictionary that maps bind keys to SQLAlchemy connection URIs. For more information about ... Can be used to explicitly disable or enable...
Read more >Settings and Environment Variables - FastAPI
To set multiple env vars for a single command just separate them with a space, and put them all before the command. And...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@willbarrett, @gbrian is not referring to
SQLALCHEMY_DATABASE_URI
, but to the ones ones that we encrypt and store in the database.I’m not sure how well that is documented, but with
DB_CONNECTION_MUTATOR
(a configuration hook) you can intercept theDatabase
at runtime and do whatever, meaning you can put in bogus username/password in the database, and yourDB_CONNECTION_MUTATOR
function can read an env var and replace the proper value.Thanks @willbarrett , @mistercrunch. I implemented differently as wanted to have a flag on database connection to set if ENV replacement should apply but reconsidering in favor of: DB_CONNECTION_MUTATOR and SQL_QUERY_MUTATOR