Skip to content

feat(appkit): add appkit db init, clean and seed - #672

Closed
ditadi wants to merge 109 commits into
stack/database-dev-flow/04-db-introspectfrom
stack/database-dev-flow/05-db-init
Closed

ditadi wants to merge 109 commits into
stack/database-dev-flow/04-db-introspectfrom
stack/database-dev-flow/05-db-init

Conversation

@ditadi

@ditadi ditadi commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

🥞 Stacked PR (generated by Stack)

Use this link to review incremental changes.


Stacked on #670. The design is #677.

What did you change, and why?

Each checkout needs its own Lakebase branch for local development, set up without prompts so an agent or a script can run it. This PR adds appkit db init, appkit db clean and appkit db seed.

appkit db init uses your dev-<you>-<hash> branch, or with --session a session-<user hash>-<id> branch that expires after 7 days (--ttl changes that). It forks a new branch 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, and points .env at the branch. It hands you the app schema when the fork copied it owned by another Lakebase role, such as the app's service principal.

Then init starts the schema, which it decides from the branch (see the fix round below). --schema migrate, introspect, clean and none choose a start explicitly. init installs the pinned drizzle-kit for every start except none when the app lacks it. init refuses the project's default branch, protected branches and any branch the deploy boundary covers, before it changes anything.

clean drops the app schema, migration journal included, 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. clean and seed run only on your dev or session branch. .env is written with mode 0600 through a temporary file, and a symlinked .env is refused.

This revision changes these behaviors:

  • init checks its planned branch against the whole deploy boundary from feat(appkit): add the appkit db commands for the database development flow #666. A databricks.yml reference the checkout's files cannot resolve refuses init before any API call or .env write.
  • init holds one history lock for its whole run, across the clean, migrate or introspect it starts. clean holds the lock too. clean, and init with --schema migrate or --schema clean, refuse a missing schema.ts before taking the lock, so a refused run leaves no empty config/database.
  • init --schema none reads no schema.ts, takes no lock, and hands off nothing. The schema and its owner stay as the fork copied them.
  • Schema public is never handed off. It belongs to the database, so AppKit never deletes its owner or reassigns its objects.
  • bundle-size-baseline.json is regenerated. The packed tarball grows from 1,364,290 to 1,448,119 bytes (+6.14%), over the 5% budget, because ./beta/database-tooling grows from 28 KB to 45 KB gzipped.

