also looking at this
fix(migrate)!: run the directories executeMigration was given
#1138
7 files
+348
-49
| @@ -22,17 +22,39 @@ The migration system automatically generates SQL DDL statements from your model | ||
| 22 | 22 | ```ts |
| 23 | 23 | import { generateMigration, executeMigration } from 'bun-query-builder' |
| 24 | 24 | |
| 25 | // Generate migration from models directory | |
| 26 | const migration = await generateMigration('./models', { | |
| 25 | // Generate migration files from the models directory | |
| 26 | await generateMigration('./models', { | |
| 27 | 27 | dialect: 'postgres', |
| 28 | apply: true, | |
| 29 | 28 | full: true |
| 30 | 29 | }) |
| 31 | 30 | |
| 32 | // Execute the migration | |
| 33 | await executeMigration(migration) | |
| 31 | // Run every pending file in the configured `migrationDir` | |
| 32 | await executeMigration() | |
| 34 | 33 | ``` |
| 35 | 34 | |
| 35 | ### Run a Corpus Spread Across Directories | |
| 36 | ||
| 37 | `executeMigration` takes the directory, or the list of directories, to run. | |
| 38 | Omit it and it uses the configured `migrationDir`. | |
| 39 | ||
| 40 | ```ts | |
| 41 | // The application's own migrations, plus the ones each installed package ships | |
| 42 | await executeMigration([ | |
| 43 | './database/migrations', | |
| 44 | './node_modules/loghq/database/migrations', | |
| 45 | ]) | |
| 46 | ``` | |
| 47 | ||
| 48 | Files from every directory are ordered together by file name, not one directory | |
| 49 | at a time — the file name is the ordinal, and a package's tables routinely carry | |
| 50 | foreign keys into the application's. So a package migration can be placed before | |
| 51 | or after an application one deliberately. | |
| 52 | ||
| 53 | Because migrations are recorded in the ledger by their bare file name, a name | |
| 54 | cannot repeat across directories; a repeat is refused rather than recorded once | |
| 55 | and silently skipped. A directory that does not exist is refused too, rather | |
| 56 | than read as an empty corpus. | |
| 57 | ||
| 36 | 58 | ### Migration Options |
| 37 | 59 | |
| 38 | 60 | ```ts |
| @@ -200,12 +222,11 @@ const models = defineModels({ | ||
| 200 | 222 | |
| 201 | 223 | ```ts |
| 202 | 224 | try { |
| 203 | const migration = await generateMigration('./models', { | |
| 204 | dialect: 'postgres', | |
| 205 | apply: true | |
| 225 | await generateMigration('./models', { | |
| 226 | dialect: 'postgres' | |
| 206 | 227 | }) |
| 207 | 228 | |
| 208 | await executeMigration(migration) | |
| 229 | await executeMigration() | |
| 209 | 230 | } catch (err) { |
| 210 | 231 | console.error('Migration failed:', err) |
| 211 | 232 | // Handle rollback or retry logic |
| @@ -270,7 +291,7 @@ beforeAll(async () => { | ||
| 270 | 291 | }) |
| 271 | 292 | |
| 272 | 293 | if (result.sqlStatements.length > 0) { |
| 273 | await executeMigration(result) | |
| 294 | await executeMigration() | |
| 274 | 295 | } |
| 275 | 296 | }) |
| 276 | 297 | ``` |
| @@ -302,13 +323,15 @@ function generateMigration( | ||
| 302 | 323 | |
| 303 | 324 | ```ts |
| 304 | 325 | function executeMigration( |
| 305 | migration: GenerateMigrationResult | |
| 326 | dirs?: string | string[] | |
| 306 | 327 | ): Promise<boolean> |
| 307 | 328 | ``` |
| 308 | 329 | |
| 309 | 330 | **Parameters:** |
| 310 | 331 | |
| 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. | |
| 312 | 335 | |
| 313 | 336 | **Returns:** |
| 314 | 337 | |