[**Documentation**](../../../../README)

***

[Documentation](../../../../README) / [@zerotal/orm](../../README) / [index](../README) / MigrationRunner

# Class: MigrationRunner

Defined in: [packages/orm/src/schema/MigrationRunner.ts:57](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/orm/src/schema/MigrationRunner.ts#L57)

Executes and tracks migrations against a `Bun.sql` connection.

Applied migrations are recorded in a tracking table (default `"migrations"`) by
name and batch, so re-runs skip already-applied entries and rollbacks can undo a
whole batch. Each `up()` runs inside its own transaction; a failure rolls that
migration back and surfaces as a [MigrationError](MigrationError) without affecting
already-committed migrations. Every run/rollback emits a `MigrationRan`
framework event (success or failure) for observability.

## Example

```ts
const runner = new MigrationRunner({ connection: sql });
await runner.runFromDirectory('./database/migrations'); // apply pending
await runner.rollbackFromDirectory('./database/migrations'); // undo last batch
```

## Constructors

### Constructor

> **new MigrationRunner**(`options`): `MigrationRunner`

Defined in: [packages/orm/src/schema/MigrationRunner.ts:65](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/orm/src/schema/MigrationRunner.ts#L65)

#### Parameters

##### options

###### connection

[`SQLInstance`](../interfaces/SQLInstance)

The `Bun.sql` connection to run DDL/DML against.

###### table?

`string`

Tracking-table name; defaults to `"migrations"`.

#### Returns

`MigrationRunner`

## Methods

### run()

> **run**(`entries`): `Promise`\<`string`[]\>

Defined in: [packages/orm/src/schema/MigrationRunner.ts:77](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/orm/src/schema/MigrationRunner.ts#L77)

Run all pending migrations from the provided list.

- Skips any entry whose name is already in the migrations table.
- Executes pending entries in the order given; assigns them all to a new batch.
- Returns the names of migrations that were actually executed.

#### Parameters

##### entries

[`MigrationEntry`](../interfaces/MigrationEntry)[]

#### Returns

`Promise`\<`string`[]\>

***

### rollback()

> **rollback**(`entries`): `Promise`\<`string`[]\>

Defined in: [packages/orm/src/schema/MigrationRunner.ts:124](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/orm/src/schema/MigrationRunner.ts#L124)

Roll back the last batch of migrations in reverse order.

`entries` must include all migration objects so the runner can locate
the correct `down()` implementation for each name in the last batch.

Returns the names of migrations that were rolled back.

#### Parameters

##### entries

[`MigrationEntry`](../interfaces/MigrationEntry)[]

#### Returns

`Promise`\<`string`[]\>

***

### pending()

> **pending**(`entries`): `Promise`\<`string`[]\>

Defined in: [packages/orm/src/schema/MigrationRunner.ts:181](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/orm/src/schema/MigrationRunner.ts#L181)

Return names of migrations from `entries` that have not yet been run.

#### Parameters

##### entries

[`MigrationEntry`](../interfaces/MigrationEntry)[]

#### Returns

`Promise`\<`string`[]\>

***

### reset()

> **reset**(`entries`): `Promise`\<`void`\>

Defined in: [packages/orm/src/schema/MigrationRunner.ts:190](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/orm/src/schema/MigrationRunner.ts#L190)

Roll back every batch, running `down()` from newest to oldest.

#### Parameters

##### entries

[`MigrationEntry`](../interfaces/MigrationEntry)[]

#### Returns

`Promise`\<`void`\>

***

### status()

> **status**(`entries`): `Promise`\<[`MigrationStatus`](../interfaces/MigrationStatus)[]\>

Defined in: [packages/orm/src/schema/MigrationRunner.ts:202](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/orm/src/schema/MigrationRunner.ts#L202)

Report which migrations from `entries` have run.

#### Parameters

##### entries

[`MigrationEntry`](../interfaces/MigrationEntry)[]

#### Returns

`Promise`\<[`MigrationStatus`](../interfaces/MigrationStatus)[]\>

***

### runFromDirectory()

> **runFromDirectory**(`dir`): `Promise`\<`string`[]\>

Defined in: [packages/orm/src/schema/MigrationRunner.ts:226](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/orm/src/schema/MigrationRunner.ts#L226)

Load migration files from a directory, sort alphabetically, and run.

Files must export a default class that extends Migration.
Skips non-.ts files.

#### Parameters

##### dir

`string`

#### Returns

`Promise`\<`string`[]\>

***

### rollbackFromDirectory()

> **rollbackFromDirectory**(`dir`): `Promise`\<`string`[]\>

Defined in: [packages/orm/src/schema/MigrationRunner.ts:235](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/orm/src/schema/MigrationRunner.ts#L235)

Load migration files from a directory and roll back the last batch.

#### Parameters

##### dir

`string`

#### Returns

`Promise`\<`string`[]\>

Names of the migrations that were rolled back.
