# Migrating from Supabase

Two options: keep your Supabase project as an external database, or switch to Percher's managed PocketBase. For most apps — and anything that leans on Postgres features — Option 1 is the right call.

Option 1, keep Supabase (recommended, no rewrite):
```toml
[data]
mode = "supabase"
[data.supabase]
url = "https://your-project.supabase.co"
anon_key = "eyJ..."
```
Percher injects `SUPABASE_URL`, `SUPABASE_ANON_KEY`, and their `VITE_` variants. Match the names your app reads; Next.js public variables must be set explicitly. Keep Supabase's auth, RLS, storage and functions at Supabase. Check the HTTPS connection method and auth redirect URLs before publishing. Follow the [connection guide](https://percher.app/docs/external-databases#supabase); native Postgres TCP connections are not supported by Percher's HTTPS proxy.

Option 2, switch to PocketBase. Percher generates most of the migration for you:
```sh
bunx percher migrate-from-supabase --project <ref> --token sbp_...
```
This writes a `./migration-preview/` folder (nothing touches your data yet):
- `pb_schema.json` — your tables mapped to PocketBase collections
- `pb_migrate.js` — a runnable data-import script that copies your rows over
- `MIGRATION_NOTES.md` — every RLS policy, flagged column, and manual step

Add `--rewrite-client` to also convert your Supabase SDK calls to the PocketBase SDK across the project (preview first; `--apply` writes in place and backs up the originals). It rewrites the high-confidence auth + CRUD calls, scaffolds a PocketBase client (`src/lib/pocketbase.ts`) and inserts `import { pb }` into the rewritten files, and writes `migration-preview/REWRITE_NOTES.md` — a checklist of what's left plus concrete PocketBase recipes for the auth calls it deliberately didn't fake (`getSession`, OTP, password reset, …). Anything with no clean equivalent (storage, rpc, realtime, edge functions) is left untouched and flagged, never stubbed.

Hybrid is fine and expected. While any Supabase call remains, the migration keeps **both** SDKs and both env sets — that's correct, not a missed step. Dropping `@supabase/supabase-js` only happens once nothing Supabase is left.

Check what's left any time with the local scoreboard (no Supabase token needed):
```sh
bunx percher migrate-from-supabase --status
```
It reports remaining `@supabase` imports, any undefined `pb` references, auth/storage/rpc/realtime/edge counts, and whether the PocketBase dependency + env are wired — each pass/fail/warn, with the project's overall state (broken / hybrid / not-started / complete).

Honest limits — read before committing:
- Auth users can't be moved directly. Supabase password hashes aren't compatible with PocketBase, so every user has to reset their password.
- It's a preview plus a script, not one click. You run the data-import yourself with a Supabase service-role key and review the flagged items.
- Not portable: Edge Functions (use API routes), Postgres views/triggers/functions (rewrite as app logic), complex RLS (PocketBase API rules use a different model — the tool translates the simple `auth.uid() = column` case and flags the rest), realtime, PostGIS, and Supabase Vector/embeddings (use an external service).

If your app depends on those Postgres features, keep Supabase (Option 1). Concept mapping for the parts you rewrite by hand:
- `supabase.from('t').select()` -> `pb.collection('t').getList()`
- `.insert({})` / `.update({})` / `.delete()` -> `.create({})` / `.update(id, {})` / `.delete(id)`
- `supabase.auth.signUp()` -> `pb.collection('users').create({})`
- `signInWithPassword()` -> `pb.collection('users').authWithPassword()`
- `auth.getUser()` -> `pb.authStore.record`
- `storage.upload()` -> `pb.collection('x').create(formData)`
- `channel().subscribe()` -> `pb.collection('x').subscribe('*', fn)`
- Row Level Security -> PocketBase API rules (per collection)
- `.select('*, posts(*)')` -> `pb.getList({ expand: 'posts' })`

Full guide: https://percher.app/docs/migrate-supabase
