#!/bin/bash # Database Migration Script using psql # Executes the complete 001-schema.sql file set -e # Exit on any error # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color # Load environment variables if [ -f .env ]; then export $(cat .env | grep -v '^#' | xargs) fi # Database connection parameters DB_HOST=${POSTGRES_HOST:-localhost} DB_PORT=${POSTGRES_PORT:-5432} DB_NAME=${POSTGRES_DB:-dev_pipeline} DB_USER=${POSTGRES_USER:-pipeline_admin} DB_PASSWORD=${POSTGRES_PASSWORD:-secure_pipeline_2024} # Schema file SCHEMA_FILE="001-schema.sql" echo -e "${BLUE}🔧 AI Repository Analysis Database Migration${NC}" echo "==================================================" echo -e "Database: ${YELLOW}${DB_NAME}@${DB_HOST}:${DB_PORT}${NC}" echo -e "User: ${YELLOW}${DB_USER}${NC}" echo -e "Schema file: ${YELLOW}${SCHEMA_FILE}${NC}" echo "" # Check if psql is available if ! command -v psql &> /dev/null; then echo -e "${RED}❌ psql command not found!${NC}" echo "Please install PostgreSQL client tools:" echo " Ubuntu/Debian: sudo apt-get install postgresql-client" echo " CentOS/RHEL: sudo yum install postgresql" echo " macOS: brew install postgresql" exit 1 fi # Check if schema file exists if [ ! -f "$SCHEMA_FILE" ]; then echo -e "${RED}❌ Schema file not found: ${SCHEMA_FILE}${NC}" exit 1 fi echo -e "${BLUE}• Executing migration...${NC}" # Set password for psql export PGPASSWORD="$DB_PASSWORD" # Run migration if psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" \ -f "$SCHEMA_FILE" \ -v ON_ERROR_STOP=1 \ --echo-errors \ --echo-queries; then echo -e "${GREEN}✅ Migration completed successfully!${NC}" # Verify migration echo -e "${BLUE}• Verifying migration...${NC}" TABLES=$(psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" -t -c " SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' AND table_name IN ('code_embeddings', 'query_embeddings', 'knowledge_embeddings', 'repository_metadata', 'analysis_sessions', 'file_analysis_history') ORDER BY table_name; " | tr -d ' ') if [ -n "$TABLES" ]; then TABLE_COUNT=$(echo "$TABLES" | wc -l) echo -e "${GREEN}✓ Found ${TABLE_COUNT} core tables: ${TABLES}${NC}" else echo -e "${YELLOW}⚠ Could not verify table creation${NC}" fi # Check for pgvector extension VECTOR_AVAILABLE=$(psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" -t -c " SELECT EXISTS(SELECT 1 FROM pg_extension WHERE extname = 'vector'); " | tr -d ' ') if [ "$VECTOR_AVAILABLE" = "t" ]; then echo -e "${GREEN}✓ pgvector extension is available${NC}" else echo -e "${YELLOW}⚠ pgvector extension not available - vector operations will be limited${NC}" fi echo "" echo -e "${GREEN}🚀 Database migration completed successfully!${NC}" echo -e "${GREEN}📊 Production-level database ready for AI repository analysis${NC}" else echo -e "${RED}❌ Migration failed!${NC}" exit 1 fi