question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow environment variables for sqlalchemy uri

See original GitHub issue

Is 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:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
mistercrunchcommented, May 31, 2020

@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 the Database at runtime and do whatever, meaning you can put in bogus username/password in the database, and your DB_CONNECTION_MUTATOR function can read an env var and replace the proper value.

# in your superset_config.py
def DB_CONNECTION_MUTATOR(sqlalchemy_url, params, effective_username, security_manager, source):
    # Assuming that by convention your `sqlalchemy_url` references the env var you want to use
    sqlalchemy_url = os.environ.get(sqlalchemy_url)
    return sqlalchemy_url, params
0reactions
gbriancommented, Jun 15, 2020

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found