From 3bd848ee3638be982a6f14aafa5900690eee0e67 Mon Sep 17 00:00:00 2001 From: chiguyong Date: Mon, 1 Jun 2026 14:37:02 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=20pgvector=20?= =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E8=84=9A=E6=9C=AC=E5=92=8C=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=BA=93=20schema=20=E5=88=9D=E5=A7=8B=E5=8C=96?= =?UTF-8?q?=E5=B7=A5=E5=85=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - backend/init-db.sh: Docker 首次启动时编译安装 pgvector v0.5.1 - backend/init_schema.py: create_all + stamp head 初始化数据库 - docker-compose.yml: 挂载 init-db.sh 到 /docker-entrypoint-initdb.d/ --- backend/init-db.sh | 29 +++++++++++++++++++++++++++++ backend/init_schema.py | 40 ++++++++++++++++++++++++++++++++++++++++ docker-compose.yml | 1 + 3 files changed, 70 insertions(+) create mode 100755 backend/init-db.sh create mode 100644 backend/init_schema.py diff --git a/backend/init-db.sh b/backend/init-db.sh new file mode 100755 index 0000000..63ab761 --- /dev/null +++ b/backend/init-db.sh @@ -0,0 +1,29 @@ +#!/bin/bash +set -e + +echo "=== Installing pgvector extension for PostgreSQL ===" + +echo "[1/5] Installing build dependencies..." +apk add --no-cache build-base git clang llvm-dev postgresql-dev + +echo "[2/5] Cloning pgvector v0.5.1..." +cd /tmp +rm -rf pgvector +git clone --branch v0.5.1 --depth 1 https://github.com/pgvector/pgvector.git + +echo "[3/5] Compiling and installing pgvector..." +cd /tmp/pgvector +make +make install + +echo "[4/5] Cleaning up build dependencies..." +cd / +rm -rf /tmp/pgvector +apk del build-base git clang llvm-dev postgresql-dev + +echo "[5/5] Creating vector extension in database..." +psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL + CREATE EXTENSION IF NOT EXISTS vector; +EOSQL + +echo "=== pgvector installation complete ===" diff --git a/backend/init_schema.py b/backend/init_schema.py new file mode 100644 index 0000000..ea1755e --- /dev/null +++ b/backend/init_schema.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python3 +"""Initialize database schema: create all tables and stamp alembic version.""" + +import asyncio +import subprocess +import sys + +from app.config import settings +from app.database import Base, engine +from app.models import * # noqa: F401, F403 + + +async def create_tables(): + async with engine.begin() as conn: + await conn.run_sync(Base.metadata.create_all) + print("All tables created successfully.") + + +def stamp_alembic(): + result = subprocess.run( + ["alembic", "stamp", "head"], + capture_output=True, + text=True, + ) + if result.returncode != 0: + print(f"alembic stamp failed:\n{result.stderr}", file=sys.stderr) + sys.exit(1) + print("Alembic version stamped to head.") + + +async def main(): + print(f"Using DATABASE_URL: {settings.DATABASE_URL.split('@')[-1]}") + await create_tables() + stamp_alembic() + await engine.dispose() + print("Schema initialization complete.") + + +if __name__ == "__main__": + asyncio.run(main()) diff --git a/docker-compose.yml b/docker-compose.yml index 70de3ff..ab184f3 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -13,6 +13,7 @@ services: - "5432:5432" volumes: - postgres_data:/var/lib/postgresql/data + - ./backend/init-db.sh:/docker-entrypoint-initdb.d/01-install-pgvector.sh healthcheck: test: ["CMD-SHELL", "pg_isready -U postgres -d geo_platform"] interval: 5s