Skip to content

feat(appkit): default defineSchema tables to the app schema - #655

Closed
ditadi wants to merge 62 commits into
stack/database-dev-flow/01-migrations-on-deployfrom
stack/database-dev-flow/02-default-app-schema
Closed

ditadi wants to merge 62 commits into
stack/database-dev-flow/01-migrations-on-deployfrom
stack/database-dev-flow/02-default-app-schema

Conversation

@ditadi

@ditadi ditadi commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

🥞 Stacked PR (generated by Stack)

Use this link to review incremental changes.


Stacked on #654. The design is #677.

What did you change, and why?

On Lakebase, the deployed app's service principal has no CREATE on public, but it can create and own a schema of its own. #654 applies migrations on start as that service principal, so with public as the default the first deploy would fail until someone ran a grant by hand.

  • defineSchema() without a schemaName puts its tables in the Postgres schema app. The service principal creates app on its first start and owns everything in it, including the migration journal. DEFAULT_SCHEMA_NAME in define-schema.ts is the only place the default is set.
  • An explicit { schemaName: "public" } still produces unqualified SQL (pgTable and pgEnum).
  • Beta apps whose tables are in public now look in app and fail setup. When every missing table exists in public, the error says so and shows { schemaName: "public" }. It leaves out the usual advice to commit migrations, which would create a second, empty copy in app.
  • A 42501 while migrating into public gets its own hint. It says to grant CREATE ON SCHEMA public to the service principal, or to use the default app.
  • The plugin docs explain the default, say each app needs its own schema, and cover upgrading an app with tables in public.
  • The dev-playground passes { schemaName: "public" }, because its tables were created by hand in public.

Apps that name their schema are unchanged, and apps on the old default get an error that says what to change. This revision only merges the corrected #654 and current main. The diff of this PR is unchanged.

Review round, 2026-11-09:

  • With APPKIT_DB_MIGRATE_ON_START=true, startup refuses to apply migrations when defineSchema() was given no schemaName, none of the declared tables exist in app, and some exist in public. Before, the first migration created empty copies in app and the app served them. The lookup lives in database/migrations/tables-left-in-public.ts, shared with the startup check's advice.
  • The 42501 hint for schema public claims missing CREATE only when has_schema_privilege says so; otherwise it names the table ownership problem.

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

  • The public ownership hint stops advising ALTER TABLE ... OWNER TO, which no role can do for the app's service principal (L503).

How do you know it works?

  • define-schema.test.ts checks that tables and enums go to app by default and stay unqualified with an explicit public.
  • schema-check.test.ts checks that the error names public when every missing table is there, and keeps the usual advice when only some are there or the declared schema is public.
  • ownership.test.ts checks the public hint. migrate-on-start.test.ts uses the default, so it checks that the journal goes to app.
  • At this PR's head, the history tests passed, and a repeated start against a local PostgreSQL 14 applied nothing the second time. The full vitest suite, a clean build, the docs build and typecheck passed at the top of the stack (feat(appkit): add appkit db check #678), which contains this PR, and its packed tarballs export the same root and ./beta names as main. 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.

@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.4 MB (+43 KB) — gzipped download (dist + bin; excludes release-only docs/NOTICE).

