Skip to content

feat(appkit): add the appkit db commands for the database development flow - #666

Draft
ditadi wants to merge 2 commits into
stack/database-dev-flow/01-migrations-on-deployfrom
stack/database-dev-flow/03-db-migrate
Draft

ditadi wants to merge 2 commits into
stack/database-dev-flow/01-migrations-on-deployfrom
stack/database-dev-flow/03-db-migrate

Conversation

@ditadi

@ditadi ditadi commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Stacked on #654. The design is #677.

This PR absorbs #670 (introspect), #672 (init, clean and seed), #674 (verify) and #678 (check). It also takes the CLI parts of the old #675: the verify warning for a .env branch that is not yours, and the plugin docs for the development flow.

What did you change, and why?

#654 applies committed migrations when the deployed app starts, so something has to write those migrations and try them first. This PR is for the developer or agent who edits config/database/schema.ts. They get a Lakebase branch of their own and one command per step, with no prompts, so a script or an agent can run the whole loop and read each error. CI gets an offline check of the committed history.

appkit db ships seven commands:

  • init points .env at your dev-<you>-<hash> branch, or with --session at a session-<user hash>-<id> branch that expires after 7 days (--ttl changes that). It forks from the branch the default databricks.yml target binds, or from the project's default branch when that target does not resolve, and says which. It makes sure the branch has a read-write endpoint that suspends after 5 idle minutes and your Postgres role with DATABRICKS_SUPERUSER, then hands you the app schema when the fork copied it owned by another role. A fresh copy is rebuilt from the migrations, so its app tables start empty. --schema migrate or --schema introspect keeps the copied rows, --schema clean removes them, and --schema none touches nothing. init installs the pinned drizzle-kit for every start except none.
  • migrate writes the next migration from schema.ts with drizzle-kit, regenerates database.d.ts and database-operations.d.ts under shared/appkit-types, and applies pending migrations to your own dev or session branch. On any other branch it still writes the migration and says why it applied nothing. It warns about statements that break the previous release, such as drops, renames, type changes and a required new column, and about statements that may fail on rows the deploy branch has. When Postgres rejects the migration it just wrote, it removes that migration and restores the journal, so the next run writes it again.
  • introspect writes schema.ts from an existing Postgres schema, keeps the old file as schema.ts.bak and the .private() columns it declared, and writes the migration that matches the database, a first 0000_baseline or the next NNNN_introspect. It records that migration as applied with a compare-and-set. It refuses a migration that would drop what schema.ts cannot express, and warns about indexes and constraints that a database built from the migrations will not have.
  • clean drops the app schema and its journal in one transaction. On public it drops only what your role owns. clean --delete-branch deletes this checkout's session branch.
  • seed runs config/database/seed.sql in one transaction.
  • verify checks your branch in one read-only transaction that it rolls back. It reports declared tables and columns the branch lacks, unapplied, stranded, edited and unknown migrations, and, on your own branch, a fork copy that init has not rebuilt. verify --deployed -t <target> reads only the Databricks API. It checks that the target's app exists and runs, is bound to the target's branch and database with CAN_CONNECT_AND_CREATE, and has a role on the branch. It also checks that the source snapshot of the active deployment has every committed migration with the same journal entry and byte-identical SQL. It refuses when the target's workspace.host is not the workspace the shell is authenticated against.
  • check is the offline check of the committed migrations that CI runs. It needs no Databricks or Lakebase credentials. Among other findings, it fails when schema.ts changed without a migration, when the journal is out of order or an entry's idx is not its position, when a snapshot does not follow the one before it, which happens when two git branches each add a migration, and when a migration file is missing or unlisted. Each finding names its fix.

The commands share these rules:

  • Writes go only to your own dev branch and this checkout's session branch. The guard reads every databricks.yml target, every postgres resource, the files include: names, and each target's variable-overrides.json. A branch any of them binds takes no writes. A reference those files cannot resolve refuses the write and names the reference.
  • Commands that change the migration history hold config/database/.appkit-db.lock, created exclusively with mode 0600. A second command in the checkout fails and names the holder.
  • .env is written with mode 0600 through a temporary file, and a symlinked .env is refused. A connection needs PGHOST to be a host of LAKEBASE_ENDPOINT, and refuses a PGSSLMODE that does not verify the server's certificate.

Packaging and runtime:

  • The commands' logic ships in @databricks/appkit/beta/database-tooling, which only the AppKit CLI of the same release imports. drizzle-kit is an optional peer dependency pinned to 0.31.10, and only the tooling loads it. An oxlint rule stops runtime code in packages/appkit/src from importing the tooling or drizzle-kit.
  • The runtime gains exports the commands use, and two startup errors now name a command. When the flag is off, the missing-table error suggests npx appkit db migrate. The ownership error now also says to rerun appkit db init on a dev or session branch.
  • The plugin docs get a Development workflow section with the deploy, failure, backfill and stuck-start runbooks and a CI section. The design doc gets the passages for these commands. template/_gitignore lists the files the commands write.

