# Database Migrations

Standard workflow for schema updates.

# File Location

/database/migrations/YYYYMMDD_description.sql

# Workflow

## 1. Create

Create .sql file in migrations folder.

## 2. Apply Local

docker exec -i <container> psql -U <user> -d <db> < database/migrations/file.sql

## 3. Open Production Tunnel

**Managed Postgres (newer):**

```bash
flyctl mpg proxy 16380:5432 -a <db-app-name>
```

**Unmanaged Postgres (legacy):**

```bash
fly proxy 5433:5432 -a <db-app-name>
```

Keep tunnel terminal open during migration.

## 4. Apply Production

In a new terminal:

```bash
PGPASSWORD='<password>' psql -h localhost -p <port> -U <user> -d <db> < database/migrations/file.sql
```

## 5. Verify

- Run check query.
- Close tunnel (Ctrl+C).

# Rules

- Never edit committed migrations.
- Always Local before Production.
- Use separate terminal for tunnel (keep open during migration).
