AgentSkillsCN

db-readonly-sql

利用 DATABASE_URL 以及直接执行 psql 命令,为本仓库提供只读的 PostgreSQL 查找技能。当用户要求查看数据、验证记录或解答数据库相关问题时,请使用此技能。务必先草拟 SQL 与命令,征得确认后再在只读事务中执行。

SKILL.md
--- frontmatter
name: db-readonly-sql
description: Read-only PostgreSQL lookup skill for this repository using DATABASE_URL and direct psql execution. Use when the user asks to inspect data, verify records, or answer questions from the DB. Always draft SQL and command first, ask for confirmation, then run inside a READ ONLY transaction.

DB Read-Only SQL

Use this skill for repository-scoped database lookups.

Rules

  • Use read-only SQL only (SELECT or WITH ... SELECT).
  • Never run mutation or schema-changing SQL.
  • Never run multi-statement SQL from user input.
  • Confirm table and column names from packages/db/prisma/schema.prisma before drafting SQL.
  • Draft the SQL and exact psql command first.
  • Ask the user to confirm before executing.

Schema Discovery

  • First source of truth: packages/db/prisma/schema.prisma.
  • If table/column names are still uncertain, draft and run an information_schema lookup query first, then draft the final query.

Disallowed Keywords

Reject SQL containing these case-insensitive keywords:

  • insert
  • update
  • delete
  • drop
  • alter
  • create
  • truncate
  • grant
  • revoke
  • copy
  • call
  • do
  • vacuum
  • analyze

Also reject semicolon-chained statements.

Default Query Limits

  • If query is not an aggregate-only result and has no LIMIT, append LIMIT 50.
  • If LIMIT exceeds 500, reduce to 500.

Required Draft Step

Before any execution, provide:

  1. Draft SQL
  2. Exact command to run
  3. Short expected output note

Then ask: Run this query? (yes/no)

Do not execute until the user confirms.

Execution Command

Preferred command (loads repo .env first, then runs read-only transaction):

bash
set -a; source .env >/dev/null 2>&1; set +a; psql "$DATABASE_URL" -v ON_ERROR_STOP=1 -X -P pager=off -c "BEGIN READ ONLY; <SQL>; ROLLBACK;"

For single-value counts, prefer machine-friendly output:

bash
set -a; source .env >/dev/null 2>&1; set +a; psql "$DATABASE_URL" -v ON_ERROR_STOP=1 -X -At -c "SELECT COUNT(*)::int FROM <table>;"

Environment Checks

Before execution:

  • Confirm DATABASE_URL is set after loading .env.
  • If still missing, check fallback env files:
    • apps/web/.env
    • apps/chainworker/.env

If connection fails, run a quick host diagnostic:

bash
node -e "const u=new URL(process.env.DATABASE_URL||''); console.log(u.host)"

If DNS/network fails in sandbox, rerun the query outside sandbox (with approval) rather than changing SQL.

Output Style

  • Return a concise summary first.
  • Include key rows/values from the result.
  • Include the SQL that was run in a code block.