# Projects and migrations

## Projects

A **project** is a directory with a `mitch.toml` at its root. `mitch init <project-id>` creates one:

```toml
[project]
id = "org.example.shop"
```

The **project ID** is globally unique and never changes once deployed, because deployments record applied migrations under it. Choose it like a package name (`de.example.user-management`) or use a UUID. It must not contain whitespace.

Commands work on the project in the current directory. Pass `--project`/`-p <dir>` to use another one.

## Migration files

Mitch finds migrations anywhere below the project root, in any subdirectory. A migration consists of up to three files with the same stem:

| File | Required | Content |
| --- | --- | --- |
| `<stem>.up.sql` | yes | SQL that applies the migration. |
| `<stem>.down.sql` | no | SQL that reverts it. A migration without one cannot be reverted. |
| `<stem>.config.toml` | no | Metadata, see below. |

`mitch create <id>` writes all three. With SQL on stdin, a `-- up.sql` and a `-- down.sql` comment line split the input between both scripts. `--dependency`/`-d` (repeatable) records dependencies, `--skip-config` and `--skip-downscript` leave out the respective file. An ID with slashes, such as `users/create-table`, creates subdirectories.

## Migration configuration

All fields of `<stem>.config.toml` are optional:

```toml
id = "create-orders"
created_at = 2026-02-03T12:34:00Z
dependencies = ["create-customers", "users:create-users"]
after = ["seed-data"]
rebuild_dependants_of = ["order-summary"]
transactional = true
idempotent = false
```

| Field | Default | Meaning |
| --- | --- | --- |
| `id` | the file stem | The migration ID, unique within the project. |
| `created_at` | none | ISO-8601 date or datetime. Orders independent migrations before falling back to the ID. |
| `dependencies` | `[]` | Migrations that must be applied first. Applying this migration applies them too; reverting one of them reverts this one first. |
| `after` | `[]` | Ordering constraints: run after these when they are applied or part of the same run, without requiring them. See [Changing applied migrations](changing-applied-migrations.md#ordering-without-requiring). |
| `rebuild_dependants_of` | `[]` | Revert the applied dependants of these migrations before this one and re-apply them afterwards. See [Rebuilds](changing-applied-migrations.md#rebuilding-dependent-objects). |
| `transactional` | `true` | `false` runs the migration alone in autocommit mode, for statements that refuse to run in a transaction, such as `CREATE INDEX CONCURRENTLY`. A failure cannot be rolled back then. |
| `idempotent` | `false` | The up script can run again on top of itself (`CREATE OR REPLACE VIEW`, for example). `mitch status` then suggests `mitch rework` when it was edited after being applied. |

### Order of migrations

Dependencies and ordering constraints fix the order where it matters. Among independent migrations, mitch orders by `created_at` and then by ID. That tie-break is not a guarantee: if one migration needs another, declare the dependency.

Consecutive transactional migrations run in one transaction. If one fails, the whole batch is rolled back.

## Linked projects

A project can use migrations from other projects. A project nested in the project's directory (a subdirectory with its own `mitch.toml`) is linked automatically under its directory name. Projects elsewhere, for example in a sibling checkout, are declared in `mitch.toml`:

```toml
[project]
id = "org.example.app"

[projects]
users = "../shared/user-management"
```

The key is a local **alias**, the value a path relative to the project root. A migration depends on a linked migration by its qualified ID `alias:migration`:

```toml
dependencies = ["create-groups", "users:create-users"]
```

- `mitch up` applies the project's own migrations plus the linked migrations they require, nothing else.
- `mitch down` and `mitch drop` also revert linked migrations that nothing requires any more. `drop` accepts qualified IDs.
- A revert is refused while an applied migration of a project that is not loaded depends on it.
- `mitch status --all` also lists migrations of directly linked projects that are not required.
- The same project may be linked along several paths. It is still one project; its copies must agree on content and dependencies.

Mitch does not fetch linked projects. Use git submodules, Mercurial subrepositories or a similar mechanism of your version-control system.

## Bookkeeping

Mitch records deployments in its own tables, created on first use:

- On engines with standard schema support (PostgreSQL, SQL Server, DB2), the tables live in a schema named `mitch`.
- On SQLite and Oracle, they are prefixed with `mitch_` in the default schema.

The tables record the projects deployed to the database, the applied migrations with their hashes, the dependency edges they were applied with, which migrations were pulled in from linked projects, and an audit trail. One database can hold several projects.
