(
migration: Migration,
method: 'up' | 'down',
afterRun?: (tx?: Transaction) => Promise<void>,
)
| 19 | } |
| 20 | |
| 21 | async run( |
| 22 | migration: Migration, |
| 23 | method: 'up' | 'down', |
| 24 | afterRun?: (tx?: Transaction) => Promise<void>, |
| 25 | ): Promise<void> { |
| 26 | migration.reset(); |
| 27 | |
| 28 | if (!this.options.transactional || !migration.isTransactional()) { |
| 29 | // Without a pinned transaction the set/reset statements may land on different pooled |
| 30 | // connections than the DDL, so refuse rather than silently running in the default schema. |
| 31 | if (this.#runSchema) { |
| 32 | throw new Error( |
| 33 | 'Runtime schema (migrations.schema / migrator.up({ schema })) is only supported with transactional migrations', |
| 34 | ); |
| 35 | } |
| 36 | |
| 37 | const queries = await this.getQueries(migration, method); |
| 38 | await Utils.runSerial(queries, sql => this.driver.execute(sql)); |
| 39 | await afterRun?.(); |
| 40 | } else { |
| 41 | await this.#connection.transactional( |
| 42 | async tx => { |
| 43 | migration.setTransactionContext(tx); |
| 44 | |
| 45 | try { |
| 46 | const queries = await this.getQueries(migration, method); |
| 47 | await Utils.runSerial(queries, sql => this.driver.execute(sql, undefined, 'all', tx)); |
| 48 | await afterRun?.(tx); |
| 49 | } finally { |
| 50 | await this.resetSessionSchema(tx); |
| 51 | } |
| 52 | }, |
| 53 | { ctx: this.#masterTransaction }, |
| 54 | ); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | private async resetSessionSchema(ctx?: Transaction): Promise<void> { |
| 59 | if (!this.#runSchema) { |
nothing calls this directly
no test coverage detected