feat(db): add supabase db query command for executing SQL#4955
feat(db): add supabase db query command for executing SQL#4955
supabase db query command for executing SQL#4955Conversation
Pull Request Test Coverage Report for Build 23144421972Details
💛 - Coveralls |
Add a new CLI command that allows executing raw SQL against local and remote databases, designed for seamless use by AI coding agents without requiring MCP server configuration. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
c45c99b to
095618d
Compare
avallete
left a comment
There was a problem hiding this comment.
Overall looks good to me, just a few nitpicks and questions.
- Remove unnecessary math.MaxInt guard on fd cast, use //nolint:gosec - Add --db-url, --linked, --local flags with mutual exclusivity - Replace custom jsonReader with bytes.NewReader - Add tests for formatOutput with nil cols/data Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
@Rodriguespn so happy to see this! I missed it, but wanted to add, can we add an alias for |
I don't see big issues with having an alias. But wonder about the best solution to solve LLM halucinations. Does having an |
Summary
Add
supabase db query [sql]command for executing raw SQL against local and remote Supabase databases.Why do we need this if we already have
execute_sqlin the MCP server?The MCP server is great for agents to securely interact with remote projects. The user has to follow the OAuth flow to authenticate the agent and then reload the agent session to load the MCP tools into context. This allows the agent to interact with the user's remote project without managing api keys and it's one of the advandages of using MCP over the CLI.
When working locally, there is no auth flow needed to connect to
localhost:54321/mcp, but the agent still requires the human to reload the agent session to load the MCP tools into context, when setting up the MCP initial connection. This adds friction to a path that should be fully agentic (no human in the loop).For this, the AI team suggests having a
db queryCLI command that allows the agent to interact with the database like theexecute_sqlMCP tool does.Example use case: local schema management. The agent changes the schema of the database by running DDL commands and, once it determines the schema is stable, runs
db diff --localto inspect schema changes and inform the migration name. With the current solution, we need theexecute_sqlMCP tool configured to run the queries. With this command, this development path only needs the CLI — no MCP configuration needed.Prompt injection safety
To prevent prompt injections, the default output format is JSON, where we wrap every response in a safety envelope — the same approach used by the
execute_sqlMCP tool output. The warning message reads:Implementation
supabase db query "SELECT ...", default): Uses pgx (direct Postgres wire protocol). pgx makes more sense than pg-meta for local because pg-meta runs as a Docker container inside thesupabase startstack — using it would require discovering the container port, authenticating with the service-role JWT, and making HTTP requests. pgx simply connects tolocalhost:54322with the password from config: direct TCP, no Docker dependency, no HTTP overhead, and consistent with every other localdbsubcommand (push,pull,diff,lint,test,reset,dump).supabase db query "SELECT ..." --linked): Uses the Management API (POST /v1/projects/{ref}/database/query), authenticated with the access token fromsupabase login. No database password needed — zero credential friction for agents.Usage
Test plan
go test ./internal/db/query/...— 17 unit tests covering:--file, stdin pipe, no input, file not foundcc @gregnr @mattrossman