# Fly.io

> How to run Reylo Migrate on Fly.io. The release command that migrates your database before each deploy goes live, the connection it needs, and other images.

Reylo Migrate on Fly.io

Run your migrations as Fly's release command:

`fly.toml`:

```toml
[deploy]
  release_command = "npx reylo migrate deploy"
```

It runs once per deploy, on a temporary Machine with the new image. A migration that fails stops the deploy.

## Database

Attaching a Managed Postgres cluster sets `DATABASE_URL` on your app:

```terminal
$ fly mpg attach <cluster-id>
```

Fly recommends the cluster's direct connection for migrations. Set it as a secret of its own:

```terminal
$ fly secrets set DIRECT_DATABASE_URL="postgresql://…@direct.<cluster-id>.flympg.net/…"
```

Then point [`direct_url`](https://reylo.dev/configuration#direct_url) at it:

`.reylo.yaml`:

```yaml
direct_url: ${DIRECT_DATABASE_URL:-${DATABASE_URL}}
```

## Builds

A build that type-checks reads your schema before the release command migrates it. Give it [a database of its own](https://reylo.dev/deployment#build-before-migrate).

Fly sets your secrets only when your app runs, not while it builds. So pass that database's URL as a [build secret](https://fly.io/docs/apps/build-secrets/).

## Other images

A Next.js standalone build, or any image without your `node_modules`, has no `reylo` command. Copy it from Reylo's image, with your `reylo` directory and `.reylo.yaml`:

**Dockerfile**:

```dockerfile
COPY --from=ghcr.io/reylosql/reylo:0.8.3 /usr/local/bin/reylo /usr/local/bin/reylo
COPY reylo/ reylo/
COPY .reylo.yaml ./
```

Then run it directly:

`fly.toml`:

```toml
[deploy]
  release_command = "reylo migrate deploy"
```

The release command replaces your image's `CMD`, and its `ENTRYPOINT` still runs. So start your app from `CMD`.
