diff --git a/backend/Dockerfile b/backend/Dockerfile index 5280db82..a5516ddc 100644 --- a/backend/Dockerfile +++ b/backend/Dockerfile @@ -1,4 +1,4 @@ -FROM python:3.12-slim-bookworm +FROM python:3.12-slim-bookworm WORKDIR /app @@ -10,11 +10,12 @@ RUN apt-get update \ && rm -rf /var/lib/apt/lists/* # Install Python dependencies -COPY requirements.txt . +COPY backend/requirements.txt . RUN pip install --no-cache-dir -r requirements.txt -# Copy application code -COPY . . +# Copy backend code and bundled Text2SQL schema docs +COPY backend/ . +COPY docs /app/docs ENV ORACLE_HOME=/app/oracle_client/instantclient_19_31 ENV LD_LIBRARY_PATH=/app/oracle_client/instantclient_19_31:/app/oracle_client:${LD_LIBRARY_PATH} diff --git a/backend/app/services/text2sql.py b/backend/app/services/text2sql.py index 8efe113d..0eb22d1d 100644 --- a/backend/app/services/text2sql.py +++ b/backend/app/services/text2sql.py @@ -154,15 +154,41 @@ class Text2SQLService: @classmethod def get_schema(cls): if cls._schema_text is None: - try: - # docs is at the project root: c:\Users\PC\Desktop\HX工作目录\hxops\hxops\docs - # This file is in backend/app/services/text2sql.py - base_dir = cls._project_root() - doc_path = os.path.join(base_dir, 'docs', '(表+字段).md') - with open(doc_path, 'r', encoding='utf-8') as f: - cls._schema_text = f.read() - except Exception as e: - cls._schema_text = f"Failed to load schema: {e}" + schema_filename = '(表+字段).md' + env_path = (os.getenv("TEXT2SQL_SCHEMA_PATH") or "").strip() + candidate_paths = [] + if env_path: + candidate_paths.append(env_path) + + candidate_paths.extend([ + os.path.join(cls._backend_dir(), 'docs', schema_filename), + os.path.join(cls._project_root(), 'docs', schema_filename), + os.path.join(os.getcwd(), 'docs', schema_filename), + ]) + + unique_paths = [] + seen = set() + for path in candidate_paths: + normalized = os.path.normpath(path) + if normalized not in seen: + seen.add(normalized) + unique_paths.append(path) + + for doc_path in unique_paths: + try: + if not os.path.exists(doc_path): + continue + with open(doc_path, 'r', encoding='utf-8') as f: + cls._schema_text = f.read() + cls._log("info", f"Loaded Text2SQL schema from {doc_path}") + break + except Exception as exc: + cls._log("warning", f"Failed to load Text2SQL schema from {doc_path}: {exc}") + + if cls._schema_text is None: + tried = ", ".join(unique_paths) + cls._schema_text = f"Failed to load schema: schema file not found. Tried: {tried}" + cls._log("warning", cls._schema_text) return cls._schema_text @staticmethod diff --git a/docker-compose.yml b/docker-compose.yml index 7be94982..36710f7d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,12 +1,12 @@ -services: +services: # ============================================================================== # hxops-backend: Flask 后端服务 (端口: 5000) # ============================================================================== hxops-backend: image: ${BACKEND_IMAGE:-hxops-backend:local} build: - context: ./backend - dockerfile: Dockerfile + context: . + dockerfile: backend/Dockerfile container_name: hxops-backend restart: always # 与 scripts/deploy_compose.sh 使用的 .env. 对齐(通过 COMPOSE_ENV_BASENAME)