Session branch names carry a hash of your user name (#666). init treats .env as this checkout's session branch only when the name matches your hash, so a .env copied from another developer never leads init to their branch. appkit db stays hidden from appkit --help.

Review round, 2026-11-09:

  • A branch init forks starts without the rows the fork copied: clean with schema.ts, or introspect and then a rebuild from the baseline. --schema migrate or --schema introspect keeps them; a reused branch is migrated as before. In public, the report lists what other roles own and keep.
  • init refuses a database databricks.yml binds that the fork lacks, reads the deploy target through variable-overrides.json, resumes an interrupted session rotation, reconciles with a concurrent run that created the endpoint or role first, writes PGSSLMODE=require over weaker values, and hands off and introspects the schema schema.ts declares.
  • The schema hand-off refuses when the copied owner also owns objects outside the app schema; clean previews the full CASCADE closure; drizzle-kit's installer uses shared's package-manager detection; .env rewrites recognise KEY: value and go through tooling/files.ts; the .env temp file is git-ignored in the template.

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

  • The APPKIT_DB_NEW_BRANCH marker is gone (L236, L690). init reads the branch. An app schema still owned by another role is an untouched copy, and init rebuilds it. Before the hand-off, init tags the copied journal with one row, only in a journal the migrator trusts, so a rerun from any checkout finishes an interrupted rebuild. A branch this run created is rebuilt even when you already owned the source schema. When the copy cannot be tagged, init warns first.
  • --schema none touches nothing, so the next plain init still rebuilds a copy. Explicit --schema migrate or introspect keep the rows and say that --schema clean removes them. migrate, introspect and seed refuse an untouched or tagged copy.
  • init installs drizzle-kit for every start except none (L541), and forks the branch the default databricks.yml target binds when it resolves (L633). The hand-off error stops suggesting ALTER ... OWNER TO.
  • A declined clean is an init outcome instead of a named error, and the report says why init rebuilt.

How do you know it works?

  • init.test.ts runs runInit against an in-memory Lakebase. It covers branch creation and reuse, session TTLs, a 403, a 409, the branch limit, and deploy-target and unresolved refusals before any API call. It also covers the hand-off and its failures, --schema none, public, and one lock across init's clean and migrate.
  • env-file.test.ts, clean-seed.test.ts and the CLI's init-clean-seed.test.ts cover the .env writer, the clean and seed refusals, the confirmations and the exit codes.
  • clean.pg.test.ts and hand-off.pg.test.ts run against Postgres. They check what CASCADE takes outside the app schema, a public clean, and the objects and grants that block a role delete.
  • At this PR's head, the database, plugin and CLI db tests passed against a local PostgreSQL 14, typecheck passed, and a clean build matched the new size baseline. 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 init, clean, seed and session-branch deletion. CI runs the full suite on this PR.
  • This revision was not run against a real Lakebase workspace, so who owns public on a real Lakebase branch was not observed.

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.

`appkit db init` gives a checkout its own Lakebase branch, forked from the
project's default branch: your dev branch, or with --session a branch for
this checkout that expires after 7 days (or --ttl). It asks nothing. It
refuses the default branch, protected branches and branches databricks.yml
deploys to before any change, and when the project is at its branch limit it
lists your branches to delete.

It makes sure the branch has a read-write endpoint that suspends after 5 idle
minutes and a Postgres role for you, points .env at it with an atomic 0600
write, hands you the app schema when the fork copied it owned by another
Lakebase role, installs drizzle-kit when the app lacks it, then starts the
schema by migrating, introspecting or cleaning.

`appkit db clean` drops the app schema (only your objects in public) and
lists what outside it went along; --delete-branch deletes this checkout's
session branch instead. `appkit db seed` runs config/database/seed.sql in one
transaction with the app schema on the search_path. Both run only on a branch
created for you or this checkout.

Signed-off-by: ditadi <victordperd@gmail.com>
@ditadi
ditadi force-pushed the stack/database-dev-flow/05-db-init branch from c56de21 to 98ef27b 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 45822394412 -R databricks/appkit -n appkit-template-0.105.0-pr.8cabeb5-stack-database-dev-flow-05-db-init-672 -D appkit-pr-672 \
  && unzip -o "appkit-pr-672/appkit-template-0.105.0-pr.8cabeb5-stack-database-dev-flow-05-db-init-672.zip" -d "appkit-pr-672" \
  && databricks apps init --template "appkit-pr-672"

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 (+606 B) — gzipped download (dist + bin; excludes release-only docs/NOTICE).

dist raw gzip
JS (runtime) 1.6 MB (+388 B) 576 KB (+159 B)
Type declarations 498 KB (+64 B) 181 KB (+21 B)
Source maps 3.1 MB (+1.2 KB) 1.0 MB (+436 B)
Other 11 KB 3.7 KB
Total 5.2 MB (+1.6 KB) 1.8 MB (+616 B)
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 (+18 B) 4.6 KB (+25 B) 131 KB (+43 B) external 381 KB (+63 B)
./beta/database-tooling 56 KB (+60 B) 0 B 56 KB (+60 B) external 167 KB (+174 B)
./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 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 56 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 — gzipped download (dist + bin; excludes release-only docs/NOTICE).

dist raw gzip
JS (runtime) 430 KB 146 KB
Type declarations 250 KB 93 KB
Source maps 837 KB 281 KB
CSS 16 KB 3.2 KB
Total 1.5 MB 522 KB
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

ditadi and others added 14 commits October 30, 2026 16:31
Signed-off-by: ditadi <victordperd@gmail.com>

# Conflicts:
#	packages/appkit/src/database/tooling/migrate.ts
#	packages/appkit/src/database/tooling/naming.ts
#	packages/appkit/src/database/tooling/tests/guard.test.ts
#	packages/appkit/src/database/tooling/tests/naming.test.ts
#	packages/appkit/src/database/tooling/tests/targets.test.ts
init still checked its planned branch against the readDeployBranches list that
the deploy boundary replaced. It now asks deployRefusal, like every other
guard, so a binding the checkout's files cannot resolve refuses init before any
API call or .env write. readDeployTarget reads the default target from the same
bundle model: its first resolved app binding, else its postgres_branch and
postgres_database variables. The shared CLI test mocks the beta tooling path.

Signed-off-by: ditadi <victordperd@gmail.com>
Signed-off-by: ditadi <victordperd@gmail.com>
clean drops the branch's migration journal, and init runs clean, migrate or
introspect after its own checks, so another appkit db command could run in
between. runClean now holds the checkout's history lock, and init holds one lock
for its whole run and hands it to cleanHolding, migrateHolding and
introspectHolding. init --schema none changes no history, so it takes no lock
and creates no config/database.

Signed-off-by: ditadi <victordperd@gmail.com>
init --schema none still handed the app schema to the developer, which deletes
the branch copy of the role that owns it, in apps that may have no database
schema contract at all. It now leaves the schema and its owner as the fork
copied them. Schema public is never handed off either: it belongs to the
database (pg_database_owner on PostgreSQL 15 and later, the bootstrap superuser
before), so AppKit never deletes its owner or reassigns its objects.

Signed-off-by: ditadi <victordperd@gmail.com>
Regenerated with pnpm clean && pnpm build && pnpm size:baseline. Against the
previous baseline the @databricks/appkit packed tarball grows from 1364290 to
1448119 bytes (+83829, +6.14%), over the 5% budget. The growth is the CLI-only
@databricks/appkit/beta/database-tooling entry (45 KB gzipped, from 28 KB) with
the introspect fixes and init, clean and history lock code it ships.
@databricks/appkit-ui is unchanged at 406741 bytes.

Signed-off-by: ditadi <victordperd@gmail.com>
The re-run test ran with --schema none, which skips the hand-off, so no test
reached the check that keeps init from deleting your own role. It now runs the
default migrate start and asserts that the owner query ran for schema app and
no role was deleted.

Signed-off-by: ditadi <victordperd@gmail.com>
init --schema none hands off no schema and starts none, but it still loaded
schema.ts for the schema name, so a half-edited file stopped a branch-only
init. It now loads schema.ts only when the hand-off and the start use it.

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

withHistoryLock creates config/database for its lock file, and init and clean
checked for schema.ts only after taking it, so a refused init or clean left an
empty config/database behind. Both now check first, as runMigrate does.

Signed-off-by: ditadi <victordperd@gmail.com>
dotenv 16.6.1 reads `KEY: value` (a colon followed by whitespace) as an
entry, and a later entry wins. updateEnvFile's parser only knows `KEY=`, so
a later `PGHOST: old` outlives the rewrite and dotenv reads the old host.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: ditadi <victordperd@gmail.com>
updateEnvFile only recognized `KEY=`. dotenv 16.6.1 also reads `KEY: value`
when whitespace follows the colon, and its last entry wins, so a later
`PGHOST: old` outlived the rewrite. The parser now treats that form as an
entry too and writes it back as `KEY=`.

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

updateEnvFile writes `..env.<pid>.<hex>.tmp` and renames it over .env. A run
killed between the two leaves the file, which holds the .env contents, and
nothing ignored it.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: ditadi <victordperd@gmail.com>
Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: ditadi <victordperd@gmail.com>
init kept any existing PGSSLMODE. The Lakebase pool maps prefer to
rejectUnauthorized: false, disable to no TLS, and rejects every other value,
so only require gives a checked TLS connection. init now writes require over
anything else and says what it replaced.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: ditadi <victordperd@gmail.com>
ditadi and others added 27 commits November 25, 2026 16:29
…spects, then rebuilds

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

The design rebuilds a copy from the baseline introspect writes when there is
no schema.ts. A tagged copy now takes that path too instead of refusing.

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

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

After the rebuild the rows are gone, so the flag cannot keep them on this branch.

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

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

Another role owns that schema, so the drop failed with Postgres's raw "must be
owner". clean and its preview now refuse first and say that init takes the
copy over and rebuilds it.

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

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: ditadi <victordperd@gmail.com>
.env read an invalid id as unset, so init generated a new one and overwrote
the hand-set value. Every command now refuses it with naming.ts's message.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: ditadi <victordperd@gmail.com>
A failed session run also names --session in its rerun advice.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: ditadi <victordperd@gmail.com>
…me --session in its advice

A --session run writes APPKIT_DB_SESSION before the fork and points .env at
the branch last. When it stopped in between, the next plain init went to the
dev branch. It now picks the session when .env does not point at your dev
branch and the session's branch exists, and a session run's rerun advice says
`appkit db init --session`.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: ditadi <victordperd@gmail.com>
… and its win over .env

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: ditadi <victordperd@gmail.com>
…and let it win over .env

init read the database databricks.yml binds only after the fork, so a missing
one left a new branch with copied rows behind, and a PGDATABASE already in
.env skipped the check. The create path now reads the binding on the source
branch before createBranch. A reused branch is still checked after. The bound
database replaces a different PGDATABASE, and init says so.

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

The blocker count mixed privileges in with what the role owns, so the failure
message called grants blocking objects. What the owner holds is now split:
objects in other databases or shared ones, and privileges, each reported
apart. Objects in another database of the branch, which the reassign cannot
reach, are refused before the delete. An extension the owner has stays a
refusal that names it and the manual delete-role command; a test pins it.

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

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

A quoted Postgres name can hold control characters that rewrite the terminal.
The labels clean and the hand-off read, the owners their refusals name, and
the copy's owner in migrate, introspect and seed refusals now go through
introspect's shown(), so the CLI's output prints them escaped too.

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

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

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

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: ditadi <victordperd@gmail.com>
Layer 03 made the databricks.yml reader take the command's environment,
so a BUNDLE_VAR_ value counts the way a deploy from the shell counts
it. init passed no environment to readDeployBoundary or to
readDeployTarget, the reader of the branch it forks and the database
it binds. Both now get ctx.env, and readDeployTarget applies the
BUNDLE_VAR_ view ahead of variable-overrides.json.

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

# Conflicts:
#	bundle-size-baseline.json
#	packages/appkit/src/database/tooling/migrate.ts
#	packages/appkit/src/database/tooling/tests/migrate.test.ts

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: ditadi <victordperd@gmail.com>
The 03 merge removed the CLI option of the runtime loader, so init, clean
and seed now load schema.ts through loadDatabaseSchema. The init test
reports carry the operation types file migrate and introspect now return.

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>
…tabase-dev-flow/05-db-init

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

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

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

loadDatabaseSchema now keeps the import error only for a caller that asks, so
the commands that read the app schema from schema.ts ask for it.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: ditadi <victordperd@gmail.com>
The warning about the bound database, the step that points .env at the branch,
the hand-off messages and the report the CLI prints carried the Postgres
database name raw, so a name with a control character reached the terminal.
Pass them through shown(); .env and the connections keep the raw name.

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

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: ditadi <victordperd@gmail.com>
CI sets CI, so picocolors colors the warning marker and the assertion on
"! Could not mark ..." failed there while it passed in a local shell.

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 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 init, clean and seed ship 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