|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Post-create script for MSSQL Python Driver devcontainer |
| 4 | +set -e |
| 5 | + |
| 6 | +echo "🚀 Setting up MSSQL Python Driver development environment..." |
| 7 | + |
| 8 | +# Install Python packages from requirements.txt |
| 9 | +echo "📦 Installing Python packages..." |
| 10 | +pip install --upgrade pip setuptools wheel |
| 11 | +pip install -r requirements.txt |
| 12 | + |
| 13 | +# Create symlink for 'python' command (build.sh expects it) |
| 14 | +echo "🔗 Creating python symlink..." |
| 15 | +sudo ln -sf $(which python3) /usr/local/bin/python |
| 16 | + |
| 17 | +# Set up useful shell aliases (for both bash and zsh) |
| 18 | +echo "⚡ Setting up aliases..." |
| 19 | +cat > ~/.shell_aliases << 'EOF' |
| 20 | +# MSSQL Python Driver development aliases |
| 21 | +alias build='cd /workspaces/mssql-python/mssql_python/pybind && ./build.sh && cd /workspaces/mssql-python' |
| 22 | +alias test='python -m pytest -v' |
| 23 | +EOF |
| 24 | + |
| 25 | +# Ensure aliases are sourced in both shells |
| 26 | +grep -qxF 'source ~/.shell_aliases' ~/.bashrc 2>/dev/null || echo 'source ~/.shell_aliases' >> ~/.bashrc |
| 27 | +grep -qxF 'source ~/.shell_aliases' ~/.zshrc 2>/dev/null || echo 'source ~/.shell_aliases' >> ~/.zshrc |
| 28 | + |
| 29 | +# Verify environment |
| 30 | +echo "" |
| 31 | +echo "🔍 Verifying environment..." |
| 32 | +python --version |
| 33 | +pip --version |
| 34 | +cmake --version |
| 35 | +if command -v sqlcmd &> /dev/null; then |
| 36 | + echo "✅ sqlcmd available" |
| 37 | +else |
| 38 | + echo "❌ sqlcmd not found" |
| 39 | +fi |
| 40 | + |
| 41 | +# Build the C++ extension |
| 42 | +echo "" |
| 43 | +echo "🔨 Building C++ extension..." |
| 44 | +if cd mssql_python/pybind && ./build.sh && cd ../..; then |
| 45 | + echo "✅ C++ extension built successfully" |
| 46 | +else |
| 47 | + echo "❌ C++ extension build failed!" |
| 48 | + exit 1 |
| 49 | +fi |
| 50 | + |
| 51 | +# Generate random password for SQL Server |
| 52 | +echo "" |
| 53 | +echo "Generating SQL Server password..." |
| 54 | +SA_PASSWORD="$(openssl rand -base64 16 | tr -dc 'A-Za-z0-9' | head -c 16)Aa1!" |
| 55 | +echo "$SA_PASSWORD" > /tmp/.sqlserver_sa_password |
| 56 | +chmod 600 /tmp/.sqlserver_sa_password |
| 57 | + |
| 58 | +# Start SQL Server container (use Azure SQL Edge for ARM64 compatibility) |
| 59 | +# This is optional - if Docker-in-Docker fails, the devcontainer still works |
| 60 | +echo "" |
| 61 | +echo "Starting SQL Server container (optional)..." |
| 62 | + |
| 63 | +ARCH=$(uname -m) |
| 64 | +if [[ "$ARCH" == "aarch64" || "$ARCH" == "arm64" ]]; then |
| 65 | + echo "Detected ARM64 - using Azure SQL Edge..." |
| 66 | + docker run -e 'ACCEPT_EULA=Y' -e "MSSQL_SA_PASSWORD=$SA_PASSWORD" \ |
| 67 | + -p 1433:1433 --name sqlserver \ |
| 68 | + -d mcr.microsoft.com/azure-sql-edge:latest && SQL_STARTED=true || SQL_STARTED=false |
| 69 | +else |
| 70 | + echo "Detected x86_64 - using SQL Server 2025..." |
| 71 | + docker run -e 'ACCEPT_EULA=Y' -e "MSSQL_SA_PASSWORD=$SA_PASSWORD" \ |
| 72 | + -p 1433:1433 --name sqlserver \ |
| 73 | + -d mcr.microsoft.com/mssql/server:2025-latest && SQL_STARTED=true || SQL_STARTED=false |
| 74 | +fi |
| 75 | + |
| 76 | +if [ "$SQL_STARTED" = "true" ]; then |
| 77 | + echo "Waiting for SQL Server to start..." |
| 78 | + sleep 15 |
| 79 | +else |
| 80 | + echo "WARNING: SQL Server container failed to start (Docker issue)" |
| 81 | + echo " You can start it manually later with:" |
| 82 | + echo " docker run -e 'ACCEPT_EULA=Y' -e 'MSSQL_SA_PASSWORD=YourPassword123!' -p 1433:1433 --name sqlserver -d mcr.microsoft.com/azure-sql-edge:latest" |
| 83 | +fi |
| 84 | + |
| 85 | +# Set DB_CONNECTION_STRING environment variable (persist across all terminals) |
| 86 | +DB_CONNECTION_STRING="Server=localhost,1433;Database=master;UID=sa;PWD=$SA_PASSWORD;TrustServerCertificate=Yes;Encrypt=Yes" |
| 87 | + |
| 88 | +# Write to /etc/environment for system-wide persistence |
| 89 | +echo "DB_CONNECTION_STRING=\"$DB_CONNECTION_STRING\"" | sudo tee -a /etc/environment > /dev/null |
| 90 | + |
| 91 | +# Also add to shell rc files for immediate availability in new terminals |
| 92 | +echo "export DB_CONNECTION_STRING=\"$DB_CONNECTION_STRING\"" >> ~/.bashrc |
| 93 | +echo "export DB_CONNECTION_STRING=\"$DB_CONNECTION_STRING\"" >> ~/.zshrc |
| 94 | + |
| 95 | +# Export for current session |
| 96 | +export DB_CONNECTION_STRING |
| 97 | + |
| 98 | +# Display completion message and next steps |
| 99 | +echo "" |
| 100 | +echo "==============================================" |
| 101 | +echo "🎉 Dev environment setup complete!" |
| 102 | +echo "==============================================" |
| 103 | +echo "" |
| 104 | +echo "📦 What's ready:" |
| 105 | +echo " ✅ C++ extension built" |
| 106 | +echo " ✅ SQL Server running (localhost:1433)" |
| 107 | +echo " ✅ DB_CONNECTION_STRING set in environment" |
| 108 | +echo "" |
| 109 | +echo "🚀 Quick start - just type these commands:" |
| 110 | +echo " python main.py → Test the connection" |
| 111 | +echo " test → Run all pytest tests" |
| 112 | +echo " build → Rebuild C++ extension" |
| 113 | +echo "" |
| 114 | +echo "==============================================" |
| 115 | +echo "" |
0 commit comments