> ## Documentation Index
> Fetch the complete documentation index at: https://docs.sqd.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# TypeORM migration generation

> Generate, create, apply, and revert TypeORM migrations for a Squid SDK PostgreSQL schema.

The [`typeorm-migration`](https://github.com/subsquid/squid-sdk/tree/master/typeorm/typeorm-migration) tool is used to generate, apply and revert database migrations. It follows the conventions below.

* The migrations are generated in the `db/migrations` folder.
* The database connection is inferred from the `DB_XXX` environment variables, including [`DB_SCHEMA`](./data-stores/typeorm-store#db_schema). When `DB_SCHEMA` is set, `squid-typeorm-migration apply` issues a `CREATE SCHEMA IF NOT EXISTS` before running the migrations, and the migrations ledger lands in that schema too.
* All entities should be exported from `lib/model` commonjs module, i.e. the entity classes must be compiled from TypeScript.

Here are some useful commands:

```bash theme={"system"}
npx squid-typeorm-migration apply # apply pending migrations
```

```bash theme={"system"}
npx squid-typeorm-migration generate # generate the migration for the schema defined in schema.graphql
```

```bash theme={"system"}
npx squid-typeorm-migration generate -n AddTransferIndex # choose the migration name suffix
```

```bash theme={"system"}
npx squid-typeorm-migration create # create an empty migration with the default Data suffix
```

```bash theme={"system"}
rm -r db/migrations # clean the db/migrations folder
```

In `@subsquid/typeorm-migration@1.4.0`, `generate -n <name>` accepts a custom suffix. The `create` command does not accept a name value: passing its `--name` flag creates a `true` suffix instead. Run `create` without arguments.
