Bladeren bron

修复db问题

lxylxy123321 2 dagen geleden
bovenliggende
commit
70af767e12
2 gewijzigde bestanden met toevoegingen van 33 en 24 verwijderingen
  1. 26 8
      backend/app/core/db.py
  2. 7 16
      result.txt

+ 26 - 8
backend/app/core/db.py

@@ -49,22 +49,40 @@ async def init_db():
 async def _migrate_tables():
     """补齐表新增字段(兼容已有数据库)。"""
     from sqlalchemy import text
+    # 用 DO 块判断列是否存在,避免 ADD COLUMN 重复列导致事务 abort
     alter_stmts = [
-        # deploy_tasks 补齐字段
-        "ALTER TABLE deploy_tasks ADD COLUMN deploy_mode VARCHAR(16) DEFAULT 'export'",
-        "ALTER TABLE deploy_tasks ADD COLUMN endpoint_url VARCHAR(256)",
-        "ALTER TABLE deploy_tasks ADD COLUMN port INTEGER",
-        "ALTER TABLE deploy_tasks ADD COLUMN pid VARCHAR(32)",
-        "ALTER TABLE deploy_tasks ADD COLUMN user_id VARCHAR(36)",
+        "DO $$ BEGIN "
+        "IF NOT EXISTS (SELECT 1 FROM information_schema.columns "
+        "WHERE table_name='deploy_tasks' AND column_name='deploy_mode') THEN "
+        "ALTER TABLE deploy_tasks ADD COLUMN deploy_mode VARCHAR(16) DEFAULT 'export'; "
+        "END IF; END $$",
+        "DO $$ BEGIN "
+        "IF NOT EXISTS (SELECT 1 FROM information_schema.columns "
+        "WHERE table_name='deploy_tasks' AND column_name='endpoint_url') THEN "
+        "ALTER TABLE deploy_tasks ADD COLUMN endpoint_url VARCHAR(256); "
+        "END IF; END $$",
+        "DO $$ BEGIN "
+        "IF NOT EXISTS (SELECT 1 FROM information_schema.columns "
+        "WHERE table_name='deploy_tasks' AND column_name='port') THEN "
+        "ALTER TABLE deploy_tasks ADD COLUMN port INTEGER; "
+        "END IF; END $$",
+        "DO $$ BEGIN "
+        "IF NOT EXISTS (SELECT 1 FROM information_schema.columns "
+        "WHERE table_name='deploy_tasks' AND column_name='pid') THEN "
+        "ALTER TABLE deploy_tasks ADD COLUMN pid VARCHAR(32); "
+        "END IF; END $$",
+        "DO $$ BEGIN "
+        "IF NOT EXISTS (SELECT 1 FROM information_schema.columns "
+        "WHERE table_name='deploy_tasks' AND column_name='user_id') THEN "
+        "ALTER TABLE deploy_tasks ADD COLUMN user_id VARCHAR(36); "
+        "END IF; END $$",
     ]
     engine = _get_engine()
     for stmt in alter_stmts:
         try:
-            # 每条 ALTER 在独立事务中执行,避免一条失败导致整个事务 abort
             async with engine.begin() as conn:
                 await conn.execute(text(stmt))
         except Exception:
-            # 列已存在或其他错误,跳过即可
             pass
 
 

+ 7 - 16
result.txt

@@ -1,11 +1,7 @@
-lq@lq:~/Fine-tuning$ curl -s http://localhost:8000/api/v1/deployment/c43bf8e3-d92d-4266-be19-0212c3e1b6  21/status | python3 -m json.tool
-b^HExpecting value: line 1 column 1 (char 0)
-lq@lq:~/Fine-tuning$ sudo docker logs -f finetune-backend
-[sudo] password for lq: 
+
+ERROR:    Application startup failed. Exiting.
 => Syncing backend code to compute node 192.168.91.253 ...
-Warning: Permanently added '192.168.91.253' (ED25519) to the list of known hosts.
 sending incremental file list
-./
 .dockerignore
 .env.docker
 .env.example
@@ -15,11 +11,11 @@ entrypoint.sh
 main.py
 pyproject.toml
 requirements.txt
