Skip to content

feat(cli): add --project-ref flag to db commands - #6156

Open
Coly010 wants to merge 1 commit into
developfrom
columferry/db-commands-project-ref-flag
Open

feat(cli): add --project-ref flag to db commands#6156
Coly010 wants to merge 1 commit into
developfrom
columferry/db-commands-project-ref-flag

Conversation

@Coly010

@Coly010 Coly010 commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

Summary

A Front support ticket surfaced that we tell users db push --project-ref <ref> works, but the legacy TS shell never accepted the flag. In the Go CLI the linked-ref override was only reachable on db commands via the SUPABASE_PROJECT_ID env var (Go never registered --project-ref on them); the Effect CLI is explicit about which commands take which flags, and the db family was missed when groups like sso got it.

This adds an optional --project-ref flag ("Project ref of the Supabase project.") to the eight db subcommands that resolve a project ref: push, pull, diff, dump, reset, lint, advisors, query.

Behavior:

  • The flag feeds LegacyProjectRefResolver.loadProjectRef, keeping the resolver's existing precedence: flag > SUPABASE_PROJECT_ID > supabase/.temp/project-ref. With the flag unset, behavior is unchanged.
  • It is threaded into every ref-resolution site per command (direct loadProjectRef calls, the shared LegacyDbConfigResolver's linkedProjectRef, the pooler-fallback path, and dump's pre-connect cache capture), so the connection, the [remotes.<ref>] config merge, the linked-project cache, and Management API paths can never disagree on the ref within one invocation.
  • It does not imply --linked. Instead of silently ignoring the flag on a non-linked target (a footgun on db reset), the command errors: --project-ref only applies when targeting the linked project; use it with --linked (not --local or --db-url). db diff keeps its explicit-mode exception (--from/--to linked) and rejects the flag with the Go-delegating --use-pgadmin/--use-pg-schema engines rather than dropping it.
  • Telemetry: the flag is recorded but its value stays redacted (no Go markFlagTelemetrySafe baseline exists for db commands; the nearest Go registration, cmd/pgdelta_catalog.go, is unmarked).
  • adHocProjectRef deliberately stays unset so credential semantics match the env-var path (documented in legacy-db-config.types.ts).

Docs updated: go-cli-porting-status.md (extra-TS-flag notes + missing db advisors/db query rows) and the eight commands' SIDE_EFFECTS.md.

Follow-up (not in this PR): the migration family (up/down/list/repair/fetch), seed buckets, and storage ls/cp/mv/rm share the same linked-ref chain and still only support the env var/link file.

Linked issue

No GitHub issue — raised via a Front support ticket (Supabase maintainer).

🤖 Generated with Claude Code

Adds an optional --project-ref flag to the eight legacy db subcommands
that resolve a project ref (push, pull, diff, dump, reset, lint,
advisors, query), so e.g. `supabase db push --project-ref <ref>`
targets that project without requiring a linked workdir. The flag
follows the resolver's existing precedence (flag > SUPABASE_PROJECT_ID
> supabase/.temp/project-ref) and errors when combined with a
non-linked target instead of being silently ignored.
@Coly010
Coly010 requested a review from a team as a code owner August 11, 2026 16:37
@github-actions

Copy link
Copy Markdown
Contributor

Supabase CLI preview

npx --yes https://pkg.pr.new/supabase/cli/supabase@a6ef7c65c445989ee8e7eefbde2743773b09249f

Preview package for commit a6ef7c6.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a6ef7c65c4

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

connType,
dnsResolver,
password: flags.password ?? Option.none(),
linkedProjectRef: flags.projectRef,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Forward the project override to the delegated pull

When experimental structured-dump mode is enabled, this resolves and checks the connection for flags.projectRef, but delegatePull() later starts the bundled Go command through rebuildDelegateArgs(flags), which neither forwards this TS-only flag nor sets SUPABASE_PROJECT_ID in the child environment. The child therefore falls back to the workdir's linked ref and can pull schema from a different project than the one the parent just resolved; either pass the resolved ref through the environment or reject this flag/mode combination as db diff does.

AGENTS.md reference: apps/cli/AGENTS.md:L249-L255

Useful? React with 👍 / 👎.

Comment on lines +175 to 181
Option.isSome(flags.projectRef) && flags.projectRef.value.length > 0
? flags.projectRef
: Option.isSome(cliConfig.projectId)
? cliConfig.projectId
: yield* legacyReadProjectRefFile(fs, path, cliConfig.workdir);
if (Option.isSome(refOpt)) {
linkedRefForCache = refOpt.value;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Validate the ref before scheduling its cache write

For a malformed nonempty --project-ref, this stores the raw value before resolver.resolve() validates and rejects it. Because the cache operation runs in an Effect.ensuring finalizer, a cache miss with credentials still issues an authenticated /v1/projects/<malformed-ref> request—and may write its response—even though ref resolution failed. Resolve and validate through LegacyProjectRefResolver.loadProjectRef() before assigning linkedRefForCache, as the other changed handlers do.

AGENTS.md reference: apps/cli/AGENTS.md:L96-L98

Useful? React with 👍 / 👎.

Comment on lines +77 to +81
* The eight `db` commands' `--project-ref` deliberately leave this unset:
* unlike `gen types --project-id`'s genuinely ad-hoc target, `db`'s
* `--project-ref` is meant to have identical workdir credential semantics to
* `SUPABASE_PROJECT_ID` — it may still reuse the ambient `SUPABASE_DB_PASSWORD`
* / `--password`, since forcing ad-hoc would silently break existing

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Fetch the overridden project's pooler configuration

When --project-ref targets an unlinked project from an IPv4-only network, leaving adHocProjectRef false prevents legacyResolveLinkedConn() from fetching that project's primary pooler configuration: it only checks the workdir's saved .temp/pooler-url, which is absent or belongs to another project, and then fails with the IPv6/link suggestion. This breaks the newly tested “without a linked workdir” workflow for the common IPv6-only direct database host case across all eight commands. Decouple pooler selection from ambient-password handling so an explicit ref fetches its own pooler while retaining the intended password precedence.

Useful? React with 👍 / 👎.

Comment on lines +193 to +195
Option.isSome(flags.projectRef) &&
legacyClassifyExplicitRef(from) !== "linked" &&
legacyClassifyExplicitRef(to) !== "linked"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Honor --linked in the explicit-mode project-ref guard

For db diff --from local --to migrations --linked --project-ref <ref>, this condition rejects the invocation even though the error explicitly says to use --project-ref with --linked. The subsequent preflight is designed to resolve that linked ref and merge its [remotes.<ref>] configuration into explicit local/migrations references, so the flag is not unused in this scenario. Exclude an explicitly changed --linked target from this guard, in addition to the existing from/to linked exceptions.

AGENTS.md reference: apps/cli/AGENTS.md:L249-L255

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants