Skip to content

fix(teardown,sdk): MSSQL CHECK-constraint teardown + clean db.reset()#37

Merged
damusix merged 7 commits into
masterfrom
fix/teardown-mssql-check-constraint-udf
Jun 6, 2026
Merged

fix(teardown,sdk): MSSQL CHECK-constraint teardown + clean db.reset()#37
damusix merged 7 commits into
masterfrom
fix/teardown-mssql-check-constraint-udf

Conversation

@damusix
Copy link
Copy Markdown
Collaborator

@damusix damusix commented Jun 6, 2026

Summary

Two related MSSQL teardown fixes plus cross-dialect verification.

1. teardown drops MSSQL CHECK constraints before functions (#36)

A scalar UDF referenced by a CHECK constraint can't be dropped while its table exists (MSSQL error 3729). Functions are dropped before tables to satisfy schema-bound dependents, so the CHECK dependency is now severed first via a new MSSQL-only dropCheckConstraints() op (excludes the noorm schema). PostgreSQL is unaffected (dropFunction uses CASCADE); MySQL forbids UDFs in CHECK constraints; SQLite has no stored functions. A PG integration test locks in the CASCADE behavior.

2. db.reset() ignores preserveTables for a clean rebuild

db.reset() (teardown + build) honored settings.teardown.preserveTables, so preserved reference tables survived teardown and collided with the build's CREATE TABLE, aborting the rebuild. reset() now runs a full teardown that ignores preserveTables; the setting still applies to teardown() and truncate(). Verified end to end: db.reset() now fully and idempotently rebuilds the 38-table llm-memory-db-mssql example schema.

Tests

  • New PG integration regression (tests/integration/sdk/db-reset.test.ts, plus CASCADE guard in tests/integration/teardown/postgres.test.ts)
  • New MSSQL integration regression (CHECK-constraint UDF teardown)
  • Unit coverage for ordering / noorm-schema exclusion / keepFunctions + dialect gating
  • typecheck + lint clean

Changesets

@noormdev/sdk and @noormdev/cli patch entries for both fixes (noorm db teardown / noorm db reset route through the same core).

Note: this branch also carries two earlier session commits (atomic signals scaffolding + the example-llm-memory-db-mssql CI job) that were on local master but unpushed. No changesets; they don't affect the version bump.

Closes #36

damusix added 7 commits June 5, 2026 22:52
Mirrors example-todo-db: bootstrap state.enc via noorm ci, build schema,
apply changes, run SDK suite against SQL Server 2022. Uses noorm run build
(not db reset) because schema-bound validator UDFs lock referenced tables,
and config 'test' with isTest=true to match the test-context bootstrap.
Wire .claude/project/signals.md @-ref into CLAUDE.md, add generated
signals files, .signalsignore, followups, and docs/design + docs/spec
scaffolding. Gitignore scratchpad and prev-deterministic snapshot.
Two related MSSQL teardown fixes plus cross-dialect verification.

1) teardown drops MSSQL CHECK constraints before functions (#36)
   A scalar UDF referenced by a CHECK constraint can't be dropped while its
   table exists (MSSQL error 3729). Functions are dropped before tables to
   satisfy schema-bound dependents, so the CHECK dependency is severed first
   via a new MSSQL-only dropCheckConstraints() op that drops all user-schema
   CHECK constraints (excluding the noorm schema) ahead of the function drops.
   PostgreSQL is unaffected (dropFunction uses CASCADE); MySQL forbids UDFs in
   CHECK constraints; SQLite has no stored functions. A PG integration test
   locks in the CASCADE behavior so a refactor can't reintroduce the break.

2) db.reset() ignores preserveTables for a clean rebuild
   db.reset() (teardown + build) honored settings.teardown.preserveTables, so
   preserved reference tables survived teardown and collided with the build's
   CREATE TABLE, aborting the rebuild. reset() now runs a full teardown that
   ignores preserveTables; the setting still applies to teardown() and
   truncate(). Verified end to end: db.reset() now fully and idempotently
   rebuilds the 38-table llm-memory-db-mssql example schema.

Changesets added for both @noormdev/sdk and @noormdev/cli (noorm db teardown
and noorm db reset route through the same core).

Closes #36
Master CI has been red since the alpha.36 work began; the failures were
cross-file test contamination plus stale assertions, not product bugs.
Each failing test passes in isolation.

Contamination (state leaking across files in a serial in-process run):
- tests/core/identity/env.test.ts reassigned the whole process.env object
  in afterEach, detaching the reference the config module bound at import —
  poisoned every later env-config test (config: env / resolver). Restore env
  keys in place instead.
- tests/core/sql-terminal/executor.test.ts spied on kysely's shared sql.raw
  export without restoring it, so sql.raw stayed mocked process-wide and the
  sdk impersonate suite executed no SQL. Add vi.restoreAllMocks() in afterEach.

Stale assertions (identifier quoting added in 66e6fb8 / df24197; execution
tests already pass with the quoted form, only the SQL-string asserts lagged):
- tests/sdk/context.test.ts: EXEC checkout_trx -> EXEC [checkout_trx]
- tests/integration/sdk/tvf.test.ts: quote pg and mssql TVF call identifiers

Verified: all four CI groups green locally (group1 2040, group2 116,
group3 337, group4 587), typecheck + lint clean.
The two useOnEvent tests used fixed 10ms setTimeout waits for observer to
React state propagation, which is too tight on a loaded CI runner (the
subscribe test failed all three retries). Add a polling waitFor helper:
the single-event test re-emits each poll until received (tolerates a not-
yet-registered subscription); the counter test waits generously for the
subscription, emits exactly three times, then polls for the final count.
Verified stable across repeated local runs.
await import('tarn') / import('tedious') expose their exports under .default
once bundled by tsup, so spreading the namespace left tarn.Pool undefined and
kysely threw 'Pool is not a constructor' on every MSSQL connection — breaking
noorm db create/run/change in the published CLI and any bundled SDK consumer.
Normalize with 'module.default ?? module', mirroring the postgres dialect's
existing pg.default?.Pool guard.

Surfaced by the new example-llm-memory-db-mssql CI job. Verified: the bundled
CLI now connects to MSSQL and queries sys.databases instead of crashing.
The MCP discovery test hard-coded connect config 'dev', but the example
suite (createContext({config:'test'})) and the CI job (noorm ci init --name
test) only provision the 'test' config, so the MCP connect returned an error
under CI. Use 'test' to match the rest of the suite.
@damusix damusix merged commit 3ef0007 into master Jun 6, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

teardown() drops functions before tables on MSSQL — fails on scalar UDFs referenced by CHECK constraints (regression in alpha.35)

1 participant