# Migrations

Every schema change is a SQL file in your `migrations/` folder. The files are the source of truth, and the database records which of them it has applied.

## Create a migration

```bash
fakebase migrate create add_users
```

This creates a file like `migrations/20261106_143000_add_users.sql`. The timestamp keeps migrations in order. Write any SQL PostgreSQL accepts in it:

```sql
create table users (
  id bigint generated always as identity primary key,
  email text not null unique,
  created_at timestamptz not null default now()
);
```

## Check before you apply

```bash
fakebase migrate validate
```

Checks your migration files for mistakes, like bad file names or two files with the same timestamp. With a database running, it also flags risky statements in pending migrations.

```bash
fakebase migrate diff
```

Runs pending migrations in a transaction that is rolled back, then shows the schema changes, anything risky, and errors with line numbers. Nothing is applied.

## Apply migrations

```bash
fakebase migrate up
```

Shows the same preview as `migrate diff`, asks you to confirm, then applies each pending migration in order. Each migration runs in its own transaction, so a failing migration leaves nothing half-applied. Running it again with nothing pending does nothing.

Use `--to` with a migration ID to apply only up to that migration.

## Destructive changes

Migrations that drop or truncate tables, delete rows, or change column types can lose data. `migrate up` refuses to apply them unless you add `--allow-destructive`:

```bash
fakebase migrate up --allow-destructive
```

`--yes` alone is never enough for these, so a script or agent can't lose data by accident.

## See where you are

```bash
fakebase migrate status
```

Lists every migration with its state:

| State      | Meaning                                                              |
| ---------- | -------------------------------------------------------------------- |
| `applied`  | Applied to the database                                              |
| `pending`  | Not applied yet                                                      |
| `modified` | The file changed after it was applied. Later migrations are blocked. |
| `missing`  | Applied, but the file is gone. Later migrations are blocked.         |

Never edit or delete a migration that has been applied. Restore it from git and put the change in a new migration.

```bash
fakebase migrate history
```

Lists applied migrations, when they ran, and how long they took.

To open a single migration in Fakebase Studio, the desktop app, give its ID, its file name, or a unique part of the ID:

```bash
fakebase studio add_users
```

## Drift

Drift is a change in the database that no migration describes, usually SQL someone ran by hand. `migrate status`, `migrate up`, and `fakebase doctor` warn you when they find it.

To keep the change, turn it into a migration:

```bash
fakebase migrate create keep_manual_changes --from-diff
```

This writes the change to a new migration file and records it as applied, because the database already has it.

Don't run migration files with `psql` or another database client. Fakebase can't see that they ran, so they stay pending and show up as drift. Use `fakebase migrate up`.

## Import older migrations

Projects from the older Fakebase format have numbered files like `0001_create_users.sql` and a `meta/journal.json`. Convert them to the current format with:

```bash
fakebase migrate import
```

It converts the files, updates the matching history in the database, and asks you to confirm first.

Add `--help` to any command for its flags.

---

## Next steps

- [Database](https://fakebase.studio/docs/cli-database.md): Set up a project, run its local PostgreSQL database, and connect your app with the Fakebase CLI.
- [Build with Agents](https://fakebase.studio/docs/build-with-agents.md): Set up Cursor, Claude Code, or Codex to change your database for you with the Fakebase CLI.
