|
| 1 | +# Local Development Environment Setup |
| 2 | + |
| 3 | +This document explains how to set up and manage the local development environment for the COC-API project. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +- **Docker & Docker Compose**: Ensure you have Docker installed and running. |
| 8 | +- **PostgreSQL Client (`pg_dump`)**: Optional. The current local setup prefers loading a local `seed/dump.sql` file. `pg_dump` is only required if you want to create a fresh dump from a remote database manually. |
| 9 | + |
| 10 | +## Setup Instructions |
| 11 | + |
| 12 | +### 1. Configure Environment Variables |
| 13 | + |
| 14 | +Copy the `.env.example` file to `.env` and fill in the required values: |
| 15 | + |
| 16 | +```bash |
| 17 | +cp .env.example .env |
| 18 | +``` |
| 19 | + |
| 20 | +Key variables for the setup script: |
| 21 | +- `SESSION_POOLER`: The direct connection string to your Supabase project (Session Pooler / IPv4). |
| 22 | + - **Format**: `postgresql://postgres.PROJECT_REF:PASSWORD@aws-0-us-east-1.pooler.supabase.com:5432/postgres` |
| 23 | + |
| 24 | +### 2. Run the Setup Script |
| 25 | + |
| 26 | +The `scripts/setup-local.sh` script automates the local environment bring-up. By default it will load the local `seed/dump.sql` into the local Postgres instance. |
| 27 | + |
| 28 | +```bash |
| 29 | +bun run local |
| 30 | +``` |
| 31 | + |
| 32 | +#### What the script does: |
| 33 | +1. **Starts Postgres**: Launches the `db` container. |
| 34 | +2. **Installs Extensions**: Pre-installs `pgcrypto`, `uuid-ossp`, and `pg_stat_statements` into the `public` schema. |
| 35 | +3. **Loads Local Seed**: The script will attempt to load `seed/dump.sql` into the DB only when there are no existing user tables present in the database. If the database already contains tables (non-system tables), the seed step will be skipped to avoid accidentally overwriting existing data. This is the default and recommended local flow. |
| 36 | +4. **Starts the API**: Launches the `api` container and waits for it to be healthy. |
| 37 | + |
| 38 | +Flags: |
| 39 | + - `--skip-dump`: reuse existing `seed/dump.sql` (no remote dump step) |
| 40 | + - `--skip-seed`: skip loading the seed and just start containers |
| 41 | + |
| 42 | +### 3. Useful Commands |
| 43 | + |
| 44 | +- **Start environment**: `bash scripts/setup-local.sh` |
| 45 | +- **Skip dump (reuse existing `seed/dump.sql`)**: `bash scripts/setup-local.sh --skip-dump` |
| 46 | +- **Skip seeding (just start containers)**: `bash scripts/setup-local.sh --skip-seed` |
| 47 | +- **View logs**: `docker compose logs -f` |
| 48 | +- **Stop containers**: `docker compose down` |
| 49 | +- **Wipe local data (force re-seed)**: `docker compose down -v` |
| 50 | + |
| 51 | +## Troubleshooting |
| 52 | + |
| 53 | +### Tables not in `public` schema |
| 54 | +The script automatically patches the dump to ensure tables are placed in the `public` schema. If you manually imported a dump, ensure you've installed the required extensions first. |
0 commit comments