---
title: "generateD1Migrations"
description: "Writes one `<id>_<name>.sql` per migration into `options.to`, then deletes generated files with no migration behind them, so the output directory is a pure function of the input one."
url: "https://pitlane.tools/package/data-table-d1/migrations/function/generateD1Migrations"
---

# generateD1Migrations

```ts
import { generateD1Migrations } from "@pitlane/data-table-d1/migrations";
```

```ts
function generateD1Migrations(options): Promise<GeneratedD1Migration[]>;
```

Writes one `<id>_<name>.sql` per migration into `options.to`, then deletes generated files with no migration behind them, so the output directory is a pure function of the input one.

The SQL is copied verbatim. Splitting it into statements is the job of whatever executes the file, and a splitter naive enough to write here would corrupt any migration containing a semicolon inside a string literal or a `begin ... end` trigger body.

## Parameters

### options

[`GenerateD1MigrationsOptions`](https://pitlane.tools/package/data-table-d1/migrations/interface/GenerateD1MigrationsOptions.md)

Where to read migrations from, and where to write SQL to.

## Returns

`Promise`\<[`GeneratedD1Migration`](https://pitlane.tools/package/data-table-d1/migrations/interface/GeneratedD1Migration.md)\[\]>

The generated files, ordered by migration id.

## Throws

If `options.from` holds no migrations, or one of them has an empty `up`. Both mean the output directory is about to be wrong, and a migration runner is the worst place to discover that.

## Example

```ts
import { generateD1Migrations } from "@pitlane/data-table-d1/migrations";

await generateD1Migrations({ to: "db/d1-migrations" });
```

## See

[Cloudflare D1 guide: Migrations](https://pitlane.tools/guides/cloudflare-d1.md#migrations)
