Skip to content

Commit 64dc654

Browse files
committed
WEB-86 Setup supabase locally, add docs for setting up supabase, add example .env
1 parent 3209483 commit 64dc654

6 files changed

Lines changed: 208 additions & 134 deletions

File tree

.env.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
DATABASE_URL="postgresql://postgres:postgres@127.0.0.1:54322/postgres?schema=public"
2+
SUPABASE_URL="http://localhost:54321"
3+
SUPABASE_SERVICE_ROLE_KEY="sb_secret_XXXXXXXXX"

DATABASE_SETUP.md

Lines changed: 0 additions & 134 deletions
This file was deleted.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Migrating to Supabase-managed Postgres
2+
3+
Migrating to Supabase-managed Postgres is not necessary, but it can simplify your development workflow.
4+
5+
## A: Fresh start (recommended)
6+
7+
Use this if your local data is expendable. This will still seed from `database.json` at the project root.
8+
9+
```bash
10+
npx supabase start
11+
```
12+
13+
Set `DATABASE_URL` in `.env`:
14+
15+
```env
16+
DATABASE_URL="postgresql://postgres:postgres@127.0.0.1:54322/postgres?schema=public"
17+
```
18+
19+
```bash
20+
npx prisma migrate dev
21+
```
22+
23+
Stop your old Postgres instance.
24+
25+
## B: Migrate existing data
26+
27+
```bash
28+
npx supabase start
29+
```
30+
31+
Set `DATABASE_URL` in `.env`:
32+
33+
```env
34+
DATABASE_URL="postgresql://postgres:postgres@127.0.0.1:54322/postgres?schema=public"
35+
```
36+
37+
Dump data from your current DB:
38+
39+
```bash
40+
pg_dump -h localhost -p 5432 -U sga_puck_app -d sga_db --schema=public --data-only > dump.sql
41+
```
42+
43+
Restore data to Supabase-managed Postgres, setting `session_replication_role` to `replica` to avoid triggering any constraints or triggers during the restore:
44+
```bash
45+
psql -h 127.0.0.1 -p 54322 -U postgres -d postgres <<'EOF'
46+
SET session_replication_role = 'replica';
47+
\i dump.sql
48+
EOF
49+
```
50+
51+
Verify data and stop old Postgres:
52+
53+
```bash
54+
npx prisma studio
55+
```

docs/SUPABASE_SETUP.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# Supabase Setup
2+
3+
We use a local Postgres instance **or** Supabase as the local database (managed via Prisma) and Supabase for storage (media uploads). In production, we deploy to Supabase Postgres.
4+
5+
## Prerequisites
6+
7+
- Docker running locally
8+
- Docker desktop is easiest to setup and run: https://www.docker.com/products/docker-desktop/
9+
- Features a nice GUI, works on all platforms
10+
- For WSL users: install on Windows and enable WSL integration in Docker desktop settings
11+
- Alternatively, you can install any Docker engine: https://docs.docker.com/engine/install/
12+
13+
## Local Development
14+
15+
### 1. Start Supabase
16+
17+
```bash
18+
npx supabase start
19+
```
20+
21+
If Docker is running, Supabase will begin creating a local instance of the necessary services. On success, it should output something like:
22+
23+
```
24+
╭─────────────────────────────────────────────────╮
25+
│ 🌐 APIs │
26+
├─────────────┬───────────────────────────────────┤
27+
│ Project URL │ http://127.0.0.1:54321 │
28+
│ REST │ http://127.0.0.1:54321/rest/v1 │
29+
│ GraphQL │ http://127.0.0.1:54321/graphql/v1 │
30+
╰─────────────┴───────────────────────────────────╯
31+
32+
╭───────────────────────────────────────────────────────────────╮
33+
│ ⛁ Database │
34+
├─────┬─────────────────────────────────────────────────────────┤
35+
│ URL │ postgresql://postgres:postgres@127.0.0.1:54322/postgres │
36+
╰─────┴─────────────────────────────────────────────────────────╯
37+
38+
╭──────────────────────────────────────────────────────────────╮
39+
│ 🔑 Authentication Keys │
40+
├─────────────┬────────────────────────────────────────────────┤
41+
│ Publishable │ sb_publishable_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX │
42+
│ Secret │ sb_secret_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX │
43+
╰─────────────┴────────────────────────────────────────────────╯
44+
```
45+
46+
These credentials are also accessible again via `npx supabase status` if you need to reference them later.
47+
48+
### 2. Configure environment variables
49+
50+
In your `.env` copy the "Project URL" to `SUPABASE_URL`, and the "Secret" to `SUPABASE_SERVICE_ROLE_KEY`.
51+
52+
For `DATABASE_URL`, you may use a locally managed instance, or use the Database "URL". Append `?schema=public` to ensure Prisma uses the public schema.
53+
54+
```env
55+
DATABASE_URL="postgresql://postgres:postgres@127.0.0.1:54322/postgres?schema=public"
56+
SUPABASE_URL="http://127.0.0.1:54321"
57+
SUPABASE_SERVICE_ROLE_KEY="sb_secret_XXXXXXXXX"
58+
```
59+
60+
### 3. Run Prisma migrations
61+
62+
```bash
63+
npx prisma migrate dev
64+
```
65+
66+
This will apply the Prisma schema to your local database. You can also use `npx prisma studio` to view and manage your database records in a nice UI.
67+
68+
## Resetting the database
69+
If you want to reset your database (e.g. to clear all data), you can run:
70+
71+
```bash
72+
npx prisma migrate reset
73+
```
74+
75+
## Stopping Supabase
76+
77+
```bash
78+
npx supabase stop
79+
```
80+
81+
## Configuration
82+
83+
The config lives in `supabase/config.toml`.
84+
85+
- DB: Enabled — used by both application and storage
86+
- Storage: Enabled with a public `media` bucket
87+
- Auth/API: Enabled because storage depends on them
88+
- Everything else: Disabled
89+

supabase/.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Supabase
2+
.branches
3+
.temp
4+
5+
# dotenvx
6+
.env.keys
7+
.env.local
8+
.env.*.local

supabase/config.toml

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# For detailed configuration reference documentation, visit:
2+
# https://supabase.com/docs/guides/local-development/cli/config
3+
project_id = "website-development"
4+
5+
# We use Supabase for storage and as our Postgres database (via Prisma).
6+
# Services not needed locally are disabled.
7+
8+
[api]
9+
enabled = true
10+
port = 54321
11+
12+
[db]
13+
port = 54322
14+
major_version = 17
15+
16+
[db.pooler]
17+
enabled = false
18+
19+
[db.migrations]
20+
enabled = false
21+
22+
[db.seed]
23+
enabled = false
24+
25+
[realtime]
26+
enabled = false
27+
28+
[studio]
29+
enabled = false
30+
31+
[inbucket]
32+
enabled = false
33+
34+
[storage]
35+
enabled = true
36+
file_size_limit = "50MiB"
37+
38+
[storage.buckets.media]
39+
public = true
40+
file_size_limit = "50MiB"
41+
42+
[storage.s3_protocol]
43+
enabled = false
44+
45+
[auth]
46+
enabled = true
47+
site_url = "http://127.0.0.1:3000"
48+
49+
[edge_runtime]
50+
enabled = false
51+
52+
[analytics]
53+
enabled = false

0 commit comments

Comments
 (0)