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

feat: Accept numbered replica database URLs #5222

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions api/app/settings/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from task_processor.task_run_method import TaskRunMethod # type: ignore[import-untyped]

from app.routers import ReplicaReadStrategy
from app.utils import get_numbered_env_vars_with_prefix

env = Env()

Expand Down Expand Up @@ -176,8 +177,14 @@
),
}
REPLICA_DATABASE_URLS_DELIMITER = env("REPLICA_DATABASE_URLS_DELIMITER", ",")
REPLICA_DATABASE_URLS = env.list(
"REPLICA_DATABASE_URLS", default=[], delimiter=REPLICA_DATABASE_URLS_DELIMITER
REPLICA_DATABASE_URLS = (
env.list(
"REPLICA_DATABASE_URLS",
default=[],
delimiter=REPLICA_DATABASE_URLS_DELIMITER,
)
if not os.getenv("REPLICA_DATABASE_URL_0")
else get_numbered_env_vars_with_prefix("REPLICA_DATABASE_URL_")
)
NUM_DB_REPLICAS = len(REPLICA_DATABASE_URLS)

Expand All @@ -186,10 +193,14 @@
CROSS_REGION_REPLICA_DATABASE_URLS_DELIMITER = env(
"CROSS_REGION_REPLICA_DATABASE_URLS_DELIMITER", ","
)
CROSS_REGION_REPLICA_DATABASE_URLS = env.list(
"CROSS_REGION_REPLICA_DATABASE_URLS",
default=[],
delimiter=CROSS_REGION_REPLICA_DATABASE_URLS_DELIMITER,
CROSS_REGION_REPLICA_DATABASE_URLS = (
env.list(
"CROSS_REGION_REPLICA_DATABASE_URLS",
default=[],
delimiter=CROSS_REGION_REPLICA_DATABASE_URLS_DELIMITER,
)
if not os.getenv("CROSS_REGION_REPLICA_DATABASE_URL_0")
else get_numbered_env_vars_with_prefix("CROSS_REGION_REPLICA_DATABASE_URL_")
)
NUM_CROSS_REGION_DB_REPLICAS = len(CROSS_REGION_REPLICA_DATABASE_URLS)

Expand Down
17 changes: 17 additions & 0 deletions api/app/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import os
import pathlib
from functools import lru_cache
from typing import TypedDict
Expand Down Expand Up @@ -73,3 +74,19 @@ def _get_file_contents(file_path: str) -> str:
return f.read().replace("\n", "")
except FileNotFoundError:
return UNKNOWN


def get_numbered_env_vars_with_prefix(prefix: str) -> list[str]:
"""
Returns a list containing the values of all environment variables whose names have a given prefix followed by an
integer, starting from 0, until no more variables with that prefix are found.
"""
db_urls = []
i = 0
while True:
db_url = os.getenv(f"{prefix}{i}")
if not db_url:
break
db_urls.append(db_url)
i += 1
return db_urls
15 changes: 14 additions & 1 deletion api/tests/unit/app/test_unit_app_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from pyfakefs.fake_filesystem import FakeFilesystem
from pytest_django.fixtures import SettingsWrapper

from app.utils import get_version_info
from app.utils import get_numbered_env_vars_with_prefix, get_version_info


def test_get_version_info(fs: FakeFilesystem) -> None:
Expand Down Expand Up @@ -97,3 +97,16 @@ def test_get_version_info__email_config_disabled__return_expected(

# Then
assert result["has_email_provider"] is False


def test_get_numbered_env_vars_with_prefix(monkeypatch: pytest.MonkeyPatch) -> None:
# Given
monkeypatch.setenv("DB_URL_0", "0")
monkeypatch.setenv("DB_URL_1", "1")
monkeypatch.setenv("DB_URL_3", "3")

# When
env_vars = get_numbered_env_vars_with_prefix("DB_URL_")

# Then
assert env_vars == ["0", "1"]