|
1 | 1 | # go-sqlcmd Development Container |
2 | 2 |
|
3 | | -This folder contains the configuration for a VS Code Dev Container / GitHub Codespaces development environment for go-sqlcmd. |
4 | | - |
5 | | -## What's Included |
6 | | - |
7 | | -- **Go 1.24** development environment with all necessary tools |
8 | | -- **SQL Server 2025** (Developer Edition) running in a sidecar container |
9 | | -- **Pre-configured VS Code extensions**: |
10 | | - - Go (official extension) |
11 | | - - MS SQL (for database management) |
12 | | - - Docker |
13 | | - - GitHub Copilot |
14 | | - - GitLens |
15 | | -- **Go quality tools**: golangci-lint, gopls, delve debugger, staticcheck |
16 | | -- **Locally built sqlcmd** added to PATH automatically |
| 3 | +Dev Container / Codespaces environment with Go 1.24 and SQL Server 2025. |
17 | 4 |
|
18 | 5 | ## Quick Start |
19 | 6 |
|
20 | | -### Using VS Code (Recommended) |
21 | | - |
22 | | -1. Install the [Dev Containers extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers) |
23 | | -2. Open this repository in VS Code |
24 | | -3. When prompted, click **"Reopen in Container"**, or: |
25 | | - - Press `F1` and select **"Dev Containers: Reopen in Container"** |
26 | | -4. Wait for the container to build (first time takes ~5 minutes) |
27 | | -5. Start developing! |
28 | | - |
29 | | -### Using GitHub Codespaces |
30 | | - |
31 | | -1. Click the green **"Code"** button on the repository |
32 | | -2. Select **"Codespaces"** tab |
33 | | -3. Click **"Create codespace on main"** (or your preferred branch) |
34 | | -4. Wait for the environment to start |
35 | | - |
36 | | -## Running Tests |
37 | | - |
38 | | -Environment variables are pre-configured for running tests: |
39 | | - |
40 | | -```bash |
41 | | -# Run all tests |
42 | | -go test ./... |
| 7 | +**VS Code**: Open repo → click "Reopen in Container" when prompted. |
43 | 8 |
|
44 | | -# Run short tests |
45 | | -go test -short ./... |
| 9 | +**Codespaces**: Click "Code" → "Codespaces" → "Create codespace". |
46 | 10 |
|
47 | | -# Run tests with verbose output |
48 | | -go test -v ./... |
49 | | -``` |
| 11 | +First build takes ~5 minutes. |
50 | 12 |
|
51 | | -### Helpful Aliases |
| 13 | +## Commands |
52 | 14 |
|
53 | | -After the container starts, these aliases are available: |
54 | | - |
55 | | -| Alias | Command | |
56 | | -|-------|---------| |
57 | | -| `gtest` | Run all tests | |
58 | | -| `gtest-short` | Run short tests | |
59 | | -| `gtest-v` | Run tests with verbose output | |
60 | | -| `gbuild` | Build sqlcmd locally | |
| 15 | +| Alias | What it does | |
| 16 | +|-------|--------------| |
| 17 | +| `gtest` | Run tests | |
61 | 18 | | `ginstall` | Build and install sqlcmd to ~/bin | |
62 | | -| `gfmt` | Format code | |
63 | | -| `gvet` | Run go vet | |
64 | 19 | | `glint` | Run golangci-lint | |
65 | | -| `ggen` | Run go generate (for translations) | |
66 | | -| `test-db` | Test database connection | |
67 | 20 | | `sql` | Connect to SQL Server (go-sqlcmd) | |
68 | | -| `sql-legacy` | Connect using legacy ODBC sqlcmd | |
69 | | -| `rebuild` | Rebuild sqlcmd | |
| 21 | +| `sql-legacy` | Connect with legacy ODBC sqlcmd | |
| 22 | +| `test-db` | Test database connection | |
70 | 23 |
|
71 | 24 | ## SQL Server Connection |
72 | 25 |
|
73 | | -The SQL Server instance is accessible at: |
74 | | - |
75 | 26 | - **Server**: `localhost,1433` |
76 | | -- **Username**: `sa` |
77 | | -- **Password**: Available via `$SQLCMDPASSWORD` environment variable |
78 | | -- **Database**: `master` (default) or `SqlCmdTest` (created for testing) |
79 | | - |
80 | | -> **Note**: The SQL Server password for this devcontainer is provided via the `$SQLCMDPASSWORD` environment variable and is a non-production, development-only default. |
81 | | -> For the default devcontainer setup, the password value (`SqlCmd@2025!`) is checked into this repository for convenience; override it via environment variables or Codespaces/CI secrets for non-local use. |
82 | | -> When using VS Code's MSSQL extension, copy the value from `$SQLCMDPASSWORD` when prompted. |
83 | | -
|
84 | | -### Using the Built-in sqlcmd |
85 | | - |
86 | | -The container has **two versions** of sqlcmd available: |
87 | | - |
88 | | -1. **go-sqlcmd** (this project) - the modern Go-based sqlcmd, built from source at `~/bin/sqlcmd` |
89 | | -2. **Legacy ODBC sqlcmd** - the classic version from mssql-tools18 at `/opt/mssql-tools18/bin/sqlcmd` |
90 | | - |
91 | | -Since go-sqlcmd is first in PATH, the `sqlcmd` command runs the modern version. Use the aliases or full paths to choose which version: |
92 | | - |
93 | | -```bash |
94 | | -# go-sqlcmd (this project) - default in PATH |
95 | | -sql # alias for go-sqlcmd with connection args |
96 | | -sqlcmd -S localhost -U sa -P "$SQLCMDPASSWORD" -C |
97 | | - |
98 | | -# Legacy ODBC sqlcmd (for compatibility testing) |
99 | | -sql-legacy # alias for legacy sqlcmd with connection args |
100 | | -/opt/mssql-tools18/bin/sqlcmd -S localhost -U sa -P "$SQLCMDPASSWORD" -C |
101 | | - |
102 | | -# Run a query with go-sqlcmd |
103 | | -sql -Q "SELECT @@VERSION" |
104 | | - |
105 | | -# Compare behavior between versions |
106 | | -sql -Q "SELECT 1 AS test" |
107 | | -sql-legacy -Q "SELECT 1 AS test" |
108 | | -``` |
109 | | - |
110 | | -### VS Code SQL Extension |
111 | | - |
112 | | -The MSSQL extension is pre-configured with a connection profile named **"sqlcmd-container (use SQLCMDPASSWORD env var)"**. Click the SQL Server icon in the Activity Bar to connect. |
113 | | - |
114 | | -### Connecting from Host Machine Tools |
115 | | - |
116 | | -The SQL Server port (1433) is forwarded to your host machine, so you can connect using tools installed locally: |
117 | | - |
118 | | -#### Azure Data Studio |
119 | | - |
120 | | -1. Open Azure Data Studio on your host machine |
121 | | -2. Create a new connection: |
122 | | - - Server: `localhost,1433` |
123 | | - - Authentication: SQL Login |
124 | | - - User: `sa` |
125 | | - - Password: `SqlCmd@2025!` |
126 | | - - Trust server certificate: Yes |
127 | | - |
128 | | -#### SQL Server Management Studio (Windows) |
129 | | - |
130 | | -1. Open SSMS on your Windows host |
131 | | -2. Connect to Server: |
132 | | - - Server name: `localhost,1433` |
133 | | - - Authentication: SQL Server Authentication |
134 | | - - Login: `sa` |
135 | | - - Password: `SqlCmd@2025!` |
136 | | - - Check "Trust server certificate" |
137 | | - |
138 | | -## Environment Variables |
139 | | - |
140 | | -The following environment variables are set automatically: |
141 | | - |
142 | | -| Variable | Value | |
143 | | -|----------|-------| |
144 | | -| `SQLCMDSERVER` | `localhost` | |
145 | | -| `SQLCMDUSER` | `sa` | |
146 | | -| `SQLCMDPASSWORD` | `SqlCmd@2025!` | |
147 | | -| `SQLCMDDATABASE` | `master` | |
148 | | -| `SQLCMDDBNAME` | `master` | |
| 27 | +- **User**: `sa` |
| 28 | +- **Password**: `$SQLCMDPASSWORD` env var (`SqlCmd@2025!` for local dev) |
| 29 | +- **Database**: `master` or `SqlCmdTest` |
149 | 30 |
|
150 | | -These use the same environment variable names as the CI pipeline to ensure local tests behave similarly, although the actual password and SQL Server version in CI may differ. |
| 31 | +Port 1433 is forwarded — connect from host tools (ADS, SSMS) using same credentials. |
151 | 32 |
|
152 | | -## Working with sqlcmd |
| 33 | +## Two sqlcmd Versions |
153 | 34 |
|
154 | | -### Build and Test Workflow |
155 | | - |
156 | | -```bash |
157 | | -# Build sqlcmd from source |
158 | | -ginstall |
159 | | - |
160 | | -# Test the build |
161 | | -~/bin/sqlcmd --version |
162 | | - |
163 | | -# Run a query against the local SQL Server |
164 | | -sql -Q "SELECT name FROM sys.databases" |
165 | | - |
166 | | -# Run the test suite |
167 | | -gtest |
168 | | -``` |
169 | | - |
170 | | -### Rebuilding After Changes |
171 | | - |
172 | | -```bash |
173 | | -# Quick rebuild |
174 | | -rebuild |
175 | | - |
176 | | -# Or full rebuild with install |
177 | | -ginstall |
178 | | -``` |
| 35 | +- **go-sqlcmd**: `~/bin/sqlcmd` (default in PATH, use `sql` alias) |
| 36 | +- **Legacy ODBC**: `/opt/mssql-tools18/bin/sqlcmd` (use `sql-legacy` alias) |
179 | 37 |
|
180 | 38 | ## Customization |
181 | 39 |
|
182 | | -### Adding SQL Setup Scripts |
183 | | - |
184 | | -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. |
| 40 | +**Change SQL version**: Edit `docker-compose.yml` image tag. |
185 | 41 |
|
186 | | -### Modifying the SA Password |
| 42 | +**Add setup scripts**: Edit `.devcontainer/mssql/setup.sql`. |
187 | 43 |
|
188 | | -To change the SQL Server password: |
189 | | - |
190 | | -1. Update both `SA_PASSWORD` and `MSSQL_SA_PASSWORD` in `docker-compose.yml` (these must match) |
191 | | -2. Update `SQLCMDPASSWORD` in `devcontainer.json` (both `remoteEnv` and `go.testEnvVars` sections) |
192 | | -3. Update the password in the `mssql.connections` settings in `devcontainer.json` |
193 | | - |
194 | | -### Using a Different SQL Server Version |
195 | | - |
196 | | -Edit `docker-compose.yml` and change the image tag: |
197 | | - |
198 | | -```yaml |
199 | | -db: |
200 | | - image: mcr.microsoft.com/mssql/server:2022-latest # or 2019-latest |
201 | | -``` |
202 | | -
|
203 | | -> **Note:** SQL Server 2025 is the default as it includes the latest features. |
| 44 | +**Change password**: Update `docker-compose.yml` and `devcontainer.json`. |
204 | 45 |
|
205 | 46 | ## Troubleshooting |
206 | 47 |
|
207 | | -### ARM64 (Apple Silicon) Users |
208 | | -
|
209 | | -SQL Server container images may have issues on ARM64 architecture. If you encounter problems: |
210 | | -
|
211 | | -1. Edit `docker-compose.yml` to use SQL Server 2022: |
212 | | - ```yaml |
213 | | - db: |
214 | | - image: mcr.microsoft.com/mssql/server:2022-latest |
215 | | - ``` |
216 | | -2. Ensure Rosetta is enabled in Docker Desktop: **Settings > General > "Use Rosetta for x86_64/amd64 emulation on Apple Silicon"** |
217 | | - |
218 | | -### SQL Server not starting |
219 | | - |
220 | | -Check the Docker logs: |
221 | | -```bash |
222 | | -docker logs $(docker ps -qf "name=db") |
223 | | -``` |
224 | | - |
225 | | -Common issues: |
226 | | -- Insufficient memory (SQL Server requires at least 2GB RAM) |
227 | | -- Port 1433 already in use |
228 | | -- ARM64 architecture issues (see above) |
229 | | - |
230 | | -### Connection refused |
231 | | - |
232 | | -Wait a few seconds after the container starts. SQL Server takes ~30 seconds to become ready. The health check should handle this automatically. |
233 | | - |
234 | | -### Tests failing with connection errors |
235 | | - |
236 | | -Ensure the environment variables are set: |
237 | | -```bash |
238 | | -echo $SQLCMDSERVER |
239 | | -echo $SQLCMDPASSWORD |
240 | | -``` |
241 | | - |
242 | | -If empty, try restarting the terminal or running: |
243 | | -```bash |
244 | | -source ~/.bashrc |
245 | | -``` |
246 | | - |
247 | | -### sqlcmd not found |
248 | | - |
249 | | -Run the install command: |
250 | | -```bash |
251 | | -ginstall |
252 | | -``` |
253 | | - |
254 | | -Or manually: |
255 | | -```bash |
256 | | -go build -o ~/bin/sqlcmd ./cmd/modern |
257 | | -``` |
258 | | - |
259 | | -## Files Reference |
260 | | - |
261 | | -| File | Purpose | |
262 | | -|------|---------| |
263 | | -| `devcontainer.json` | Main configuration file | |
264 | | -| `docker-compose.yml` | Container orchestration (Go + SQL Server) | |
265 | | -| `Dockerfile` | Go development container image | |
266 | | -| `post-create.sh` | Setup script (runs after container creation) | |
267 | | -| `mssql/setup.sql` | Initial database setup script | |
268 | | - |
269 | | -## Contributing |
270 | | - |
271 | | -When modifying the devcontainer: |
272 | | - |
273 | | -1. Test locally with `Dev Containers: Rebuild Container` |
274 | | -2. Ensure all tests pass: `go test ./...` |
275 | | -3. Verify SQL connection works: `test-db` |
276 | | -4. Verify sqlcmd builds: `ginstall && ~/bin/sqlcmd --version` |
| 48 | +- **ARM64 (Apple Silicon)**: Use SQL Server 2022 image, enable Rosetta in Docker Desktop |
| 49 | +- **SQL Server not starting**: Check `docker logs $(docker ps -qf "name=db")`. Needs 2GB+ RAM |
| 50 | +- **Connection refused**: Wait ~30s for SQL Server to start |
| 51 | +- **sqlcmd not found**: Run `ginstall` |
0 commit comments