Migrating · Platform guides
Coming from Supabase
Translate Supabase RLS policies into schema access policies, migrate auth.users to built-in sign-in and map Storage buckets to typed File records.
- API
- —
- In the schema
ext::files::File
What you’ll move#
This guide migrates a Supabase notes app to Napalm. The existing project stores profiles keyed to auth.users, notes guarded by Postgres RLS policies, shared access in a note_shares join table and attachments in Supabase Storage. Browser clients query those tables directly through supabase-js.
| In Supabase | In Napalm |
|---|---|
| RLS policies on tables | Access rules declared on each object type |
auth.users and auth.uid() | Built-in project sign-in and global caller |
service_role keys that bypass RLS | Service keys scoped by schema access rules |
Storage buckets and storage.objects policies | Typed file records governed by schema access rules |
supabase.from(…).select(…) in the browser | Deployed EdgeQL queries called with typed arguments |
| Realtime Postgres Changes | Live subscriptions on any deployed read query |
SQL functions called via rpc() | Deployed EdgeQL queries |
| Edge Functions | Actions and background jobs |
pg_cron jobs | Declarative schedules |
Before you start#
Create a project in the console and sign in with the napalm CLI. Column types map according to the Postgres translation table.
# Dump public tables and RLS policies to a local filesupabase db dump --schema public -f old-schema.sql # Draft an initial schema into dbschema/default.gelnapalm schema draft --from old-schema.sql # Create an isolated branch and push the schemanapalm branches create notes rehearsalnapalm schema push notes --branch rehearsalThe generated draft creates an object type for every table in public and a closed using (false) scaffold for every RLS policy with the original SQL preserved in a comment above each rule. Translating a Postgres schema details how napalm schema draft processes the dump.
Convert RLS policies into access rules#
In the Supabase app, three RLS policies guard notes and note_shares. Because the third policy queries notes while the notes policy queries note_shares, Postgres aborts with infinite recursion detected in policy for relation "notes". Teams usually work around that error by wrapping the lookup in a security definer function that bypasses RLS.
alter table public.notes enable row level security;alter table public.note_shares enable row level security; create policy "Authors manage their notes" on public.notes for all to authenticated using ((select auth.uid()) = author_id); create policy "People a note is shared with read it" on public.notes for select to authenticated using (exists ( select 1 from public.note_shares s where s.note_id = notes.id and s.person_id = (select auth.uid()) )); -- Reads notes, whose policy reads note_shares: "infinite recursion detected in policy for relation notes".create policy "Authors manage a note's shares" on public.note_shares for all to authenticated using (exists ( select 1 from public.notes n where n.id = note_shares.note_id and n.author_id = (select auth.uid()) ));In Napalm, the join table collapses into a shared_with multi link, and both access rules live directly on Note without helper functions or privilege bypasses.
Recursion-free link rules
Access rules traverse object links directly without policy recursion or helper functions.
Scoped service keys
Backend service keys follow schema policies instead of bypassing row security.
The Note type below declares ownership and collaborator read access in one place:
type Note { required owner: Person; multi shared_with: Person; required title: str; required body: str; required created: datetime { default := datetime_current(); }; old_id: str { constraint exclusive; }; access policy owner_has_full_access allow all using (.owner.identity ?= global caller); access policy shared_with_can_read allow select using ((global caller in .shared_with.identity) ?? false); access policy the_move_can_write allow all using (global role ?= 'service');}Moving from Postgres RLS to Napalm access rules changes four behaviors:
- No policy recursion
- An access rule traverses linked types without re-evaluating target policies recursively, so mutually linked types never deadlock or fail.
- Closed by default
- In Supabase, forgetting
enable row level securityleaves a table readable and writable with theanonkey. In Napalm, a type without an access rule is closed to every caller. - No superuser bypass
- Napalm has no
service_rolekey orsecurity definerescape hatch. Backend workers authenticate with service keys governed by schema rules, andnapalm keys reachreports exact reachable row counts. - Translating RLS roles
(select auth.uid())becomesglobal caller,to authenticatedbecomesexists global caller, andto anonbecomes an unconditionalallow selectrule.
Migrate Supabase Auth users#
Each profiles row linked to auth.users.id becomes an unclaimed Person record until that user signs in to Napalm. The export script below reads each user’s email address alongside their linked Google and GitHub provider IDs from auth.identities.
- Google and GitHub
- Returning users sign in with the same Google or GitHub account and immediately reclaim their notes without a verification email.
- Email and password
- Supabase password hashes stay behind. Password users sign in with an emailed verification code or set a verified new password.
- Magic links
- Napalm sends one-time email codes and magic links natively, and your app chooses whether unrecognized addresses create new accounts.
- Phone and anonymous users
- Napalm does not support SMS or anonymous sign-in, so the export script skips accounts that lack both an email address and an OAuth identity.
- Signup triggers on auth.users
- Drop the
on_auth_user_createdtrigger that copied rows intopublic.profiles. Your app creates aPersonrecord the first time a new user signs in.
Moving your users provides the Person schema, the claimPerson query and the CLI commands for enabling each sign-in provider.
Import existing rows#
Connect the export script (scripts/from-supabase.ts) using your Supabase project’s direct Postgres connection string so the query can join public.profiles with auth.users and auth.identities. The script streams rows into the batch sender from Switching over.
// scripts/from-supabase.tsimport pg from 'pg'import { move } from './send' // The database's own connection string, from the Supabase dashboard: it reads auth.users, which the API doesn't.const old = new pg.Client({ connectionString: process.env.OLD_DATABASE_URL }) /** A table's rows in the order of their ids, a thousand at a time, so nothing large is held or locked. */async function* pages(sql: string) { let after = '' for (;;) { const { rows } = await old.query(sql, [after]) if (rows.length === 0) return yield* rows after = rows[rows.length - 1].id }} await old.connect()// People first: a note names its owner. Each comes with the Google and GitHub accounts Supabase knew them by.await move( 'importPeople', pages(` select u.id::text, u.email, coalesce(p.name, u.email) as name, coalesce(array_agg(case i.provider when 'google' then 'https://accounts.google.com ' || i.provider_id when 'github' then 'https://github.com ' || i.provider_id end) filter (where i.provider in ('google', 'github')), '{}') as sign_ins from auth.users u left join public.profiles p on p.id = u.id left join auth.identities i on i.user_id = u.id where u.id::text > $1 and u.email is not null group by u.id, p.name order by u.id::text limit 1000`),)await move( 'importNotes', pages(` select n.id::text, n.author_id::text as owner_id, n.title, n.body, n.created_at, coalesce(array_agg(s.person_id::text) filter (where s.person_id is not null), '{}') as shared_with from public.notes n left join public.note_shares s on s.note_id = n.id where n.id::text > $1 group by n.id order by n.id::text limit 1000`),)// Files' records last: each names its object by the path it has in the bucket.await move( 'importAttachments', pages(` select id::text, note_id::text, path as key, name from public.attachments where id::text > $1 order by id::text limit 1000`),)await old.end()npm install pg tsxOLD_DATABASE_URL=postgres://postgres:…@db.<project-ref>.supabase.co:5432/postgres \NAPALM_URL=https://<project-address>/branches/rehearsal NAPALM_KEY=ndb_… \ npx tsx scripts/from-supabase.tsConvert Storage buckets into file types#
Supabase Storage policies often parse slash-delimited object keys to infer ownership. In Napalm, each file is a typed record linked to its Note, so the note’s own access rules govern uploads, downloads and deletions.
Record-governed file storage
Napalm copies bucket objects into file records governed by their parent note.
Declare an Attachment type extending ext::files::File with a required note link and an old_key property for the legacy Storage path:
# A file on a note. Whoever may read the note may have the file.type Attachment extending ext::files::File { required note: Note; # The object's key in the old store, which the import copies it by. old_key: str { constraint exclusive; }; access policy note_owner_has_full_access allow all using (.note.owner.identity ?= global caller); access policy shared_with_can_read allow select using ((global caller in .note.shared_with.identity) ?? false); access policy the_move_can_write allow all using (global role ?= 'service');}The export script’s final pass inserts an Attachment record storing each object’s key in old_key. Because Supabase Storage exposes an S3-compatible endpoint, napalm files store import copies the bytes directly.
# One batch of files' records, each on its note. The bytes come after: Napalm copies them by the key a row names.for row in json_array_unpack(<json>$rows)union ( insert Attachment { old_key := <str>row['key'], note := assert_exists((select Note filter .old_id = <str>row['note_id'])), file_name := <str>row['name'], } unless conflict on .old_key)# Create an S3 access key in Supabase Storage settings, then preview counts and sample objectsexport NAPALM_SOURCE_SECRET_ACCESS_KEY=…napalm files store import notes --endpoint https://<project-ref>.supabase.co/storage/v1/s3 \ --region <project-region> --path-style --access-key-id <key-id> \ --map type=default::Attachment,key=old_key,bucket=attachments --plan- Running the copy
- Re-run the command without
--planto start copying bytes.napalm files store ls notesreports imported, missing and rejected file counts. - Format and size checks
- Napalm measures each object’s byte length, detects its MIME format from the header bytes and enforces any
file_formatconstraints on your type. - Signed download URLs
- Selecting
file_urlin a query returns a short-lived signed URL for every attachment the caller is allowed to read. Files & uploads covers direct client uploads.
Migrate Realtime, functions and cron#
- Postgres Changes
- Raw table-change channels emit single-table row deltas and force clients to stitch joins back together. A live query streams complete, rule-filtered query results across joins as the signed-in user.
- Broadcast and Presence
- Napalm does not expose a separate unpersisted broadcast channel API yet. Show active viewers implements room presence with live rows.
- rpc() functions
- Database RPC functions become deployed EdgeQL queries. Function parameters map to typed query variables, and declarative access rules replace
security definerwrappers. - Edge Functions
- Edge Functions that call third-party APIs with secrets become actions that pair a gatekeeper EdgeQL query with a handler you host. Post-commit Edge Functions become jobs.
- pg_cron
napalm schedules addruns any deployed query on a cron schedule under a dedicated service key.- Database webhooks
- A
notifydeclaration on an object type delivers a signed HTTP webhook after every commit affecting that type. Webhooks covers signatures and retries.
Check the migration#
Run the export script twice against the branch
Both runs print identical counts matching your Supabase tables.
Verify shared-note access for both users
Run
napalm people reachfor a note’s author and for a shared collaborator. Both rules evaluate cleanly without policy recursion orsecurity definerhelpers.Sign in with Google as an imported user
Their existing notes appear immediately without sending an email or prompting for a password.
Preview the Storage import with --plan
The dry run counts every
Attachmentawaiting bytes and verifies sample objects against the Supabase S3 endpoint.