|
38 | 38 | # environment variables already set by the runtime (e.g., Cloud Run jobs). |
39 | 39 | load_dotenv(override=False) |
40 | 40 |
|
| 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 | + |
41 | 54 | from alembic import command |
42 | 55 | from alembic.config import Config |
43 | 56 |
|
@@ -690,20 +703,30 @@ def _transfer_parallel( |
690 | 703 | def main(): |
691 | 704 | message("START--------------------------------------") |
692 | 705 |
|
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 | + ) |
707 | 730 |
|
708 | 731 | metrics = Metrics() |
709 | 732 |
|
|
0 commit comments