{"record":{"id":"147cf5f06e63265b","repo":"iflytek/astron-agent","slug":"missing-required-environment-variables-for-alembic-missing","errorCode":null,"errorMessage":"Missing required environment variables for Alembic: {missing}","messagePattern":"Missing required environment variables for Alembic: (.+?)","errorType":"console","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"core/plugin/link/alembic/env.py","lineNumber":41,"sourceCode":"    host = os.getenv(\"MYSQL_HOST\")\n    port = os.getenv(\"MYSQL_PORT\")\n    user = os.getenv(\"MYSQL_USER\")\n    password = os.getenv(\"MYSQL_PASSWORD\")\n    db = os.getenv(\"MYSQL_DB\")\n\n    missing = [\n        key\n        for key, value in [\n            (\"MYSQL_HOST\", host),\n            (\"MYSQL_PORT\", port),\n            (\"MYSQL_USER\", user),\n            (\"MYSQL_PASSWORD\", password),\n            (\"MYSQL_DB\", db),\n        ]\n        if not value\n    ]\n    if missing:\n        raise ValueError(\n            \"Missing required environment variables for Alembic: \" + \", \".join(missing)\n        )\n\n    return f\"mysql+pymysql://{user}:{password}@{host}:{port}/{db}\"\n\n\nconfig.set_main_option(\"sqlalchemy.url\", get_database_url())\n\n\ndef get_metadata():  # type: ignore[no-untyped-def]\n    return SQLModel.metadata\n\n\ndef include_object(\n    object: SchemaItem,\n    name: str | None,\n    type_: Literal[\n        \"schema\",","sourceCodeStart":23,"sourceCodeEnd":59,"githubUrl":"https://github.com/iflytek/astron-agent/blob/5e758547a83371a5a4b29dadf4ac03e8dd527635/core/plugin/link/alembic/env.py#L23-L59","documentation":"get_database_url builds the SQLAlchemy Alembic database URL from required MYSQL_* environment variables. It collects any of MYSQL_HOST, MYSQL_PORT, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DB that are empty or unset and raises ValueError listing exactly which ones are missing, preventing Alembic from forming an invalid connection string.","triggerScenarios":"Running `alembic upgrade head` (or any alembic command importing env.py) when one or more of MYSQL_HOST/MYSQL_PORT/MYSQL_USER/MYSQL_PASSWORD/MYSQL_DB are unset or empty in the process environment.","commonSituations":"Forgetting to source the .env file before running migrations; running alembic in CI without injecting the DB secrets; renamed variables between environments; empty-string values in a compose file that satisfy os.getenv but fail the truthiness check.","solutions":["Export all five required variables (MYSQL_HOST, MYSQL_PORT, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DB) in the shell or environment running alembic","Use `set -a; source .env; set +a` or `export $(grep -v '^#' .env | xargs)` before invoking alembic","In Docker Compose/CI, pass the variables via env_file or secrets instead of relying on the shell","Check the error message's `missing` list — it names the exact variables to add"],"exampleFix":"// before\n$ alembic upgrade head\nValueError: Missing required environment variables for Alembic: MYSQL_PASSWORD, MYSQL_DB\n// after\n$ set -a; source ./mysql.env; set +a\n$ alembic upgrade head   # proceeds with mysql+pymysql://user:***@host:3306/db","handlingStrategy":"validation","validationCode":"import os\nrequired = ['MYSQL_HOST','MYSQL_PORT','MYSQL_USER','MYSQL_PASSWORD','MYSQL_DB']\nmissing = [k for k in required if not os.getenv(k)]\nif missing:\n    raise SystemExit(f'Missing env vars for Alembic: {\", \".join(missing)}')","typeGuard":null,"tryCatchPattern":"try:\n    subprocess.run(['alembic', 'upgrade', 'head'], check=True, env=env_with_mysql_vars)\nexcept ValueError as e:\n    print('Alembic env incomplete:', e)","preventionTips":["Always source the mysql env file before running alembic","Inject DB secrets via env_file/secrets in CI and containers, never rely on the interactive shell","Keep the five MYSQL_* variable names consistent across environments","Guard empty-string values — treat them as unset"],"tags":["config","env","database","alembic"],"backgroundTag":"missing-env-var","analyzedSha":"5e758547a83371a5a4b29dadf4ac03e8dd527635","analyzedAt":"2026-09-12T08:03:51.356Z","contentChangedAt":"2026-09-12T08:03:51.356Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}