codemod
Rewrite imports, subpaths and environment names for renamed better-supabase APIs after an upgrade.
better-supabase codemod rewrites your source for the renames in a release,
so an upgrade is a command and a review instead of a search through the
codebase. Run it after you update the package:
pnpm better-supabase codemod naming --dry-run
pnpm better-supabase codemod namingPass files or directories after the name to limit it (codemod naming src app).
Without them it walks the project and skips node_modules, build output
(dist, build, .next, .turbo, .vercel, coverage) and .d.ts files.
--dry-run prints a unified diff of each file it would change and writes
nothing. --check writes nothing and exits 1 when a file still needs the
codemod, so CI can tell the upgrade is done (writing files).
--table <block>.<table> names the table the adopt-names codemod renames, one per run.
--prune applies to the config codemod only: it removes every config entry
that equals its default (doctor BS427), renders the blocks with the old and the
new config, and writes nothing when any generated file differs.
--explain applies to the table-tags codemod only: it lists each entry that would move, says whether the render reads the tag or only doctor does, and writes nothing. Before it moves a data-lifecycle or expose entry, the codemod reads the config back through the tag and keeps the entry in the config when the value differs.
The codemod runs even when the config file fails to load, because the config
it migrates is often the one the new version rejects. It then walks the
directory that holds the discovered config file, or --cwd when there is none.
This release upgrades from 0.53 only. An app on an older version upgrades to 0.53 first with that release (ADR 0245).
| Codemod | What it changes |
|---|---|
naming | Applies the rename table of api/naming.json: renamed exports and types in imports and where they are used, renamed better-supabase/<subpath> specifiers, renamed environment variables in source, .env* files and turbo.json, one hand migration for renamed SQL objects of installed blocks (alter ... rename, -- destructive: reviewed, no data moves), the renamed block column names in your schema files and seeds (only in statements that name a better_supabase table, never in migrations), and a columns entry in the config that keeps the old name of a column your own table already has (drive_nodes.deleted_at, the actor columns of scorecards and status history). |
extra-columns | Moves each blocks.<name>.options.extraColumns (and *ExtraColumns) column into your schema files and drops the option from the config. A column goes beside the alter table statements of an app file that already alters the block's table, or into <table>.ext.sql next to your schema files when none does. The block then reads the column as a field because your file adds it. Files a block wrote (@bsb-block) are never edited. For a table you create yourself, the codemod lists the columns in the table comment as @bsb fields, adding the comment when the table has none. A column whose table the block does not have in your project stays in the config and is listed after the summary. The options are rejected at render time with a message that names this codemod. |
owned-columns | Declares, in your schema files, the columns blocks add to tables you create (doctor BS622). It renders the blocks, finds each alter table ... add column if not exists on a table your files create when your files lack the column, and writes the statements to <table>.block-columns.sql next to your schema files. The statements are the blocks' own, so a database that has the columns changes nothing. Running it again finds nothing. |
blocks-list | Rewrites blocks: { name: { ... } } in the config as a list: a name for an empty entry and { block, name?, ... } for the rest. The keyed form is rejected when the config loads, with a hint to run this codemod. It edits the config file only, keeps comments, and writes nothing when the file already holds a list. |
record-types | Writes the names in blocks.<name>.on as the record types in recordTypes, where they differ only by - and _ or are a type's alias. It edits the config file only and keeps comments. Doctor rule BS346 suggests it when a name matches after that normalization. |
table-tags | Moves the facts the config holds about one table into comment on table tags in your schema files and drops the config entries: an audit exemption in blocks.audit.options.exempt (@bsb audit-exempt reason="..."), a blocks.data-lifecycle.options.tables entry (@bsb data-lifecycle user=owner_id) and an expose entry (@bsb grant authenticated=select). A tag goes into the schema file that creates the table. A fact whose table no schema file creates, or whose comment already holds the tag, stays in the config and is listed. It also moves a blocks.memberships.options.sameTenant entry into a @bsb same-tenant tag on its column when the tag renders the same trigger statement, and lists each entry it leaves with the reason. It moves a recordTypes entry (@bsb subject), a realtime table with its global and users entries (@bsb realtime) and a vectorSearch entry (@bsb vector-search) the same way, and lists each hand-written trigger that calls better_supabase.broadcast_topic(), which realtime.triggers declares instead. Doctor rule BS702 suggests it. |
adopt-names | Renames one table to the block's names: --table <block>.<table>. It writes a hand migration with the alter table ... rename statements, rewrites the statements in your schema files that name the table and its mapped columns (through the SQL scanner, so strings and comments stay), removes the blocks.<block>.tables and columns entries and lists the types that change. A values entry stays. It refuses a table outside the block's schema. |
declared-rows | Moves the option set items and brands, the pipeline stages and brands, and the approval policies floors out of the config. It writes supabase/rows/option-sets.sql, pipelines.sql and approvals.sql (upserts into better_supabase.declared_defaults) and removes the moved keys from the config file. It never overwrites a rows file. Run db rows afterwards to write the data migration, and remove an import the config no longer uses, such as a stage helper. |
doctor-baseline | Rewrites the file at tooling.doctor.baseline from the { "BS351": { "reason": "...", "targets": [] } } object form into the grouped list doctor reads ([{ code, reason, targets }]). A file that is already a list is left alone. |
read-many | Rewrites nothing. Lists each Promise.all of repository reads on one db that db.$many or a read set can load in one round trip, and each one that mixes in other calls. |
expire-tags | Rewrites nothing. Lists each updateTag and revalidateTag call from next/cache with the await bsb.expireTags([...]) call that replaces it, so the kept store expires with Next's cache (doctor BS648). |
table-modules | Moves a project to the default layout: schema is imported from generated-schema, and a "use client" file reads bsb.db.<table> as bsb.table(<table>Table) with the table module imported. A queries.<table> read is listed for a hand change. It does nothing when gen.tableModules is false or gen.contentMapper is on. |
account-calls | Rewrites bsb.deleteAccount(id) to deleteAccount(bsb, id), and the same for suspendAccount, endSessions and lastSignIns, and imports them from better-supabase/server/account (ADR 0604). Only a call on a plain identifier is rewritten. |
server-imports | Splits each import from better-supabase/server so the account helpers (deleteAccount, suspendAccount, endSessions, lastSignIns, supportSessions, supportStorage, sessionStatusRoute, claimsFingerprint), the CSV helpers and the rate-limit header helpers import from better-supabase/server/accounts, /csv and /rate-limit (ADR 0450). Names that stayed keep their import. |
generators | Moves the generator imports from better-supabase/config to better-supabase/generators/<name> (zod, valibot, jsonSchema and toJsonSchema, standardSchema, factories, nuqs, with their option types), and lists each line of a generator that still uses the apiVersion: 1 shape: runtimeImport, input.importPath, apiVersion: 1 and validators: true. |
config | Removes the columns entries the render refuses: a null for an optional column the view the block runs over lacks anyway, and a tenant that repeats sql.tenantColumn (doctor BS379). Other null entries stay. It edits the config file only. With --prune it removes every entry doctor BS427 reports (including a gen.functions notNull the block already declares), and refuses to write when the render changes. When no sql.api is set and most blocks name the same api schema, it also sets database.sql.api, removes the matching entries and writes api: false on the blocks with none. |
| Command | Side effects |
|---|---|
codemod naming | Writes files. |
codemod extra-columns | Writes files. |
codemod owned-columns | Writes files. |
codemod config | Writes files. |
codemod blocks-list | Writes files. |
codemod record-types | Writes files. |
codemod table-tags | Writes files. |
codemod adopt-names | Writes files. |
codemod declared-rows | Writes files. |
codemod doctor-baseline | Writes files. |
codemod expire-tags | Writes files. |
codemod read-many | Writes files. |
codemod table-modules | Writes files. |
codemod account-calls | Writes files. |
codemod server-imports | Writes files. |
codemod generators | Writes files. |
What it rewrites
Each codemod makes narrow, mechanical renames. It reads the source with a parser, so a name in a string, a template literal or a comment stays as it is.
- Imports. A renamed export imported from
better-supabaseor one of its subpaths is renamed in the import and wherever the file uses it. An aliased import (Postgres as PG) keeps the alias, and imports from other modules are left alone. - Subpaths. A
better-supabase/<subpath>specifier in a string is renamed. - Environment variables. In TypeScript, a renamed variable is renamed where
code names it (
process.env.NAME, a key, a binding), and in a string only when the whole string is the name. A comment stays as it is, and a string that holds the name among other words is listed for review. In an.env*file orturbo.jsonit is renamed as a whole word. - SQL. A renamed table, column or policy of an installed block gets one
migration of
alter ... renamestatements, each behind an existence check.
Following a symbol across files
With typescript 7.1 or newer installed, the CLI can ask TypeScript where a
symbol is used and declared, so an edit reaches every file that uses it. The
CLI starts it only for an edit that needs it, and works from the version in
your own node_modules. With an older TypeScript, or none, each codemod works
file by file and lists the places a symbol leaves its file as review lines.
- Re-exports.
codemod namingrenames a name your app re-exports (export { old } from "better-supabase") and the files that import it from there. Without TypeScript 7.1 the re-export gets a review line and its importers stay as they are. - Imported config values.
codemod configremoves acolumnsentry from the file that declares a value the config imports (blocks: sharedBlocks). Without TypeScript 7.1 a review line lists the imported values to edit by hand. - Other config edits.
codemod prune,declared-rows,adopt-namesandextra-columnsedit the file that declares an imported value too, and list it for review, because the render check covers only the config file. Runbetter-supabase gen --checkafterwards.
What it leaves for you
A TypeScript file that does not parse is never skipped in silence: the codemod leaves it unchanged and lists the line and the parser's message, so you can fix the file and run the codemod again.
Some renames depend on what a variable holds. The codemod doesn't guess: it
lists those lines after the summary, as path:line: message, so you can change
them by hand.
Updated 3 of 41 files:
src/lib/supabase/client.ts
src/app/providers.tsx
src/app/api/mcp/route.ts
Check these by hand:
src/app/page.tsx:12: the message the codemod listsRun your type checker afterwards; a rename the codemod missed shows up there.
With --json, the envelope's data is { codemod, dryRun, changed, review }.
SQL module renames of the block SQL itself are not source rewrites:
better-supabase upgrade handles them. Doctor BS260 reports source that still
uses a renamed name.
Last updated on