Skip to content

Commit 4ea1c80

Browse files
committed
feat: enhance database configuration handling for Cloud SQL with IAM authentication
1 parent 2261484 commit 4ea1c80

1 file changed

Lines changed: 37 additions & 14 deletions

File tree

transfers/transfer.py

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,19 @@
3838
# environment variables already set by the runtime (e.g., Cloud Run jobs).
3939
load_dotenv(override=False)
4040

41+
# In managed runtime environments, DB_DRIVER is occasionally omitted while
42+
# CLOUD_SQL_* vars are present. Default to cloudsql in that case to avoid
43+
# silently falling back to localhost/postgres settings.
44+
if (
45+
not (os.getenv("DB_DRIVER") or "").strip()
46+
and (os.getenv("CLOUD_SQL_INSTANCE_NAME") or "").strip()
47+
):
48+
os.environ["DB_DRIVER"] = "cloudsql"
49+
50+
# Cloud SQL should use IAM auth by default unless explicitly disabled.
51+
if (os.getenv("DB_DRIVER") or "").strip().lower() == "cloudsql":
52+
os.environ.setdefault("CLOUD_SQL_IAM_AUTH", "true")
53+
4154
from alembic import command
4255
from alembic.config import Config
4356

@@ -690,20 +703,30 @@ def _transfer_parallel(
690703
def main():
691704
message("START--------------------------------------")
692705

693-
# Display database configuration for verification
694-
db_name = os.getenv("POSTGRES_DB", "postgres")
695-
db_host = os.getenv("POSTGRES_HOST", "localhost")
696-
db_port = os.getenv("POSTGRES_PORT", "5432")
697-
message(f"Database Configuration: {db_host}:{db_port}/{db_name}")
698-
699-
# Double-check we're using the development database
700-
if db_name != "ocotilloapi_dev":
701-
message(f"WARNING: Using database '{db_name}' instead of 'ocotilloapi_dev'")
702-
if db_name in ("ocotilloapi_test", "nmsamplelocations_test"):
703-
raise ValueError(
704-
"ERROR: Cannot run transfer on test database! "
705-
"Set POSTGRES_DB=ocotilloapi_dev in .env file"
706-
)
706+
db_driver = (os.getenv("DB_DRIVER") or "").strip().lower()
707+
if db_driver == "cloudsql":
708+
db_name = os.getenv("CLOUD_SQL_DATABASE", "")
709+
instance_name = os.getenv("CLOUD_SQL_INSTANCE_NAME", "")
710+
iam_auth = os.getenv("CLOUD_SQL_IAM_AUTH", "")
711+
message(
712+
"Database Configuration: "
713+
f"driver=cloudsql instance={instance_name} db={db_name} iam_auth={iam_auth}"
714+
)
715+
else:
716+
# Display database configuration for verification
717+
db_name = os.getenv("POSTGRES_DB", "postgres")
718+
db_host = os.getenv("POSTGRES_HOST", "localhost")
719+
db_port = os.getenv("POSTGRES_PORT", "5432")
720+
message(f"Database Configuration: {db_host}:{db_port}/{db_name}")
721+
722+
# Double-check we're using the development database
723+
if db_name != "ocotilloapi_dev":
724+
message(f"WARNING: Using database '{db_name}' instead of 'ocotilloapi_dev'")
725+
if db_name in ("ocotilloapi_test", "nmsamplelocations_test"):
726+
raise ValueError(
727+
"ERROR: Cannot run transfer on test database! "
728+
"Set POSTGRES_DB=ocotilloapi_dev in .env file"
729+
)
707730

708731
metrics = Metrics()
709732

0 commit comments

Comments
 (0)