|
| 1 | +# go-mssqldb Development Container |
| 2 | + |
| 3 | +This folder contains the configuration for a VS Code Dev Container / GitHub Codespaces development environment for the go-mssqldb driver. |
| 4 | + |
| 5 | +> **⚠️ Security Note**: The SQL Server password for this devcontainer is provided via the `$SQLPASSWORD` environment variable and is a non-production, development-only default. |
| 6 | +> For the default devcontainer setup, the password value (`MssqlDriver@2025!`) is checked into this repository for convenience; override it via environment variables or Codespaces/CI secrets for non-local use. |
| 7 | +> When using VS Code's MSSQL extension, copy the value from `$SQLPASSWORD` when prompted. |
| 8 | +> Never expose port 1433 outside of localhost or use these credentials in any production or shared environment. |
| 9 | +
|
| 10 | +## What's Included |
| 11 | + |
| 12 | +- **Go 1.24** development environment with all necessary tools |
| 13 | +- **SQL Server 2025** (Developer Edition) running in a sidecar container with AI/vector capabilities |
| 14 | +- **Pre-configured VS Code extensions**: |
| 15 | + - Go (official extension) |
| 16 | + - MS SQL (for database management) |
| 17 | + - Docker |
| 18 | + - GitHub Copilot |
| 19 | + - GitLens |
| 20 | +- **SQL Server tools**: go-sqlcmd (uses this driver - dogfooding!) |
| 21 | +- **Go quality tools**: golangci-lint, gopls, delve debugger, staticcheck |
| 22 | + |
| 23 | +## Quick Start |
| 24 | + |
| 25 | +### Using VS Code (Recommended) |
| 26 | + |
| 27 | +1. Install the [Dev Containers extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers) |
| 28 | +2. Open this repository in VS Code |
| 29 | +3. When prompted, click **"Reopen in Container"**, or: |
| 30 | + - Press `F1` and select **"Dev Containers: Reopen in Container"** |
| 31 | +4. Wait for the container to build (first time takes ~5 minutes) |
| 32 | +5. Start developing! |
| 33 | + |
| 34 | +### Using GitHub Codespaces |
| 35 | + |
| 36 | +1. Click the green **"Code"** button on the repository |
| 37 | +2. Select **"Codespaces"** tab |
| 38 | +3. Click **"Create codespace on main"** (or your preferred branch) |
| 39 | +4. Wait for the environment to start |
| 40 | + |
| 41 | +## Running Tests |
| 42 | + |
| 43 | +Environment variables are pre-configured for running integration tests: |
| 44 | + |
| 45 | +```bash |
| 46 | +# Run all tests (includes SQL Server integration tests) |
| 47 | +go test ./... |
| 48 | + |
| 49 | +# Run unit tests only (no SQL Server required) |
| 50 | +go test ./msdsn ./internal/... ./integratedauth ./azuread -v |
| 51 | + |
| 52 | +# Run short tests |
| 53 | +go test -short ./... |
| 54 | +``` |
| 55 | + |
| 56 | +### Helpful Aliases |
| 57 | + |
| 58 | +After the container starts, these aliases are available: |
| 59 | + |
| 60 | +| Alias | Command | |
| 61 | +|-------|---------| |
| 62 | +| `gtest` | Run all tests | |
| 63 | +| `gtest-unit` | Run unit tests only | |
| 64 | +| `gtest-short` | Run short tests | |
| 65 | +| `gbuild` | Build all packages | |
| 66 | +| `gfmt` | Format code | |
| 67 | +| `gvet` | Run go vet | |
| 68 | +| `glint` | Run golangci-lint | |
| 69 | +| `test-db` | Test database connection | |
| 70 | +| `sql` | Connect to SQL Server (using go-sqlcmd) | |
| 71 | + |
| 72 | +## SQL Server Connection |
| 73 | + |
| 74 | +The SQL Server instance is accessible at: |
| 75 | + |
| 76 | +- **Server**: `localhost,1433` |
| 77 | +- **Username**: `sa` |
| 78 | +- **Password**: `MssqlDriver@2025!` |
| 79 | +- **Database**: `master` (default) or `GoDriverTest` (created for testing) |
| 80 | + |
| 81 | +### Connecting with go-sqlcmd |
| 82 | + |
| 83 | +The container includes [go-sqlcmd](https://github.com/microsoft/go-sqlcmd), which is built on this driver (dogfooding!): |
| 84 | + |
| 85 | +```bash |
| 86 | +# Using the alias |
| 87 | +sql |
| 88 | + |
| 89 | +# Or explicitly (-C trusts the self-signed certificate used by the devcontainer SQL Server) |
| 90 | +sqlcmd -S localhost -U sa -P "MssqlDriver@2025!" -C |
| 91 | +``` |
| 92 | + |
| 93 | +### VS Code SQL Extension |
| 94 | + |
| 95 | +The MSSQL extension is pre-configured with a connection profile named **"mssql-container"**. Click the SQL Server icon in the Activity Bar to connect. |
| 96 | + |
| 97 | +## Environment Variables |
| 98 | + |
| 99 | +The following environment variables are set automatically: |
| 100 | + |
| 101 | +| Variable | Value | |
| 102 | +|----------|-------| |
| 103 | +| `HOST` | `localhost` | |
| 104 | +| `SQLUSER` | `sa` | |
| 105 | +| `SQLPASSWORD` | `MssqlDriver@2025!` (default, overridable via Codespaces Secrets) | |
| 106 | +| `DATABASE` | `master` | |
| 107 | +| `SQLSERVER_DSN` | Full connection string | |
| 108 | + |
| 109 | +## Customization |
| 110 | + |
| 111 | +### Adding SQL Setup Scripts |
| 112 | + |
| 113 | +The `setup.sql` script in `.devcontainer/mssql/` is executed automatically when the container starts. To run additional SQL scripts, either add them to `setup.sql` or update `post-create.sh` to execute them explicitly. |
| 114 | + |
| 115 | +### Modifying the SA Password |
| 116 | + |
| 117 | +The devcontainer uses environment variable substitution for the password. To change it: |
| 118 | + |
| 119 | +**Option 1: Environment Variable Override (Recommended)** |
| 120 | + |
| 121 | +Set `SQLPASSWORD` in your environment before opening the devcontainer: |
| 122 | +- **GitHub Codespaces**: Add `SQLPASSWORD` as a [Codespaces Secret](https://docs.github.com/en/codespaces/managing-your-codespaces/managing-encrypted-secrets-for-your-codespaces) |
| 123 | +- **VS Code**: Set `SQLPASSWORD` in your shell before opening VS Code |
| 124 | + |
| 125 | +**Option 2: Edit Configuration Files** |
| 126 | + |
| 127 | +1. Update both `SA_PASSWORD` and `MSSQL_SA_PASSWORD` defaults in `docker-compose.yml` (these must match) |
| 128 | +2. Update `SQLPASSWORD` default in `devcontainer.json` (remoteEnv section) |
| 129 | +3. Update the password in the `mssql.connections` settings in `devcontainer.json` |
| 130 | + |
| 131 | +### Using a Different SQL Server Version |
| 132 | + |
| 133 | +Edit `docker-compose.yml` and change the image tag: |
| 134 | + |
| 135 | +```yaml |
| 136 | +db: |
| 137 | + image: mcr.microsoft.com/mssql/server:2022-latest # or 2019-latest |
| 138 | +``` |
| 139 | +
|
| 140 | +> **Note:** SQL Server 2025 is the default as it includes the latest features like JSON type support, vector search, and AI capabilities that this driver supports. |
| 141 | +
|
| 142 | +## Troubleshooting |
| 143 | +
|
| 144 | +### ARM64 (Apple Silicon) Users |
| 145 | +
|
| 146 | +SQL Server does not have native ARM64 container images. **Use GitHub Codespaces** instead - it runs on x86_64 infrastructure where SQL Server works natively. |
| 147 | +
|
| 148 | +### SQL Server not starting |
| 149 | +
|
| 150 | +Check the Docker logs: |
| 151 | +```bash |
| 152 | +docker logs $(docker ps -qf "name=db") |
| 153 | +``` |
| 154 | + |
| 155 | +Common issues: |
| 156 | +- Insufficient memory (SQL Server requires at least 2GB RAM) |
| 157 | +- Port 1433 already in use |
| 158 | +- ARM64 architecture issues (see above) |
| 159 | + |
| 160 | +### Connection refused |
| 161 | + |
| 162 | +Wait a few seconds after the container starts. SQL Server takes ~30 seconds to become ready. The health check should handle this automatically. |
| 163 | + |
| 164 | +### Tests failing with "no database connection string" |
| 165 | + |
| 166 | +Ensure the environment variables are set: |
| 167 | +```bash |
| 168 | +echo $SQLSERVER_DSN |
| 169 | +``` |
| 170 | + |
| 171 | +If empty, try restarting the terminal or running: |
| 172 | +```bash |
| 173 | +source ~/.bashrc |
| 174 | +``` |
| 175 | + |
| 176 | +## Files Reference |
| 177 | + |
| 178 | +| File | Purpose | |
| 179 | +|------|---------| |
| 180 | +| `devcontainer.json` | Main configuration file | |
| 181 | +| `docker-compose.yml` | Container orchestration (Go + SQL Server) | |
| 182 | +| `Dockerfile` | Go development container image | |
| 183 | +| `post-create.sh` | Setup script (runs after container creation) | |
| 184 | +| `mssql/setup.sql` | Initial database setup script | |
| 185 | + |
| 186 | +## Contributing |
| 187 | + |
| 188 | +When modifying the devcontainer: |
| 189 | + |
| 190 | +1. Test locally with `Dev Containers: Rebuild Container` |
| 191 | +2. Ensure all tests pass: `go test ./...` |
| 192 | +3. Verify SQL connection works: `test-db` |
0 commit comments