refactor(db): embed migrations + tars via Bun macros, drop folder-tar plugin#22
Merged
maxscharwath merged 2 commits intomainfrom May 6, 2026
Merged
refactor(db): embed migrations + tars via Bun macros, drop folder-tar plugin#22maxscharwath merged 2 commits intomainfrom
maxscharwath merged 2 commits intomainfrom
Conversation
… plugin
Fixes the canary docker image crash on startup:
Error: Can't find meta/_journal.json file
Drizzle's `migrate({ migrationsFolder })` reads `_journal.json` and the
`*.sql` files at runtime via `fs.readFileSync`. When `bun build --compile`
produces a standalone binary, `import.meta.dir` resolves to a `$bunfs/...`
virtual path with no migration files on disk, so the migrator throws.
Replace the runtime fs lookup (and the previous `folder-tar` plugin used
for templates/locales) with a single `@brika/db/macros` module imported
with `with { type: 'macro' }`. Bun evaluates the macro at bundle time
(or first import in dev), inlining the resulting `MigrationMeta[]` and
gzipped tar bytes as literals.
Two macros:
- loadMigrations(repoRelativePath): MigrationMeta[]
Drizzle's public `readMigrationFiles(...)` against the absolute path.
- loadTarBytes(repoRelativePath): Promise<number[]>
Walks the folder with Bun.Glob, packs into Bun.Archive (gzip).
Why drop the folder-tar plugin: the runtime preload that registered it
(via `Bun.plugin()`) silently broke macro substitution in transitively
imported files. Without the preload, macros work in dev, test, and
compile mode uniformly. The two `*.tar` import sites (templates init
and i18n) move to `loadTarBytes` calls; the plugin and its preload
become unnecessary.
A small `applyMigrations` in `@brika/db` replicates drizzle's sqlite
migrate logic against `bun:sqlite` directly (CREATE `__drizzle_migrations`,
gate by `folderMillis`, transactional apply), so we don't depend on the
protected `db.dialect.migrate` internal API.
DX win: add a migration with `drizzle-kit generate`, restart, done.
No codegen step, no committed JSON, no plugin to register.
- packages/db/src/macros.ts: the macros, paths resolved from repo root
- packages/db/src/database.ts: defineDatabase signature + applyMigrations
- 5 database.ts callers (auth, http/cache, hub state/sparks/logs)
- apps/hub/src/runtime/config/brika-initializer.ts: templates via macro
- apps/hub/src/runtime/i18n/i18n-service.ts: locales via macro
- delete folder-tar plugin, pack-folder, preload, bunfig.toml
- compile.ts + bundle.ts: drop the plugins arg
b2be2fc to
66592b9
Compare
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Summary
Fixes the canary docker image crash:
Drizzle's
migrate({ migrationsFolder })reads_journal.jsonand*.sqlfiles viafs.readFileSyncat runtime. Inbun build --compile,import.meta.dirresolves to\$bunfs/...with no migration files on disk → throw.Approach
A single
@brika/db/macrosmodule, imported withwith { type: 'macro' }. Bun evaluates the macro at bundle time (or first import in dev), inlining the result as a literal — zero fs at startup, zero codegen step, zero committed artifacts.Two macros:
loadMigrations(repoRelativePath): MigrationMeta[]— wraps drizzle's publicreadMigrationFiles(...).loadTarBytes(repoRelativePath): Promise<number[]>— walks the folder withBun.Glob, packs withBun.Archive(gzip).A small
applyMigrationsin@brika/dbreplicates drizzle's sqlite migrate logic directly (CREATE__drizzle_migrations, gate byfolderMillis, transactional apply), so we don't depend on the protecteddb.dialect.migrateinternal API.Why drop the folder-tar plugin
The previous runtime preload (which registered
folderTarPluginviaBun.plugin()) silently broke macro substitution in transitively imported files — a Bun bug. Removing the preload makes macros work uniformly in dev, test, and compile mode.The two
*.tarimport sites — templates init and i18n — switch toloadTarBytescalls. The plugin and its preload become unnecessary.DX
Add a migration with
drizzle-kit generate, restart, done. No codegen step, no committed JSON, no plugin to register.Files
packages/db/src/macros.ts— the two macros (paths from repo root)packages/db/src/database.ts—defineDatabasesignature +applyMigrationsapps/hub/src/runtime/config/brika-initializer.ts— templates via macroapps/hub/src/runtime/i18n/i18n-service.ts— locales via macrocompile.ts+bundle.ts— dropplugins: [folderTarPlugin()]Net diff: +102 / −138 across 18 files.
Test plan
bun --filter @brika/db --filter @brika/auth --filter @brika/http --filter @brika/hub typecheck— cleanbun --filter @brika/db test— 17/17bun --filter @brika/auth test— 288/288 (includes a re-migration test that exercises the gate)bun --filter @brika/http test— 272/272bun --filter @brika/hub test— 2176/2176 (includes `cli-start.test.ts` which previously broke under macro+preload)