ReviewOS

also looking at this

stacks/bun-query-builder

fix(migrate)!: run the directories executeMigration was given

#1138
Merged glennmichael123 wants to merge fix/execute-migration-dirs into main
7 files +348 -49
docs/features/migrations.mdmodified+35-12
Changes to docs/features/migrations.md
@@ -22,17 +22,39 @@ The migration system automatically generates SQL DDL statements from your model
2222```ts
2323import { generateMigration, executeMigration } from 'bun-query-builder'
2424
25// Generate migration from models directory
26const migration = await generateMigration('./models', {
25// Generate migration files from the models directory
26await generateMigration('./models', {
2727 dialect: 'postgres',
28 apply: true,
2928 full: true
3029})
3130
32// Execute the migration
33await executeMigration(migration)
31// Run every pending file in the configured `migrationDir`
32await executeMigration()
3433```
3534
35### Run a Corpus Spread Across Directories
36
37`executeMigration` takes the directory, or the list of directories, to run.
38Omit it and it uses the configured `migrationDir`.
39
40```ts
41// The application's own migrations, plus the ones each installed package ships
42await executeMigration([
43 './database/migrations',
44 './node_modules/loghq/database/migrations',
45])
46```
47
48Files from every directory are ordered together by file name, not one directory
49at a time — the file name is the ordinal, and a package's tables routinely carry
50foreign keys into the application's. So a package migration can be placed before
51or after an application one deliberately.
52
53Because migrations are recorded in the ledger by their bare file name, a name
54cannot repeat across directories; a repeat is refused rather than recorded once
55and silently skipped. A directory that does not exist is refused too, rather
56than read as an empty corpus.
57
3658### Migration Options
3759
3860```ts
@@ -200,12 +222,11 @@ const models = defineModels({
200222
201223```ts
202224try {
203 const migration = await generateMigration('./models', {
204 dialect: 'postgres',
205 apply: true
225 await generateMigration('./models', {
226 dialect: 'postgres'
206227 })
207228
208 await executeMigration(migration)
229 await executeMigration()
209230} catch (err) {
210231 console.error('Migration failed:', err)
211232 // Handle rollback or retry logic
@@ -270,7 +291,7 @@ beforeAll(async () => {
270291 })
271292
272293 if (result.sqlStatements.length > 0) {
273 await executeMigration(result)
294 await executeMigration()
274295 }
275296})
276297```
@@ -302,13 +323,15 @@ function generateMigration(
302323
303324```ts
304325function executeMigration(
305 migration: GenerateMigrationResult
326 dirs?: string | string[]
306327): Promise<boolean>
307328```
308329
309330**Parameters:**
310331
311- `migration`: Result from generateMigration
332- `dirs`: Directory, or list of directories, holding the `.sql` corpus to run.
333 Defaults to the configured `migrationDir`. Relative paths resolve against the
334 workspace root. File names must be unique across every directory in a run.
312335
313336**Returns:**
314337