+__pycache__/main.cpython-310.pyc
 app/__init__.py
 app/config.py
 app/__pycache__/__init__.cpython-310.pyc
 app/__pycache__/config.cpython-310.pyc
-app/api/
 app/api/__init__.py
 app/api/api_keys.py
 app/api/auth.py
@@ -30,7 +26,6 @@ app/api/inference.py
 app/api/models.py
 app/api/sample_center.py
 app/api/training.py
-app/api/__pycache__/
 app/api/__pycache__/__init__.cpython-310.pyc
 app/api/__pycache__/api_keys.cpython-310.pyc
 app/api/__pycache__/auth.cpython-310.pyc
@@ -41,7 +36,6 @@ app/api/__pycache__/inference.cpython-310.pyc
 app/api/__pycache__/models.cpython-310.pyc
 app/api/__pycache__/sample_center.cpython-310.pyc
 app/api/__pycache__/training.cpython-310.pyc
-app/core/
 app/core/__init__.py
 app/core/auth.py
 app/core/background_tasks.py
@@ -56,7 +50,6 @@ app/core/remote_executor.py
 app/core/security.py
 app/core/sso_client.py
 app/core/websocket.py
-app/core/__pycache__/
 app/core/__pycache__/__init__.cpython-310.pyc
 app/core/__pycache__/auth.cpython-310.pyc
 app/core/__pycache__/background_tasks.cpython-310.pyc
@@ -84,7 +77,6 @@ app/peft/__init__.py
 app/peft/__pycache__/__init__.cpython-310.pyc
 app/preprocessors/__init__.py
 app/preprocessors/__pycache__/__init__.cpython-310.pyc
-app/schemas/
 app/schemas/__init__.py
 app/schemas/background_task.py
 app/schemas/common.py
@@ -95,7 +87,6 @@ app/schemas/model.py
 app/schemas/model_test.py
 app/schemas/sample_center.py
 app/schemas/training.py
-app/schemas/__pycache__/
 app/schemas/__pycache__/__init__.cpython-310.pyc
 app/schemas/__pycache__/background_task.cpython-310.pyc
 app/schemas/__pycache__/common.cpython-310.pyc
@@ -106,7 +97,6 @@ app/schemas/__pycache__/model.cpython-310.pyc
 app/schemas/__pycache__/model_test.cpython-310.pyc
 app/schemas/__pycache__/sample_center.cpython-310.pyc
 app/schemas/__pycache__/training.cpython-310.pyc
-app/services/
 app/services/api_key_service.py
 app/services/dataset_service.py
 app/services/deploy_service.py
@@ -116,7 +106,6 @@ app/services/model_service.py
 app/services/model_test_service.py
 app/services/sample_center_service.py
 app/services/training_service.py
-app/services/__pycache__/
 app/services/__pycache__/api_key_service.cpython-310.pyc
 app/services/__pycache__/dataset_service.cpython-310.pyc
 app/services/__pycache__/deploy_service.cpython-310.pyc
@@ -127,8 +116,8 @@ app/services/__pycache__/model_test_service.cpython-310.pyc
 app/services/__pycache__/sample_center_service.cpython-310.pyc
 app/services/__pycache__/training_service.cpython-310.pyc
 
-sent 8,330 bytes  received 6,959 bytes  826.43 bytes/sec
-total size is 517,664  speedup is 33.86
+sent 8,421 bytes  received 6,982 bytes  832.59 bytes/sec
+total size is 520,877  speedup is 33.82
 => Sync done.
 INFO:     Started server process [1]
 INFO:     Waiting for application startup.
@@ -259,3 +248,5 @@ Traceback (most recent call last):
 sqlalchemy.exc.DBAPIError: (sqlalchemy.dialects.postgresql.asyncpg.Error) <class 'asyncpg.exceptions.InFailedSQLTransactionError'>: current transaction is aborted, commands ignored until end of transaction block
 [SQL: ALTER TABLE deploy_tasks ADD COLUMN endpoint_url VARCHAR(256)]
 (Background on this error at: https://sqlalche.me/e/20/dbapi)
+
+ERROR:    Application startup failed. Exiting.