feat: 添加 pgvector 初始化脚本和数据库 schema 初始化工具
- 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/
This commit is contained in:
parent
394ddfbc61
commit
3bd848ee36
|
|
@ -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 ==="
|
||||||
|
|
@ -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())
|
||||||
|
|
@ -13,6 +13,7 @@ services:
|
||||||
- "5432:5432"
|
- "5432:5432"
|
||||||
volumes:
|
volumes:
|
||||||
- postgres_data:/var/lib/postgresql/data
|
- postgres_data:/var/lib/postgresql/data
|
||||||
|
- ./backend/init-db.sh:/docker-entrypoint-initdb.d/01-install-pgvector.sh
|
||||||
healthcheck:
|
healthcheck:
|
||||||
test: ["CMD-SHELL", "pg_isready -U postgres -d geo_platform"]
|
test: ["CMD-SHELL", "pg_isready -U postgres -d geo_platform"]
|
||||||
interval: 5s
|
interval: 5s
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue