GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Merge pull request #1: run GO scripts like sqlcmd -b; reject unknown … · jwcraig/sql-server-cli@7239933

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • .github/workflows
    • release.yml
  • Cargo.lock
  • Cargo.toml
  • README.md
  • assets
    • GEMINI.md
    • SKILL.md.template
  • docs/plans
    • agent-first-db-ops-plan.md
  • src
    • cli
      • args.rs
      • mod.rs
    • commands
      • admin.rs
      • common.rs
      • compare.rs
      • compare_counts.rs
      • health.rs
      • init.rs
      • mod.rs
      • object_lookup.rs
      • sql.rs
      • sql_fragments.rs
      • sql_utils.rs
      • table_data.rs
    • config
      • env.rs
      • loader.rs
      • schema.rs
    • db
      • executor.rs
    • main.rs
    • output
      • json.rs
      • mod.rs
      • raw.rs
      • table.rs
  • tests
    • batch_semantics_test.rs
    • config_command_test.rs
    • help_test.rs
    • ops_commands_test.rs
    • raw_output_test.rs
    • sql_command_test.rs
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎.github/workflows/release.yml‎

Copy file name to clipboardExpand all lines: .github/workflows/release.yml
+19-1Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,27 @@ jobs:
3737
exit 1
3838
fi
3939
40+
# Gates the release on the same checks as ci.yml. Without this the release
41+
# only validates the version string, so a tag can publish over failing tests:
42+
# v0.1.14 shipped while windows-latest had been red for a month.
43+
test:
44+
name: Test ${{ matrix.os }}
45+
needs: validate-release-version
46+
runs-on: ${{ matrix.os }}
47+
strategy:
48+
fail-fast: false
49+
matrix:
50+
os: [ubuntu-latest, macos-latest, windows-latest]
51+
steps:
52+
- uses: actions/checkout@v4
53+
- uses: dtolnay/rust-toolchain@stable
54+
- run: cargo test --all-features
55+
- run: cargo clippy -- -D warnings
56+
- run: cargo fmt --check
57+
4058
build:
4159
name: Build ${{ matrix.target }}
42-
needs: validate-release-version
60+
needs: test
4361
runs-on: ${{ matrix.os }}
4462
strategy:
4563
fail-fast: false
Collapse file

‎Cargo.lock‎

Copy file name to clipboardExpand all lines: Cargo.lock
+1-1Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Collapse file

‎Cargo.toml‎

Copy file name to clipboardExpand all lines: Cargo.toml
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "sscli"
3-
version = "0.1.13"
3+
version = "0.1.16"
44
edition = "2024"
55
rust-version = "1.85"
66
description = "Agent-friendly token efficient SQL Server CLI tool for database inspection"
Collapse file

‎README.md‎

Copy file name to clipboardExpand all lines: README.md
+122-1Lines changed: 122 additions & 1 deletion
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ sscli sql "SELECT TOP 5 * FROM Users"
122122
sscli "SELECT COUNT(*) FROM Users" # Top-level shorthand for inline SQL
123123
sscli sql --file [path/to/file] # Run long queries, execute bulk statements
124124
cat patch.sql | sscli sql --stdin # Pipe a script on stdin
125+
sscli --format tsv --no-headers "SELECT COUNT_BIG(*) FROM Users"
126+
sscli --trust-cert true --quiet-tls-warning "SELECT 1"
127+
sscli --password-env SQL_PASSWORD status # Avoid putting passwords on the command line
125128
sscli update # Check for new releases (alias: sscli upgrade)
126129
```
127130

@@ -271,6 +274,12 @@ cp config.example.yaml .sql-server/config.yaml
271274

272275
Run `sscli config` to confirm which config file is being used and what values are in effect.
273276

277+
A profile named by `--profile`, `SQL_SERVER_PROFILE` or `defaultProfile` must exist:
278+
an unknown name is an error that lists the profiles the config defines, rather
279+
than a silent fall back to `localhost`. When no config file, `--server` or
280+
`SQL_SERVER`/`SSCLI_URL` names a server, commands warn on stderr that they are
281+
using the built-in `localhost:1433/master` default.
282+
274283
### Example `config.yaml`
275284

276285
```yaml
@@ -300,7 +309,7 @@ load `.env` from the current working directory.
300309
| ------------------------ | --------------------------------------------------------------------------------------------------------- |
301310
| Config path | `SQL_SERVER_CONFIG`, `SQLSERVER_CONFIG` |
302311
| Profile | `SQL_SERVER_PROFILE`, `SQLSERVER_PROFILE` |
303-
| Connection URL | `DATABASE_URL`, `DB_URL`, `SQLSERVER_URL` |
312+
| Connection URL | `SSCLI_URL`, `DATABASE_URL`, `DB_URL`, `SQLSERVER_URL` |
304313
| Server | `SQL_SERVER`, `SQLSERVER_HOST`, `DB_HOST`, `MSSQL_HOST` |
305314
| Port | `SQL_PORT`, `SQLSERVER_PORT`, `DB_PORT`, `MSSQL_PORT` |
306315
| Database | `SQL_DATABASE`, `SQLSERVER_DB`, `DATABASE`, `DB_NAME`, `MSSQL_DATABASE` |
@@ -319,6 +328,9 @@ load `.env` from the current working directory.
319328
| Password | `SQLCMDPASSWORD` |
320329
| Database | `SQLCMDDBNAME` |
321330

331+
Connection URL query options include `trustServerCertificate=true|false`,
332+
`encrypt=true|false`, and `timeoutMs=<milliseconds>`.
333+
322334
## Commands
323335

324336
**Core** (shown in `--help`):
@@ -344,6 +356,9 @@ load `.env` from the current working directory.
344356
| `query-stats` | Top cached queries by resource usage |
345357
| `backups` | Recent backup history |
346358
| `compare` | Schema drift detection between two connections |
359+
| `compare-counts` | Row-count validation across databases |
360+
| `health` | Constraint, trigger, and identity summaries |
361+
| `admin` | SQL Server maintenance SQL, backup, and DBCC (see [admin](#admin-maintenance-sql)) |
347362
| `integrations` | Install agent skills/extensions |
348363

349364
Note: `sscli sessions` filters by client host name using `--client-host`. `--host` is reserved as an alias for `--server`.
@@ -356,8 +371,19 @@ Note: `sscli sessions` filters by client host name using `--client-host`. `--hos
356371
| Piped / non-TTY | Markdown tables |
357372
| `--json` flag | Stable JSON (v1 contract) |
358373
| `--csv <file>` | CSV export |
374+
| `--format tsv` | Raw TSV rows |
375+
| `--format csv` | Raw CSV rows |
376+
| `--format jsonl` | One JSON object per row |
359377

360378
JSON output emits exactly one object to stdout. Errors go to stderr.
379+
`sql` and `admin sql` print an `Execution` summary table when a script returns no
380+
result sets, so a statement that succeeded silently is still visible. See
381+
[admin](#admin-maintenance-sql).
382+
Raw row formats keep stdout data-only; target banners, progress, warnings, and
383+
errors go to stderr. Use `--no-headers` for headerless TSV/CSV evidence files
384+
and `--output <path|->` to write raw row output.
385+
Use `--quiet-tls-warning` when `--trust-cert true` is intentional and verbose
386+
logging would otherwise print Tiberius' certificate-validation warning.
361387

362388
## Safety
363389

@@ -384,13 +410,108 @@ Each command returns a stable top-level object:
384410
| `table-data` | `{ table, columns, rows, total, offset, limit, hasMore, nextOffset }` |
385411
| `sql` | `{ success, batches, resultSets, csvPaths? }` |
386412
| `compare` | `{ modules, indexes, constraints, tables }` when `--summary`; `{ source, target }` snapshots with full metadata when `--json` without `--summary` |
413+
| `compare-counts` | `{ sourceDb, targetDb, counts }` |
414+
| `health` | `{ kind, checks }` |
415+
| `admin sql` | `{ status, error, partialEffectPossible, elapsedMs, batchCount, batchesSucceeded, batches, resultSetCount, rowsReturned, resultSets, verify }` |
416+
417+
`config --json` never emits plaintext passwords by default. It reports
418+
`passwordSet`, `passwordSource`, and `passwordEnv` metadata instead.
387419

388420
Errors (stderr):
389421

390422
```json
391423
{ "error": { "message": "...", "kind": "Config|Connection|Query|Internal" } }
392424
```
393425

426+
## Scripts and `GO` batches
427+
428+
`sql --file`, `sql --stdin` and `admin sql` run scripts the way `sqlcmd -b` does:
429+
430+
- A line holding only `GO` (any case, optionally `GO n` to repeat the batch, or a
431+
trailing `--` comment) ends a batch. `GO` inside a string, bracketed
432+
identifier or comment is left alone.
433+
- Every batch runs in order on one session. A transaction opened in one batch
434+
can be committed in a later one, and `SET` options, `USE` and `#temp` tables
435+
carry over, as they do in `sqlcmd`.
436+
- The first failing batch stops the script with a non-zero exit, and the error
437+
names the batch (`Batch 3 of 5 failed: ...`). The session then closes, so the
438+
server rolls back a transaction the script left open. `sql --continue-on-error`
439+
keeps going instead.
440+
441+
A batch that uses `--param` values runs through `sp_executesql`, so a
442+
transaction cannot stay open across that batch. Batches that use no parameter
443+
are unaffected.
444+
445+
`sql` runs any statement, writes included. The `sql` banner on stderr shows the
446+
target, profile and config file (`Target: host:port/db (profile dev,
447+
/path/.sql-server/config.yaml)`), so check it before you run a write script.
448+
449+
## admin (maintenance SQL)
450+
451+
`admin sql` runs full-capability maintenance SQL. `GO` splits the script into
452+
batches, which run in order on one connection and stop at the first failure.
453+
454+
### Execution summary
455+
456+
Maintenance statements usually return no rows, so `admin sql` and `sql` always
457+
print an `Execution` summary. A successful `DROP DATABASE` is therefore visibly
458+
different from a command that never ran:
459+
460+
```
461+
$ sscli admin sql "DROP DATABASE IF EXISTS [WDM_VERIFY];"
462+
Target: localhost:1433/master
463+
| Execution | Value |
464+
|--------------|------------------|
465+
| Status | ok |
466+
| Batches | 1 of 1 succeeded |
467+
| ResultSets | 0 |
468+
| RowsReturned | 0 |
469+
| ElapsedMs | 11 |
470+
```
471+
472+
Multi-batch scripts also print a per-batch table, so a run that stops halfway
473+
shows which batches already applied. That matters when an earlier batch leaves
474+
state behind, such as `ALTER DATABASE ... SET SINGLE_USER` followed by a
475+
`DROP DATABASE` that fails. Failures exit non-zero with the server message on
476+
stderr.
477+
478+
A batch is not all-or-nothing. It holds every statement up to the next `GO`, and
479+
semicolon-separated statements before the failing one have already committed. So
480+
`Batches 0 of 1 succeeded` means the batch did not complete, not that nothing
481+
happened, and the summary says so:
482+
483+
```
484+
| Status | failed |
485+
| Batches | 0 of 1 succeeded |
486+
| FailedBatch | 1 (statements before the failure may have taken effect) |
487+
```
488+
489+
After any failed batch, read the current state back rather than assuming a
490+
rollback. In JSON, `partialEffectPossible` carries the same warning.
491+
492+
### Verified maintenance
493+
494+
`Status ok` proves the statement executed, not that it changed anything:
495+
`DROP DATABASE IF EXISTS` reports success even as a no-op. Use `--verify` to run
496+
a read-back on the same connection and `--expect-rows` to assert its row count.
497+
A mismatch prints the summary, then exits non-zero.
498+
499+
```bash
500+
# Drop a database and prove it is gone
501+
sscli admin sql "DROP DATABASE [WDM_VERIFY];" \
502+
--verify "SELECT name FROM sys.databases WHERE name = 'WDM_VERIFY'" \
503+
--expect-rows 0
504+
505+
# Prove a database was created
506+
sscli admin sql "CREATE DATABASE [WDM_VERIFY];" \
507+
--verify "SELECT name FROM sys.databases WHERE name = 'WDM_VERIFY'" \
508+
--expect-rows 1
509+
```
510+
511+
`--verify` without `--expect-rows` reports the read-back rows without asserting
512+
them. The read-back is skipped when a batch fails, because it would describe a
513+
half-applied script.
514+
394515
## compare (schema drift)
395516

396517
Detects drift between two profiles or explicit connection strings.
Collapse file

‎assets/GEMINI.md‎

Copy file name to clipboardExpand all lines: assets/GEMINI.md
+10-1Lines changed: 10 additions & 1 deletion
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Use this extension when you need to connect to a SQL Server host/database to:
55
- Confirm connectivity / basic server status
66
- Browse schemas/tables/columns/relationships
77
- Describe a table (columns + types + indexes + constraints + triggers)
8-
- Run safe ad-hoc queries (read-only by default)
8+
- Run ad-hoc SQL and capture raw row evidence
99

1010
## Commands
1111

@@ -20,4 +20,13 @@ These commands run the local `sscli` CLI and inject output into the prompt.
2020

2121
- The underlying CLI is expected to be on `PATH` as `sscli`.
2222
- Prefer `--json` when you need structured data.
23+
- Prefer `--format tsv --no-headers --raw` for shell comparisons and evidence files.
24+
- Prefer `--password-env NAME` or `SSCLI_URL` over `--password value`.
25+
- Use `--quiet-tls-warning` when `--trust-cert true` is intentional and verbose logs would otherwise add TLS noise.
26+
- Use `sscli admin ... --dry-run` for SQL Server maintenance planning before execution.
27+
- `sscli admin sql` and `sscli sql` always print an `Execution` summary, so a statement with no result sets is still visibly distinct from one that never ran.
28+
- A summary of `Status ok` proves execution, not effect. For destructive SQL add `--verify "<read-back query>" --expect-rows N`; a mismatch exits non-zero.
29+
- A failed batch is not a rollback: statements before the failing one in that batch have committed. Read the state back instead of reporting "nothing happened".
30+
- Multi-batch `GO` scripts (`sscli sql --file`) run like `sqlcmd -b`: one session, transactions and `SET` options carry across batches, and the first failing batch stops the script and rolls back any open transaction.
31+
- Name the target with `--profile <name>`; an unknown profile is an error, and with no config sscli warns that it is using `localhost:1433/master`.
2332
- Output is truncated by default (cells >140 chars, total >25KB). Use `--no-truncate` for full output.
Collapse file

‎assets/SKILL.md.template‎

Copy file name to clipboardExpand all lines: assets/SKILL.md.template
+71-3Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
name: {name}
3-
description: When you need to connect to a SQL Server host/database to inspect schemas, check status, sample table data, or run ad-hoc queries, use the `sscli` CLI. Prefer markdown output for token efficiency (default) and `--json` for machine-readable results.
3+
description: When you need to connect to a SQL Server host/database to inspect schemas, check status, sample table data, run ad-hoc queries, or capture SQL Server evidence files, use the `sscli` CLI. Prefer markdown output for token-efficient reading, `--json` for structured command results, and `--format tsv|csv|jsonl` for raw row evidence.
44
---
55

66
# SQL Server CLI (agent workflow)
@@ -15,21 +15,89 @@ sscli tables
1515
sscli describe <Object> #automatic detection of Object type. Returns descriptions for all matching Objects (tables, view, triggers, procs)
1616
sscli sql "SELECT TOP 10 * FROM <TableName/ViewName>"
1717
sscli "SELECT COUNT(*) FROM <TableName/ViewName>"
18+
sscli --format tsv --no-headers "SELECT COUNT_BIG(*) FROM <TableName/ViewName>"
1819
```
1920

2021
## Output guidance
2122

2223
- Default output is markdown (token-friendly).
2324
- For machine-readable results, add `--json`.
25+
- For shell-diffable row evidence, use `--format tsv --no-headers --raw`.
26+
- For row streams consumed by scripts, use `--format jsonl`.
27+
- Raw row formats keep stdout data-only; diagnostics stay on stderr.
28+
- `sql --json` returns `{ success, batches, resultSets: [{ columns, rows }] }`;
29+
rows are arrays in column order.
2430
- Output is truncated by default (cells >140 chars, total >25KB). Use `--no-truncate` for full output when you need complete data.
2531

32+
## Connection and secrets
33+
34+
- Prefer `--password-env NAME` over `--password value`.
35+
- Use `--quiet-tls-warning` when `--trust-cert true` is intentional and evidence output must stay clean.
36+
- For ephemeral restore containers, prefer `SSCLI_URL`:
37+
38+
```bash
39+
export SSCLI_URL='sqlserver://sa:[email protected]:15433/WDM?trustServerCertificate=true'
40+
sscli status
41+
```
42+
43+
- `config --json` reports whether a password is set, but does not print the secret.
44+
2645
## Safety
2746

28-
- `sscli sql` is the canonical raw-SQL surface.
47+
- `sscli sql` is the canonical raw-SQL surface. It runs any statement,
48+
writes included, so check the `Target:` banner (host, database, profile and
49+
config file) before a write.
2950
- For file-backed or batch-oriented execution, prefer `sscli sql --file ...` or
30-
`sscli sql --stdin`.
51+
`sscli sql --stdin`. Multi-batch `GO` scripts run like `sqlcmd -b`: one
52+
session, transactions and `SET` options carry across batches, and the first
53+
failing batch stops the script (`Batch N of M failed: ...`) and rolls back
54+
any open transaction. No need to fall back to `sqlcmd`.
55+
- Name the target explicitly with `--profile <name>`. An unknown profile is an
56+
error; with no config at all, sscli warns that it is using
57+
`localhost:1433/master`.
3158
- Prefer explicit `--env-file` when loading connection settings from a file.
3259

60+
For planned maintenance, prefer explicit admin commands over dropping to `sqlcmd`:
61+
62+
```bash
63+
sscli admin checkdb --database WDM --physical-only --docker-volume-safe --dry-run
64+
sscli admin backup verify --from /var/opt/mssql/backup/WDM.bak --checksum --dry-run
65+
sscli compare-counts --source-db WDM_A --target-db WDM_B --tables dbo.TableA,dbo.TableB
66+
```
67+
68+
## Proving that maintenance SQL took effect
69+
70+
`sscli admin sql` and `sscli sql` always print an `Execution` summary
71+
(`Status`, `Batches`, `ResultSets`, `RowsReturned`, `ElapsedMs`), so a run that
72+
produced no result sets is still visibly distinct from a command that never ran.
73+
74+
The summary proves the statement executed, not that it changed anything: DDL
75+
such as `DROP DATABASE IF EXISTS` reports `Status ok` even when it was a no-op.
76+
For destructive or state-changing work, attach a read-back with `--verify` and
77+
assert it with `--expect-rows`. The read-back runs on the same connection and a
78+
mismatch exits non-zero.
79+
80+
```bash
81+
# Drop a database and prove it is gone in one command
82+
sscli admin sql "DROP DATABASE [WDM_VERIFY];" \
83+
--verify "SELECT name FROM sys.databases WHERE name = 'WDM_VERIFY'" \
84+
--expect-rows 0
85+
86+
# Prove a database was created
87+
sscli admin sql "CREATE DATABASE [WDM_VERIFY];" \
88+
--verify "SELECT name FROM sys.databases WHERE name = 'WDM_VERIFY'" \
89+
--expect-rows 1
90+
```
91+
92+
Never report a maintenance statement as successful based on exit status alone
93+
when the statement had no observable output. Use `--verify` or run an explicit
94+
read-back query.
95+
96+
A failed batch is not a rollback. `Batches 0 of 1 succeeded` means the batch did
97+
not complete; semicolon-separated statements before the failing one have already
98+
committed. Never report a failed `admin sql` run as "nothing happened" — read the
99+
state back first. JSON callers can check `partialEffectPossible`.
100+
33101
## Compare (schema drift)
34102

35103
Use `compare` to spot differences between environments. It is read-only and safe.

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.