What this PR does not do:

  • appkit db stays hidden from appkit --help.
  • A deployed app runs nothing new. Its only changes are the two error messages above.
  • The template does not set APPKIT_DB_MIGRATE_ON_START yet. The docs say to add it to the env of app.yaml, and feat(appkit): render the database development flow in the app template #675 adds it for scaffolded apps.
  • The guard reads only this checkout's files. It does not see --var, BUNDLE_VAR_* or resources defined in Python, and it does not replace Lakebase permissions.

How do you know it works?

  • The tooling tests cover each command against an in-memory Lakebase and faked Databricks APIs. targets.test.ts and guard.test.ts cover every binding kind, overrides, included files and unresolved references. history-lock.test.ts covers how the lock is taken, released and reported when stale. init.test.ts covers branch creation and reuse, TTLs, a 403, a 409, the branch limit, the refusals before any API call, the hand-off and each --schema start. verify.test.ts runs --deployed with no role on the deploy branch and asserts that no database connection or write happens. check.test.ts generates real history with drizzle-kit, with every Databricks and Lakebase call throwing.
  • The *.pg.test.ts suites run against Postgres. They cover a migration that fails on existing rows and is removed, what clean takes with CASCADE, what blocks a role delete, the catalog reads, init's hand-off and rebuild of a fork copy, and how verify reports a copy.
  • The CLI's db tests cover output, prompts and exit codes, and contract.test.ts checks that the tooling matches the CLI's contract.
  • On this branch, with Postgres 17, pnpm build, pnpm -r typecheck, pnpm check, pnpm test (385 files and 7217 tests passed, 1 skipped), CI=1 pnpm exec vitest run packages/shared/src/cli/commands/db (5 files, 66 tests) and pnpm docs:build passed. They ran one commit before a one-line fix to the design doc's Flag Strategy table, and the head differs from that commit only in that line.
  • load-routes.watch.test.ts, a watcher test from main, timed out once. It passed on rerun, alone and in the full suite.
  • The second commit removes narration comments and moves their meaning into names and small extracted functions. A script listed every string literal the diff changes, and no text in a throw, warn, info, ok or fail call changed. Tests changed only to follow the renames, and migrate.test.ts now sets isTTY itself.
  • Two GPT reviewers read the consolidation. The split reviewer found one doc issue, how the design doc described which branches appkit db changes, and that line is fixed. The cleanup reviewer found nothing.
  • bundle-size-baseline.json records the new @databricks/appkit size. The packed tarball grows from 1,454,413 to 1,638,260 bytes (+12.6%), mostly from the new ./beta/database-tooling entry (63 KB gzipped). It was measured before the comment cleanup, which only shrinks the package.
  • Earlier rounds checked real Lakebase. Earlier revisions of the old feat(appkit): add the appkit db commands for the database development flow #666 and feat(appkit): add appkit db verify #674 checked currentUser.me, endpoint and branch lookups, the Apps API's postgres resource format, a missing app's 404, a deployment's source_code_path and getHost, all live and read-only. On 2026-11-15, a throwaway Lakebase fork confirmed that the developer role can write the service principal's rows but cannot alter its tables.

This revision was not run against a real Lakebase workspace. Nobody has observed who owns public on a real Lakebase branch, or checked that workspace/export with format=AUTO returns a file's exact bytes. If it does not, verify --deployed reports the migration as changed, which fails closed. The design's done criteria for this PR, a developer's init, migrate and verify on a throwaway app, and two parallel migrations that check fails, are still open.

This pull request and its description were written by Isaac.

@ditadi
ditadi added this pull request to stack #667 October 13, 2026 13:34
@github-actions

github-actions Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

📦 Bundle size report

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

@databricks/appkit

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

