-
Notifications
You must be signed in to change notification settings - Fork 0
[Task] Update analytics db to use local.env and Remove Dynaconf from Analytics #136
Changes from 6 commits
1a99a71
42e2f6e
0a445b5
0946598
7d9a5da
bb2908b
b1546b5
0fe4fdf
868b50e
6bec7c2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,2 @@ | ||
data | ||
|
||
# Ignore dynaconf secret files | ||
.secrets.* |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,19 @@ | ||
"""Loads configuration variables from settings files and settings files | ||
"""Loads configuration variables from settings files | ||
|
||
Dynaconf provides a few valuable features for configuration management: | ||
- Load variables from env vars and files with predictable overrides | ||
- Validate the existence and format of required configs | ||
- Connect with secrets managers like HashiCorp's Vault server | ||
- Load different configs based on environment (e.g. DEV, PROD, STAGING) | ||
|
||
For more information visit: https://www.dynaconf.com/ | ||
""" | ||
import os | ||
from pydantic_settings import BaseSettings, SettingsConfigDict | ||
from pydantic import Field | ||
|
||
from dynaconf import Dynaconf, Validator, ValidationError | ||
|
||
settings = Dynaconf( | ||
# set env vars with `export ANALYTICS_FOO=bar` | ||
envvar_prefix="ANALYTICS", | ||
# looks for config vars in the following files | ||
# with vars in .secrets.toml overriding vars in settings.toml | ||
settings_files=["settings.toml", ".secrets.toml"], | ||
# merge the settings found in all files | ||
merge_enabled= True, | ||
# add validators for our required config vars | ||
validators=[ | ||
Validator("SLACK_BOT_TOKEN", must_exist=True), | ||
Validator("REPORTING_CHANNEL_ID", must_exist=True), | ||
], | ||
) | ||
# reads environment variables from .env files defaulting to "local.env" | ||
class PydanticBaseEnvConfig(BaseSettings): | ||
model_config = SettingsConfigDict(env_file="%s.env" % os.getenv("ENVIRONMENT", "local"), extra="ignore") # set extra to ignore so that it ignores variables irrelevant to the database config (e.g. metabase settings) | ||
|
||
# raises after all possible errors are evaluated | ||
try: | ||
settings.validators.validate_all() | ||
except ValidationError as error: | ||
list_of_all_errors = error.details | ||
print(list_of_all_errors) | ||
raise | ||
class DBSettings(PydanticBaseEnvConfig): | ||
db_host: str = Field(alias="DB_HOST") | ||
port: int = Field(5432,alias="DB_PORT") | ||
user: str = Field (alias="DB_USER") | ||
password: str = Field(alias="DB_PASSWORD") | ||
ssl_mode: str = Field(alias="DB_SSL_MODE") | ||
slack_bot_token: str = Field(alias="SLACK_BOT_TOKEN") | ||
reporting_channel_id: str = Field(alias="REPORTING_CHANNEL_ID") |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,13 +3,12 @@ | |
|
||
from sqlalchemy import Engine, create_engine | ||
|
||
from config import settings | ||
from config import DBSettings | ||
|
||
# The variables used in the connection url are pulled from local.env | ||
# and configured in the DBSettings class found in config.py | ||
|
||
|
||
# The variables used in the connection url are set in settings.toml and | ||
# .secrets.toml. These can be overridden with the custom prefix defined in config.py: "ANALYTICS". | ||
# e.g. `export ANALYTICS_POSTGRES_USER=new_usr`. | ||
# Docs: https://www.dynaconf.com/envvars/ | ||
def get_db() -> Engine: | ||
""" | ||
Get a connection to the database using a SQLAlchemy engine object. | ||
|
@@ -22,8 +21,10 @@ def get_db() -> Engine: | |
sqlalchemy.engine.Engine | ||
A SQLAlchemy engine object representing the connection to the database. | ||
""" | ||
db = DBSettings | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You'd want to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @chouinar do you mean something like def get_db_settings() -> DBSettings:
return DBSettings()
def get_db() -> Engine:
# rest of code There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In a few other files you do: from config import get_db_settings
settings = get_db_settings() You need to do that here as well. Just doing -- Is this bit of code actually running in the tests? It should fail if run as written right now There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've been testing locally by running the actual make commands which (bizarrely) work. I'm in the process of double checking the tests. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @aplybeah any update on the suggested change? |
||
print(f"postgresql+psycopg://{db.user}:{db.password}@{db.db_host}:{db.port}") | ||
return create_engine( | ||
f"postgresql+psycopg://{settings.postgres_user}:{settings.postgres_password}@{settings.postgres_host}:{settings.postgres_port}", | ||
f"postgresql+psycopg://{db.user}:{db.password}@{db.db_host}:{db.port}", | ||
pool_pre_ping=True, | ||
hide_parameters=True, | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this work? I believe you need to instantiate the object in order to use the variables otherwise you're just using the class definition itself.
Something like