|
| 1 | +#!/usr/bin/env bash |
| 2 | +# ------------------------------------------------------------------- |
| 3 | +# Initializes the SQL Server instance running in the devcontainer |
| 4 | +# companion service with the test databases required by the test suite. |
| 5 | +# ------------------------------------------------------------------- |
| 6 | +set -euo pipefail |
| 7 | + |
| 8 | +SQL_HOST="${SQL_SERVER_HOST:-sqlserver}" |
| 9 | +SQL_PORT="${SQL_SERVER_PORT:-1433}" |
| 10 | +SA_PASSWORD_FILE="${SQL_SERVER_SA_PASSWORD_FILE:-/sql-config/sa-password}" |
| 11 | + |
| 12 | +# Wait for the auto-generated SA password file from the sqlserver service. |
| 13 | +# The sqlserver entrypoint writes this file on startup; the shared volume |
| 14 | +# may not be visible immediately when postCreateCommand begins. |
| 15 | +echo "Waiting for SA password file at ${SA_PASSWORD_FILE}..." |
| 16 | +for i in $(seq 1 30); do |
| 17 | + if [ -f "$SA_PASSWORD_FILE" ]; then |
| 18 | + break |
| 19 | + fi |
| 20 | + if [ "$i" -eq 30 ]; then |
| 21 | + echo "ERROR: SA password file not found at $SA_PASSWORD_FILE after 60 s" >&2 |
| 22 | + exit 1 |
| 23 | + fi |
| 24 | + sleep 2 |
| 25 | +done |
| 26 | +SA_PASSWORD="$(cat "$SA_PASSWORD_FILE")" |
| 27 | + |
| 28 | +REPO_ROOT="$(cd "$(dirname "$0")/.." && pwd)" |
| 29 | + |
| 30 | +# Write test config file so the test suite can find the connection string. |
| 31 | +CONFIG_DIR="${REPO_ROOT}/src/Microsoft.Data.SqlClient/tests/tools/Microsoft.Data.SqlClient.TestUtilities" |
| 32 | +CONFIG_DEFAULT_FILE="${CONFIG_DIR}/config.default.json" |
| 33 | +CONFIG_FILE="${CONFIG_DIR}/config.json" |
| 34 | +echo "Writing test config to ${CONFIG_FILE} (based on ${CONFIG_DEFAULT_FILE})..." |
| 35 | +TCP_CONN_STR="Data Source=tcp:${SQL_HOST},${SQL_PORT};Database=Northwind;User Id=sa;Password=${SA_PASSWORD};Encrypt=false;TrustServerCertificate=true" |
| 36 | +# config.default.json contains JS-style comments (// ...) which are not valid JSON. |
| 37 | +# Strip single-line comments before feeding to jq. |
| 38 | +sed 's|//.*||' "${CONFIG_DEFAULT_FILE}" \ |
| 39 | + | jq --arg cs "${TCP_CONN_STR}" \ |
| 40 | + '.TCPConnectionString = $cs | .NPConnectionString = "" | .SupportsIntegratedSecurity = false' \ |
| 41 | + > "${CONFIG_FILE}" |
| 42 | +echo "Test config written." |
| 43 | + |
| 44 | +echo "Waiting for SQL Server at ${SQL_HOST}:${SQL_PORT} to become available..." |
| 45 | +for i in $(seq 1 30); do |
| 46 | + if sqlcmd -S "${SQL_HOST},${SQL_PORT}" -U sa -P "${SA_PASSWORD}" -C -Q "SELECT 1" &>/dev/null; then |
| 47 | + break |
| 48 | + fi |
| 49 | + if [ "$i" -eq 30 ]; then |
| 50 | + echo "ERROR: SQL Server did not become available in time." >&2 |
| 51 | + exit 1 |
| 52 | + fi |
| 53 | + sleep 2 |
| 54 | +done |
| 55 | +echo "SQL Server is ready." |
| 56 | + |
| 57 | +# Ensure Northwind database exists |
| 58 | +echo "Ensuring Northwind database exists..." |
| 59 | +DB_EXISTS_RAW="$(sqlcmd -S "${SQL_HOST},${SQL_PORT}" -U sa -P "${SA_PASSWORD}" -C -h -1 -W \ |
| 60 | + -Q "SET NOCOUNT ON; SELECT COUNT(*) FROM sys.databases WHERE name = N'Northwind';" | tr -d '\r')" |
| 61 | +DB_EXISTS="$(echo "$DB_EXISTS_RAW" | tr -d ' ')" |
| 62 | +if [ "$DB_EXISTS" = "0" ]; then |
| 63 | + echo "Northwind database not found, creating..." |
| 64 | + sqlcmd -S "${SQL_HOST},${SQL_PORT}" -U sa -P "${SA_PASSWORD}" -C \ |
| 65 | + -i "${REPO_ROOT}/tools/testsql/createNorthwindDb.sql" |
| 66 | + echo "Northwind database created." |
| 67 | +else |
| 68 | + echo "Northwind database already exists; skipping creation script." |
| 69 | +fi |
| 70 | + |
| 71 | +echo "SQL Server test database setup complete." |
| 72 | +echo "" |
| 73 | +echo "Connection info:" |
| 74 | +echo " Host: ${SQL_HOST}" |
| 75 | +echo " Port: ${SQL_PORT}" |
| 76 | +echo " User: sa" |
| 77 | +echo " Password: ${SA_PASSWORD}" |
| 78 | +echo "" |
| 79 | +echo "Example sqlcmd:" |
| 80 | +echo " sqlcmd -S ${SQL_HOST},${SQL_PORT} -U sa -P '${SA_PASSWORD}' -C" |
0 commit comments