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