dist raw gzip
JS (runtime) 1.4 MB (+49 KB) 519 KB (+18 KB)
Type declarations 482 KB (+59 B) 174 KB (+24 B)
Source maps 2.8 MB (+89 KB) 960 KB (+30 KB)
Other 11 KB 3.7 KB
Total 4.7 MB (+138 KB) 1.6 MB (+48 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 (+18 B) 2.4 KB (-1 B) 121 KB (+17 B) external 378 KB (+71 B)
./beta 127 KB (+8.6 KB) 4.5 KB (-2 B) 131 KB (+8.6 KB) external 381 KB (+25 KB)
./testing 47 KB (-1 B) 38 KB (+15 B) 85 KB (+14 B) external 246 KB (+71 B)
./tsdown 630 B 0 B 630 B external 1.0 KB
./type-generator 31 KB (+18 B) 0 B 31 KB (+18 B) external 89 KB (+71 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.0 KB
./beta supervisor-api.js lazy 201 B
./beta databricks.js lazy 188 B
./beta index.js lazy 124 B
./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

@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 45807903678 -R databricks/appkit -n appkit-template-0.105.0-pr.eb7e37a-stack-database-dev-flow-02-default-app-schema-655 -D appkit-pr-655 \
  && unzip -o "appkit-pr-655/appkit-template-0.105.0-pr.eb7e37a-stack-database-dev-flow-02-default-app-schema-655.zip" -d "appkit-pr-655" \
  && databricks apps init --template "appkit-pr-655"

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
ditadi force-pushed the stack/database-dev-flow/02-default-app-schema branch from f8a437d to ec8da9c Compare October 19, 2026 12:25
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>
ditadi and others added 11 commits October 30, 2026 12:52
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>
…re still in public

An app upgraded from a release that put tables in public, still on the default
schema app, has public.notes with its rows and no app.notes. migrateOnStart
applies the committed migrations before assertSchemaMatchesDatabase runs, so the
first migration creates an empty app.notes, the check then passes, and the app
serves the empty table. The advice about tables in public never shows.

Pin the refusal: with the default schema, none of the declared tables in app and
some in public, startup fails before anything is applied. An explicit
schemaName "app", or a declared table already in app, still migrates.

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

Two committed migrations with one `when`, neither matching the row
recorded at it, are both classified edited today, so the one that never
ran is skipped with only a warning. A second recorded row at a committed
`when` is reported as neither edited nor unknown. These tests expect each
timestamp to be reconciled one to one, and the migrator to refuse when
the pairing is ambiguous.

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

reconcileMigrations keyed each `when` on a set of recorded hashes, so two
files sharing a timestamp with one recorded row were both called edited
(the one that never ran was skipped with a warning), and a second recorded
row at a committed `when` was neither edited nor unknown.

Each `when` is now a bucket: exact hash matches are consumed first; one
file left with one row left is edited; files left with no row are pending
or stranded by lastRecorded; rows left with no file are unknown. Any other
remainder is ambiguous, and the migrator refuses it with a
MigrationHistoryError that names the timestamp, the files and the row
count, before taking the lock. MigrationHistoryError takes the refusal as
an object, with a `stranded` and an `ambiguous` field.

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

An app upgraded from a release that put tables in public, left on the default
schema app, has public.notes with its rows and no app.notes. migrateOnStart ran
before assertSchemaMatchesDatabase, so the first committed migration created an
empty app.notes, the check passed, and the app served the empty table. The
"tables are in public" advice only showed when the check failed.

Before applying anything, migrateOnStart now asks the catalog where the declared
tables are. When defineSchema() was not told a schemaName, none of the tables
exist in app and some exist in public, startup fails with the advice: keep them
in public with { schemaName: "public" }, move them (ALTER TABLE ... SET SCHEMA),
or name schemaName "app" explicitly when the tables in public are another app's.
defineSchema() records whether schemaName was explicit as a symbol-keyed
property; the public API is unchanged.

The lookup and the advice live in database/migrations/tables-left-in-public.ts,
exported as locateTables, detectTablesLeftInPublic, keepInPublicAdvice and the pg
adapter catalogSqlOf, so the startup check and the CLI can share one query.
assertSchemaMatchesDatabase's inPublicInstead now uses the same lookup.

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

On SQLSTATE 42501 for schema public, ownershipHint said the app's role has no
CREATE on public whenever another role owns public, which is always. An app that
already holds CREATE and fails on a table another role owns got told to grant a
privilege it has. Pin that with CREATE granted the hint names the object's owner
as the fault and how to transfer it, and that without CREATE the grant advice stays.

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

The migrator finds the journal by name in pg_class and then selects from
and inserts into it, so a view or another role's table at that name would
run as the migrator. These tests expect the catalog read to return the
relation's kind and owner, the read and the write paths to refuse
anything but an ordinary table owned by the schema's owner, the journal
to be created only after that check and without IF NOT EXISTS, and the
refusal to become a setup failure on start.

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

On SQLSTATE 42501 for schema public, ownershipHint claimed the app's role has no
CREATE on public whenever another role owns public, which is always the case. An
app that already holds CREATE and failed on a table another role owns was told to
grant a privilege it has.

schemaOwnership now also reads has_schema_privilege(current_user, schema, 'CREATE')
from pg_catalog, null when the schema does not exist. The hint claims missing
CREATE only when it is missing; with CREATE granted it names an object another
role owns as the fault and how to transfer it (ALTER TABLE ... OWNER TO).

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: ditadi <victordperd@gmail.com>
ditadi and others added 26 commits November 15, 2026 21:41
The comment audit of this fix round flagged 24 comments in this layer
that restate a name, a type, a body or a nearby message. They go. The
kept comments each explain a foreign fact the code cannot show, such as
drizzle's hashing and separator, git autocrlf, Lakebase's ALTER OWNER
refusal, and the PGUSER that Databricks Apps sets.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: ditadi <victordperd@gmail.com>
A stranded migration next to rows this checkout lacks means a branch
deployed first, and a deploy start refuses that as diverged rather than
as stranded, so the fix is the diverged runbook and not a new timestamp.
That rule lived in a comment over a bare boolean. It is now the predicate
branchDeployedFirst, true when unrecorded migrations here sit next to
recorded rows this checkout lacks, and the onDrift check stays at the
call site. Behavior is unchanged. The migration tests pin the stranded,
edited and diverged refusals.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: ditadi <victordperd@gmail.com>
# Conflicts:
#	packages/appkit/src/database/migrations/ownership.ts
…om the startup check

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

The startup check told an app that named its schema explicitly that
defineSchema() picks that schema unless told otherwise, which it had been.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: ditadi <victordperd@gmail.com>
The tools and bin/appkit.js entry points belong with the appkit db CLI,
which a later layer adds. This layer adds neither.

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

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

The deploy-start refusal kept its own copy of the shapes `appkit db init`
names branches with, so a change to the naming rules would switch the
refusal off without an error. database/branch-names.ts now holds them
outside tooling/, which runtime code may not import, for init's naming to
share.

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

The refusal of dev and session branches read the branch from
LAKEBASE_ENDPOINT and skipped itself when the variable was unset or not
an endpoint path, so a deployed app on an unknown branch migrated. Setup
now fails and names the variable.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: ditadi <victordperd@gmail.com>
A migration that failed with SQLSTATE class 22 or 23 named the code and
nothing else, so a deploy start that met rows a NOT NULL or a retype
cannot take gave no way forward. The error now says to repair the rows,
on the deploy branch as a role with the DATABRICKS_SUPERUSER membership,
and to deploy the same migration again.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: ditadi <victordperd@gmail.com>
Postgres checks CREATE on the database before it looks at IF NOT EXISTS,
so creating the journal in a schema the app already owned failed with
42501 for a role without that privilege. The migrator now reads
pg_namespace first and runs CREATE SCHEMA only when the schema is absent.

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

Migrate on start ran the catalog lookup on every start, so a current
deploy branch paid a second query beyond the one read of the journal
the design allows. applyPendingMigrations now takes a beforeApply hook
that runs on the migrating connection, under the lock, once something
is pending, and passes what it throws through unchanged. The deployed
app's refusal runs there.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: ditadi <victordperd@gmail.com>
drizzle-orm's reader turns any read failure, such as EACCES, into a
plain Error that says the file is missing. Migrate on start passed that
on as an unknown error, which setup replaces with a bare "Database setup
failed". It now becomes a MigrationFilesError that names the file and
the real reason.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: ditadi <victordperd@gmail.com>
The startup schema check reads pg_catalog, which lists the tables of a
schema the role cannot use. An app schema owned by another role without
USAGE for the app passed setup, and every request then failed with
42501. Setup now fails with the ownership hint, which names the owner
and the connecting role.

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

Only the exact value true turns migrate on start on. A deploy that set
TRUE, 1 or yes started without migrating and without saying why. The
flag stays strict, and setup now logs the value it ignored.

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

# Conflicts:
#	packages/appkit/src/database/migrations/tests/apply.pg.test.ts
#	packages/appkit/src/plugins/database/schema-check.ts
#	packages/appkit/src/plugins/database/tests/schema-check.test.ts

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: ditadi <victordperd@gmail.com>
Layer 02 made ownershipHint need whether the connecting role holds
CREATE on the schema, and the USAGE check from layer 01 did not read
it. The check now reads it in the same catalog query. The public
lookup test counts the USAGE query that now precedes the catalog
query.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: ditadi <victordperd@gmail.com>
# Conflicts:
#	packages/appkit/src/plugins/database/lifecycle.ts

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

A stranded migration is often main's own, so deleting its files is the
wrong fix. The runtime refusal now gives the same recovery as the design:
save hand-written SQL, merge main, reset the migrations to main's, then
clean and migrate. The text lives in shared so the CLI can reuse it.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: ditadi <victordperd@gmail.com>
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>
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>
…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>
@ditadi
ditadi removed this pull request from stack #667 November 26, 2026 14:50
@ditadi
ditadi force-pushed the stack/database-dev-flow/01-migrations-on-deploy branch from 0a9a688 to 87a7c72 Compare November 26, 2026 14:51
@ditadi

ditadi commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

Folded into #654. The database development flow stack is now three PRs: #654 (runtime), #666 (appkit db commands) and #675 (template and docs). This PR's change, the app default schema, ships in #654 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