Read the user-provided .env file and inject auto-generated values. The deploy script creates the SQL server/database and storage account, so those env values must point at the resources it provisioned. If the .env already contains either variable with a different value, it is overw
(
*,
env_file: Path,
sql_server_fqdn: str,
sql_database_name: str,
storage_container_url: str,
)
| 442 | |
| 443 | |
| 444 | def _prepare_env_content( |
| 445 | *, |
| 446 | env_file: Path, |
| 447 | sql_server_fqdn: str, |
| 448 | sql_database_name: str, |
| 449 | storage_container_url: str, |
| 450 | ) -> str: |
| 451 | """ |
| 452 | Read the user-provided .env file and inject auto-generated values. |
| 453 | |
| 454 | The deploy script creates the SQL server/database and storage account, so |
| 455 | those env values must point at the resources it provisioned. If the .env |
| 456 | already contains either variable with a different value, it is overwritten |
| 457 | with a warning. |
| 458 | |
| 459 | Args: |
| 460 | env_file (Path): Path to the user-provided .env file. |
| 461 | sql_server_fqdn (str): The SQL server FQDN (e.g., myserver.database.windows.net). |
| 462 | sql_database_name (str): The SQL database name. |
| 463 | storage_container_url (str): The blob container URL |
| 464 | (e.g., https://myaccount.blob.core.windows.net/dbdata). |
| 465 | |
| 466 | Returns: |
| 467 | str: The .env content with the correct SQL connection string and |
| 468 | storage container URL injected. |
| 469 | """ |
| 470 | content = env_file.read_text(encoding="utf-8") |
| 471 | |
| 472 | # SQL connection string |
| 473 | sql_value = _SQL_CONN_TEMPLATE.format( |
| 474 | server_fqdn=sql_server_fqdn, |
| 475 | database_name=sql_database_name, |
| 476 | ) |
| 477 | sql_line = f"AZURE_SQL_DB_CONNECTION_STRING={sql_value}" |
| 478 | sql_match = _SQL_CONN_RE.search(content) |
| 479 | if sql_match: |
| 480 | existing_value = sql_match.group(0).split("=", 1)[1].strip().strip("\"'") |
| 481 | if existing_value == sql_value: |
| 482 | logger.info("AZURE_SQL_DB_CONNECTION_STRING already correct in .env") |
| 483 | else: |
| 484 | # Log only server/database, not the full connection string (may contain credentials). |
| 485 | logger.warning( |
| 486 | "Overwriting AZURE_SQL_DB_CONNECTION_STRING in .env to point at %s/%s", |
| 487 | sql_server_fqdn, |
| 488 | sql_database_name, |
| 489 | ) |
| 490 | content = _SQL_CONN_RE.sub(sql_line, content) |
| 491 | else: |
| 492 | logger.info("Appending AZURE_SQL_DB_CONNECTION_STRING to .env") |
| 493 | if not content.endswith("\n"): |
| 494 | content += "\n" |
| 495 | content += f"\n# ─── Database (auto-injected by deploy script) ───\n{sql_line}\n" |
| 496 | |
| 497 | # Storage container URL |
| 498 | storage_line = f"AZURE_STORAGE_ACCOUNT_DB_DATA_CONTAINER_URL={storage_container_url}" |
| 499 | storage_match = _STORAGE_URL_RE.search(content) |
| 500 | if storage_match: |
| 501 | existing_value = storage_match.group(0).split("=", 1)[1].strip().strip("\"'") |