summaryrefslogtreecommitdiffstats
path: root/packages/meshbay-hub/src/meshbay_hub/db/migrations/env.py
diff options
context:
space:
mode:
Diffstat (limited to 'packages/meshbay-hub/src/meshbay_hub/db/migrations/env.py')
-rw-r--r--packages/meshbay-hub/src/meshbay_hub/db/migrations/env.py24
1 files changed, 19 insertions, 5 deletions
diff --git a/packages/meshbay-hub/src/meshbay_hub/db/migrations/env.py b/packages/meshbay-hub/src/meshbay_hub/db/migrations/env.py
index f572716..8908deb 100644
--- a/packages/meshbay-hub/src/meshbay_hub/db/migrations/env.py
+++ b/packages/meshbay-hub/src/meshbay_hub/db/migrations/env.py
@@ -19,11 +19,25 @@ if config.config_file_name is not None:
target_metadata = Base.metadata
-# Allow override via env var (production uses asyncpg, tests may use aiosqlite)
-db_url = os.environ.get(
- "MESHBAY_DATABASE_URL",
- "postgresql+asyncpg://meshbay:meshbay@localhost/meshbay_hub",
-)
+# Three sources, in the order that keeps them from disagreeing.
+#
+# A caller that already resolved the URL wins: `meshbay-hub migrate` passes
+# `cfg.db.url`, which is `load_config`'s answer — the very string the server
+# will connect with. Reading the environment again here instead would be a
+# second resolution of the same question, and the two would drift the day
+# anything but the environment decides it.
+#
+# Then the environment, for `alembic` run by hand or from a deploy script,
+# where `alembic.ini` carries only its placeholder. Then a local default, so
+# a developer's checkout needs no setup.
+_PLACEHOLDER = "driver://user:pass@localhost/dbname"
+
+db_url = config.get_main_option("sqlalchemy.url", "")
+if not db_url or db_url == _PLACEHOLDER:
+ db_url = os.environ.get(
+ "MESHBAY_DATABASE_URL",
+ "postgresql+asyncpg://meshbay:meshbay@localhost/meshbay_hub",
+ )
config.set_main_option("sqlalchemy.url", db_url)