dist raw gzip
JS (runtime) 1.6 MB (-30 KB) 575 KB (-12 KB)
Type declarations 495 KB (-8.6 KB) 179 KB (-3.8 KB)
Source maps 3.1 MB (-56 KB) 1.0 MB (-21 KB)
Other 11 KB 3.7 KB
Total 5.2 MB (-95 KB) 1.8 MB (-37 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 (+5 B) 2.4 KB 121 KB (+5 B) external 378 KB (+7 B)
./beta 127 KB (+251 B) 4.6 KB (+5 B) 132 KB (+256 B) external 383 KB (+821 B)
./beta/database-tooling 62 KB (+243 B) 0 B 62 KB (+243 B) external 187 KB (+1.0 KB)
./testing 47 KB (+3 B) 38 KB (+4 B) 85 KB (+7 B) external 246 KB (+7 B)
./tsdown 630 B 0 B 630 B external 1.0 KB
./type-generator 31 KB (+5 B) 0 B 31 KB (+5 B) external 89 KB (+7 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 101 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 62 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

@github-actions

github-actions Bot commented Oct 13, 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 45843177193 -R databricks/appkit -n appkit-template-0.105.0-pr.9c91582-stack-database-dev-flow-03-db-migrate-666 -D appkit-pr-666 \
  && unzip -o "appkit-pr-666/appkit-template-0.105.0-pr.9c91582-stack-database-dev-flow-03-db-migrate-666.zip" -d "appkit-pr-666" \
  && databricks apps init --template "appkit-pr-666"

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.

ditadi and others added 2 commits November 26, 2026 15:36
… flow

`appkit db` gives each developer a Lakebase branch of their own and the
commands that write, apply and check migrations there. The command group is
hidden from `appkit --help`, and its logic ships in
`@databricks/appkit/beta/database-tooling`, which only the AppKit CLI of the
same release imports. drizzle-kit is an optional peer that only the tooling
loads, and a lint rule keeps runtime code from importing the tooling.

What ships:

- `migrate`: writes the migration when schema.ts changed, regenerates
  database.d.ts and database-operations.d.ts, and applies pending migrations
  to your own branch only. It holds the checkout's history lock, checks the
  drizzle-kit version, warns about statements that break the previous release
  or may fail on existing rows, refuses a branch databricks.yml deploys to, and
  connects only with PGSSLMODE=require.
- `introspect`: writes schema.ts and a 0000_baseline (or NNNN_introspect) from
  existing tables, keeps the old schema.ts as schema.ts.bak and its private
  columns, records the baseline with compare-and-set, and refuses a migration
  that would drop objects schema.ts cannot express.
- `init`, `clean`, `seed`: dev and session branches with no prompts, an
  untouched fork copy rebuilt without its app rows, the hand-off of the app
  schema, atomic .env writes, and the drizzle-kit install.
- `verify`: the local check of your branch, and `--deployed` for a target's
  deployed app and the migrations in its source.
- `check`: the offline check of the committed migrations that CI runs.
- Startup errors that name the `appkit db` command to run, and the runtime
  exports the commands use.
- The plugin docs for the development flow, with the deploy, failure,
  backfill and stuck-start runbooks and the CI section, and the design doc
  passages for these commands.
- The template's .gitignore entries for the files the commands write.

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

Comments in the db tooling, the db CLI commands and the runtime helpers they
added that restated a name, narrated the next lines or argued about our own
code are gone. Comments that explain a foreign constraint (Postgres, pg,
dotenv, drizzle-kit, the Databricks CLI and APIs, Node) stay as they were.

Where a comment carried meaning a name should, the name now carries it:

- MigrationDrift.unknown is unknownRecordedCount, as on ApplyMigrationsResult,
  and readMigrationDrift is readMigrationDriftUnlocked.
- SchemaDiagnosis.advice and LocalVerifyReport.schemaAdvice are both
  tablesInPublicAdvice, and SchemaCheck.absent is absentTables.
- CleanReport.kept is keptOwnedByOthers, UnappliedMigration is
  UnappliedMigrationError, shortName is resourceId as in the tooling, the
  loader option showImportError is keepImportErrorAsCause, and parseJournal's
  label is nameInError.
- One exported CERT_VERIFYING_SSL_MODE replaces the pool's "require" and
  init's TLS_MODE.

Behavior-preserving extractions replace the prose that explained the control
flow: publishJournal and unpublishJournal name the journal write and its
restore, shouldDiscardGenerated holds migrate's discard decision, the
introspect fixture answers by the exported catalog SQL constants, and the
contract check is toolingMatchesCliContract. The migrate command test sets
isTTY instead of relying on the worker having none.

The plugin doc's Basic usage points local development at appkit db migrate,
and Migrations on deploy no longer says scaffolded apps set the flag, which
holds only once the template ships it.

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 changed the base branch from stack/database-dev-flow/02-default-app-schema to stack/database-dev-flow/01-migrations-on-deploy November 26, 2026 14:50
ditadi added a commit that referenced this pull request Nov 26, 2026
…ionals from one table

The health-check note in app.yaml.tmpl and the usage note in the scaffolded
schema.ts are gone. README.md.tmpl and the plugin doc carry the same content,
and the app.yaml note rendered into every app, with or without the plugin.

The smoke test's three copies of the strip-a-conditional block are one table
of named patterns run by one loop. It scaffolds byte-identical output with
and without --all-plugins.

The design doc's Background and Flag Strategy name the three PRs, #654, #666
and #675, instead of the closed ones. Migrations on deploy says again that
scaffolded apps set APPKIT_DB_MIGRATE_ON_START, which holds once this PR
ships the template.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: ditadi <victordperd@gmail.com>
@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 changed the title feat(appkit): add appkit db migrate feat(appkit): add the appkit db commands for the database development flow Nov 26, 2026
@ditadi
ditadi added this pull request to stack #752 November 26, 2026 14:52

This branch has not been deployed

No deployments
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