Skip to content

feat(appkit): add appkit db introspect - #670

Closed
ditadi wants to merge 170 commits into
stack/database-dev-flow/03-db-migratefrom
stack/database-dev-flow/04-db-introspect
Closed

ditadi wants to merge 170 commits into
stack/database-dev-flow/03-db-migratefrom
stack/database-dev-flow/04-db-introspect

Conversation

@ditadi

@ditadi ditadi commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

🥞 Stacked PR (generated by Stack)

Use this link to review incremental changes.


Stacked on #666. The design is #677.

What did you change, and why?

Apps whose tables already exist in Lakebase need a schema.ts and a migration history that match them. appkit db introspect reads a Postgres schema (app by default, or --schema-name) from pg_catalog and writes config/database/schema.ts, keeping the previous file as schema.ts.bak. It also writes the migration that brings history up to the database, and records it as applied. Without history that migration is a first 0000_baseline, which the deployed app adopts on start (#654). With history it is the next NNNN_introspect. Recording is only true on the branch introspect read, so introspect runs only on your dev or session branch.

What the builder cannot express is left out with an // appkit: skipped … comment, and what introspect does not read at all is named in a warning. Introspect refuses when the branch does not record exactly the committed migrations, when its migration would drop something schema.ts left out, and when it would replace schema.ts with an empty one. If anything fails after the write, it puts back schema.ts, its backup, database.d.ts and the migrations.

This revision keeps what the replaced schema.ts declares:

  • Introspect loads the schema.ts it replaces first. A declared column keeps its .private() as it was. Every other column, table and foreign key is written as the catalog has it, foreign keys with fk().
  • A schema.ts that exists but does not load refuses the run before anything is read or written.
  • When two foreign keys point at one table, the one schema.ts declares keeps the relation.
  • Introspect holds the history lock from feat(appkit): add the appkit db commands for the database development flow #666 for its whole run.

Review round, 2026-11-09:

  • Introspect refuses a branch that recorded a migration whose file changed since; keeps one foreign key per column instead of silently overwriting the first; and reads the schema an existing schema.ts declares when --schema-name is absent.
  • Fidelity: an enum with an empty label skips the column; literal defaults cast to quoted type names keep their value; deferrable unique constraints and key sequences with non-default options are reported as approximations.
  • schema.ts, its .bak and the migration journal are written through tooling/files.ts (refuse a symlinked destination, temp file + rename).

Fix round for fjakobs's review of #677, 2026-11-15:

Review round for MarioCadenas's review of #677, 2026-11-25:

  • CHECK and EXCLUDE constraints and indexes are now warnings instead of Approximated information. schema.ts cannot declare them, so the baseline and a branch rebuilt from the migrations lack them. The warning says so: app.t: index t_idx is not in schema.ts, so a database the migrations create will not have it.

How do you know it works?

  • render.test.ts, introspect.test.ts and roundtrip.test.ts cover declared privacy, new columns and tables written without .private(), kept, dropped and undeclared foreign keys, a schema.ts that does not load, a run while migrate holds the lock, and each drift and drop refusal. The round trip loads both files through the real loader. A refresh keeps what the old file declares, and adds the new column, table and foreign key as the catalog has them.
  • catalog.pg.test.ts and apply.pg.test.ts run against Postgres. They read a real schema, and record pending migrations without running them.
  • A smoke against a local PostgreSQL 14 kept a private column private. It ran before this revision, which writes new columns and tables without .private().
  • At this PR's head, the database, plugin and CLI db tests passed against a local PostgreSQL 14, typecheck passed, and a clean build stayed within the size gate. The full vitest suite passed at an earlier revision of this PR and at the top of the stack (feat(appkit): add appkit db check #678). The packed tarballs export the same root and ./beta names as main, and ./beta/database-tooling adds only runIntrospect. CI runs the full suite on this PR.
  • This revision was not run against a real Lakebase workspace.

Review round, 2026-11-09: each fix has a failing-then-passing test in its PR, and every layer's appkit and shared tests ran against Postgres 14 after the sync. At the top of the stack, pnpm build, pnpm test (6502 passed, 1 skipped), pnpm -r typecheck, pnpm check, pnpm knip and pnpm docs:build pass.

Fix round for fjakobs's review, 2026-11-15: each fix has a failing-then-passing test in its PR, and every layer's database, plugin and CLI tests ran against Postgres 17 after the sync. A throwaway Lakebase fork confirmed that the developer role can write the service principal's rows but cannot alter its tables. At the top of the stack, pnpm test (6602 passed, 1 skipped), pnpm -r typecheck, pnpm build and pnpm docs:build pass, and oxlint and oxfmt are clean on every changed file. After the AutoCRUD removal, the introspect tests failed first and then passed, and at the top of the stack pnpm test (6601 passed, 1 skipped), pnpm -r typecheck, pnpm build and pnpm docs:build pass.

Review round, 2026-11-25: the render and introspect tests failed first and then passed, the database tests passed against Postgres 17, and at the top of the stack pnpm test (6601 passed, 1 skipped), pnpm -r typecheck, pnpm build and pnpm docs:build pass.

@ditadi
ditadi force-pushed the stack/database-dev-flow/03-db-migrate branch from 9f3a34f to 1827ebd Compare October 19, 2026 12:44
@ditadi
ditadi force-pushed the stack/database-dev-flow/04-db-introspect branch from a2522e5 to c3e1172 Compare October 19, 2026 15:02
ditadi added 10 commits October 20, 2026 12:48
A deployed DatabasePlugin app now applies the migrations committed under
config/database/migrations before its startup checks, as its own service
principal and under a per-schema advisory lock. Pending migrations run in
one transaction, and a table lock held for over 30s fails the migration
instead of stalling the table. A 0000_baseline whose tables already exist
is recorded, in its own transaction, instead of run. Any failure stops
startup, and the server log names it: the tags and SQLSTATE, or the
missing migration file. Local runs never migrate on start.

Signed-off-by: ditadi <victordperd@gmail.com>
- Run and record each pending migration in its own transaction. An enum value
  added by one migration can now be used by the next (55P04), and a failure
  keeps the migrations before it.
- Compare the committed journal with the recorded rows by hash and created_at.
  Fail closed on a migration older than the last recorded one; warn on edited
  migrations and on recorded rows with no committed file.
- Read the history without the lock first. Take the lock only when something
  is pending, then read again.
- Bound the wait for the migration lock to 5 minutes and name its holder. Set
  application_name and client_connection_check_interval. Lower lock_timeout to
  5s with three tries per migration.
- Migrate on start only with APPKIT_DB_MIGRATE_ON_START=true and service
  principal credentials. Skip dev- and session- branches; refuse a -pooler
  host.
- Name the failed step and the SQLSTATE or system code. On 42501, name the
  schema owner and the connecting role.
- Add real-Postgres tests gated on APPKIT_TEST_PG_URL; CI runs them against a
  postgres:17 service.

Signed-off-by: ditadi <victordperd@gmail.com>
defineSchema() without a schemaName now places its tables in the Postgres
schema "app" instead of "public". The deployed app's service principal can
create and own a schema of its own, but has no CREATE on public, so tables
in public could not be migrated on deploy without a manual grant.

Signed-off-by: ditadi <victordperd@gmail.com>
An app whose tables predate the app default fails setup with advice to
commit migrations, which would create an empty copy in app. Name the
public tables instead, give public its own ownership hint, document one
schema per app, and pin the dev-playground to public.

Signed-off-by: ditadi <victordperd@gmail.com>
`appkit db migrate` generates a migration from config/database/schema.ts
with drizzle-kit, regenerates shared/appkit-types/database.d.ts, and
applies pending migrations to the branch .env points at. It applies only
to a branch created for this developer (dev-…) or this checkout
(session-…); on any other branch, including the project default and any
branch databricks.yml deploys to, it generates and says why nothing was
applied. It connects only after the Lakebase API confirms that PGHOST is
a host of LAKEBASE_ENDPOINT.

The logic lives in a new @databricks/appkit/database-tooling entry point
that the shared CLI loads lazily, so the runtime never imports drizzle-kit.
drizzle-kit is an optional peer dependency: apps that generate migrations
install it as a dev dependency, and the command says how when it is
missing. `appkit db` stays hidden from --help until the database plugin
is offered by `databricks apps init`.

Signed-off-by: ditadi <victordperd@gmail.com>
A generated migration that fails to apply is removed and the journal
restored, so a fix in schema.ts replaces it instead of stacking behind it.

Also: refuse a drizzle-kit other than the pinned one or one loading another
drizzle-orm, parse the branch and endpoint responses the guard decides on,
type the CLI against a contract appkit's typecheck enforces, and enforce
the drizzle-kit and tooling import boundary with oxlint.

Signed-off-by: ditadi <victordperd@gmail.com>
…nch pool

resolveEnvBranch returns the branch kind (default, protected, deploy
target, dev, session, other), and takesWrites is the one rule for which
kinds take writes. It needs PGHOST, PGDATABASE and LAKEBASE_ENDPOINT,
like the pool. withBranchPool opens and always closes the pool. The
drizzle-kit v7 file names live only in migrations/paths.ts.

Signed-off-by: ditadi <victordperd@gmail.com>
…aks the previous release

Apps keeps instances of the previous release serving while the new one
migrates on start, and a code rollback cannot undo a migration. When the
generated migration drops, renames, retypes or makes a column NOT NULL,
the report lists those statements and the CLI says to ship code that
works with both schemas first.

Signed-off-by: ditadi <victordperd@gmail.com>
…lient

Signed-off-by: ditadi <victordperd@gmail.com>
`appkit db introspect` reads an existing Postgres schema (default: app) from
pg_catalog and writes config/database/schema.ts from it, keeping the previous
file as schema.ts.bak. It also writes the migration that brings history up to
the database, 0000_baseline first and NNNN_introspect after, and records it
as applied. That is only true on the branch it read, so it runs only on the
developer's dev or session branch, which `appkit db init` creates. The
deployed app adopts 0000_baseline on start when its tables already exist.

What the schema builder cannot express is skipped and reported, and what it
declares differently from the database (a timestamp precision, an index) is
listed. Objects it does not read (views, triggers, policies, functions,
standalone sequences) are named in a warning, since a database the
migrations create will not have them.

It refuses when the branch's journal and the checkout's migrations differ,
and it refuses a migration that drops something schema.ts left out, since
the deployed app runs that migration on start. Recording takes the
migration lock and checks the journal again, and a run interrupted before
recording is finished by the next one. If anything fails after schema.ts is
written, schema.ts, its backup, database.d.ts and the migrations are put
back.

Signed-off-by: ditadi <victordperd@gmail.com>
@ditadi
ditadi force-pushed the stack/database-dev-flow/03-db-migrate branch from df1b1c9 to 0d1dfaf Compare October 20, 2026 12:22
@ditadi
ditadi force-pushed the stack/database-dev-flow/04-db-introspect branch from c3e1172 to a45e7df Compare October 20, 2026 12:23
@github-actions

github-actions Bot commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

🤖 AppKit PR bot

🔬 Run evals

Start an eval for this PR from the evals-monitor app: Go to Evals Monitor →

📦 Try this PR's app template

Scaffolds a new app from this PR's SDK build. Run it in any folder (requires the GitHub CLI — gh auth login — and the Databricks CLI):

gh run download 45807918078 -R databricks/appkit -n appkit-template-0.105.0-pr.9097265-stack-database-dev-flow-04-db-introspect-670 -D appkit-pr-670 \
  && unzip -o "appkit-pr-670/appkit-template-0.105.0-pr.9097265-stack-database-dev-flow-04-db-introspect-670.zip" -d "appkit-pr-670" \
  && databricks apps init --template "appkit-pr-670"

The template pins @databricks/appkit and @databricks/appkit-ui to tarballs built from this branch, so the scaffolded app runs against this PR's code.

@github-actions

github-actions Bot commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

📦 Bundle size report

Compared against bundle-size-baseline.json (main).

@databricks/appkit

npm tarball (packed): 1.5 MB (+47 KB) — gzipped download (dist + bin; excludes release-only docs/NOTICE).

dist raw gzip
JS (runtime) 1.5 MB (+55 KB) 553 KB (+17 KB)
Type declarations 489 KB (+3.0 KB) 177 KB (+1.4 KB)
Source maps 3.0 MB (+107 KB) 1.0 MB (+31 KB)
Other 11 KB 3.7 KB
Total 5.0 MB (+165 KB) 1.7 MB (+50 KB)
Per-entry composition (own code — deps external (as shipped))
Entry Initial (gz) Lazy (gz) Total (gz) node_modules (min) Own code (min)
. 118 KB (+22 B) 2.4 KB 121 KB (+22 B) external 378 KB (+30 B)
./beta 127 KB (+47 B) 4.6 KB (+26 B) 131 KB (+73 B) external 381 KB (+144 B)
./beta/database-tooling 45 KB (+10 KB) 0 B 45 KB (+10 KB) external 133 KB (+31 KB)
./testing 47 KB (-2 B) 38 KB (+21 B) 85 KB (+19 B) external 246 KB (+30 B)
./tsdown 630 B 0 B 630 B external 1.0 KB
./type-generator 31 KB (+25 B) 0 B 31 KB (+25 B) external 89 KB (+30 B)

Chunks:

Entry Chunk Load Size (gz)
. index.js initial 114 KB
. utils.js initial 4.6 KB
. remote-tunnel-manager.js lazy 2.4 KB
./beta beta.js initial 100 KB
./beta stream-manager.js initial 5.9 KB
./beta define-schema.js initial 5.9 KB
./beta service-context.js initial 4.3 KB
./beta modular.js initial 3.6 KB
./beta databricks.js initial 3.3 KB
./beta wide-event-emitter.js initial 2.8 KB
./beta client.js initial 645 B
./beta index.js initial 20 B
./beta index.js lazy 4.1 KB
./beta supervisor-api.js lazy 201 B
./beta databricks.js lazy 188 B
./beta index.js lazy 124 B
./beta/database-tooling index.js initial 45 KB
./testing manifest.js initial 33 KB
./testing index.js initial 10 KB
./testing wide-event-emitter.js initial 2.9 KB
./testing index.js lazy 34 KB
./testing remote-tunnel-manager.js lazy 2.4 KB
./testing utils.js lazy 1.8 KB
./tsdown index.js initial 630 B
./type-generator index.js initial 31 KB

@databricks/appkit-ui

npm tarball (packed): 393 KB (-4 B) — gzipped download (dist + bin; excludes release-only docs/NOTICE).

dist raw gzip
JS (runtime) 430 KB 146 KB
Type declarations 250 KB 93 KB (-1 B)
Source maps 837 KB 281 KB
CSS 16 KB 3.2 KB
Total 1.5 MB 522 KB (-1 B)
Per-entry composition (consumer bundle — deps bundled, peerDeps external)
Entry Initial (gz) Lazy (gz) Total (gz) node_modules (min) Own code (min)
./js 5.3 KB 49 KB 55 KB 208 KB 14 KB
./js/beta 2.4 KB 0 B 2.4 KB 0 B 5.3 KB
./react 441 KB 49 KB 489 KB 1.4 MB 180 KB
./react/beta 13 KB 0 B 13 KB 25 KB 13 KB

Chunks:

Entry Chunk Load Size (gz)
./js index.js initial 5.2 KB
./js chunk initial 120 B
./js apache-arrow lazy 49 KB
./js/beta beta.js initial 2.4 KB
./react index.js initial 439 KB
./react tslib initial 2.1 KB
./react apache-arrow lazy 49 KB
./react/beta beta.js initial 13 KB

Signed-off-by: ditadi <victordperd@gmail.com>
knip.json ignores tools/**, so knip reported esbuild, ora and zod as unused even though
tools/bundle-size.ts, tools/generate-json-schema.ts and tools/playground/deploy-playground.ts
import them. It also reported packages/appkit/bin/appkit.js as an unused file. Declare these
as entry points so the pre-commit knip check passes without suppressing any finding.

Signed-off-by: ditadi <victordperd@gmail.com>
reconcileMigrations kept one recorded hash per created_at, so a committed migration whose
journal entry shares a when with an applied sibling was reported as edited and never ran.
Keep every recorded hash per when, and classify an entry whose sibling owns the recorded
row as pending or stranded.

Signed-off-by: ditadi <victordperd@gmail.com>
Signed-off-by: ditadi <victordperd@gmail.com>
Signed-off-by: ditadi <victordperd@gmail.com>

# Conflicts:
#	pnpm-lock.yaml
ditadi and others added 26 commits November 25, 2026 17:24
The design now names the ambiguous reconcile state, the 10 s
client_connection_check_interval the migrating session sets, and the
DATABRICKS_CLIENT_SECRET that migrate on start requires.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: ditadi <victordperd@gmail.com>
…tack/database-dev-flow/02-default-app-schema

# Conflicts:
#	apps/dev-playground/config/database/schema.ts

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: ditadi <victordperd@gmail.com>
…ck/database-dev-flow/03-db-migrate

# Conflicts:
#	bundle-size-baseline.json
#	pnpm-lock.yaml

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: ditadi <victordperd@gmail.com>
Main replaced the wrapper's `export type *` with an explicit list, which
drops the request type the database tooling's client interface uses.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: ditadi <victordperd@gmail.com>
…pegen helpers

appkit db migrate now loads schema.ts through loadDatabaseSchema and writes
the migration journal through writeIfChanged, both from main's database
typegen. The CLI-only branch of the runtime loader and the stack's own
atomic writer are deleted. loadDatabaseSchema keeps the import error as the
cause of its bounded message, so the CLI still prints where schema.ts failed.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: ditadi <victordperd@gmail.com>
migrate now runs main's generateDatabaseOperations next to database.d.ts,
so the operation contracts and routes.gen.ts follow a schema change without
a separate appkit generate-types. The CLI prints the file it wrote.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: ditadi <victordperd@gmail.com>
…is left

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: ditadi <victordperd@gmail.com>
…anges

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: ditadi <victordperd@gmail.com>
…ase-dev-flow/04-db-introspect

# Conflicts:
#	packages/appkit/src/database/tooling/migrate.ts
#	packages/appkit/src/database/tooling/migrations.ts

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: ditadi <victordperd@gmail.com>
…t the discard

The 03 merge removed the CLI option of the runtime loader and made
discardMigrationFiles async. introspect now loads schema.ts through
loadDatabaseSchema and waits for the discard before it reports the failure.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: ditadi <victordperd@gmail.com>
…pect

introspect and migrate now share writeGeneratedTypes, which writes
database.d.ts and then runs main's operation generator. A failed introspect
puts back both type files and routes.gen.ts with the rest of what it wrote.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: ditadi <victordperd@gmail.com>
…t it cannot express

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: ditadi <victordperd@gmail.com>
Setup now reads whether its role may use the app schema before the catalog
check. The playground's in-memory edge answered every pg_catalog query with
column rows, so setup read a missing USAGE and failed.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: ditadi <victordperd@gmail.com>
…tack/database-dev-flow/02-default-app-schema

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: ditadi <victordperd@gmail.com>
…ck/database-dev-flow/03-db-migrate

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: ditadi <victordperd@gmail.com>
…ase-dev-flow/04-db-introspect

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: ditadi <victordperd@gmail.com>
…urce name

A value with text around projects/<p>/branches/<b>/endpoints/<e> named a branch
the refusal of dev and session branches then checked, though the value is not
the app's endpoint. Anchor the match so such a value is refused as naming no
branch.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: ditadi <victordperd@gmail.com>
Name the LAKEBASE_ENDPOINT resource name that migrate on start reads the
branch from, and that a start without the flag or the app's identity skips the
refusal with the migration.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: ditadi <victordperd@gmail.com>
…tack/database-dev-flow/02-default-app-schema

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: ditadi <victordperd@gmail.com>
…ck/database-dev-flow/03-db-migrate

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: ditadi <victordperd@gmail.com>
…signal

On SIGINT or SIGTERM the lock was released even when another listener kept
the process running, so the command went on writing the history after another
command could take the lock. Release and re-raise only when the lock's handler
is the one ending the process; otherwise the exit listener releases it.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: ditadi <victordperd@gmail.com>
A route module that failed to load made migrate fail after it had written the
new migration, without saying it kept the file or what the migration warns
about. Write database.d.ts and database-operations.d.ts first, so that failure
stops migrate before it writes anything; the types still follow schema.ts when
applying fails.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: ditadi <victordperd@gmail.com>
loadDatabaseSchema kept the raw import error as the cause for every caller,
including the development start that main keeps to a bounded message. The
cause is now attached only when the caller passes showImportError, which only
the db CLI does.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: ditadi <victordperd@gmail.com>
SIGINT and SIGTERM release it only when nothing else handles the signal, and
any other way of ending can leave the file for the next command to report.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: ditadi <victordperd@gmail.com>
…ase-dev-flow/04-db-introspect

# Conflicts:
#	packages/appkit/src/database/tooling/migrate.ts

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: ditadi <victordperd@gmail.com>
loadDatabaseSchema now keeps the import error only for a caller that asks, so
introspect asks for it when it loads the schema.ts it replaces and the one it
generates.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: ditadi <victordperd@gmail.com>
@ditadi
ditadi removed this pull request from stack #667 November 26, 2026 14:50
@ditadi
ditadi force-pushed the stack/database-dev-flow/03-db-migrate branch from 6edeb03 to 0cae6bf Compare November 26, 2026 14:51
@ditadi

ditadi commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

Folded into #666. The database development flow stack is now three PRs: #654 (runtime), #666 (appkit db commands) and #675 (template and docs). appkit db introspect ships in #666 unchanged. The branch stays for reference.

This comment was written by Isaac.

@ditadi ditadi closed this Nov 26, 2026
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.

1 participant