/sqlty:sqlty-adoptfrom sqlty
Introduces sqlty into an existing Python project with raw SQL queries, one module at a time. Guides installation, dialect/schema configuration, query conversion, and CI gating.
Ships in sqlty. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
sqlty only acts on strings that start with --sql, so you can adopt it one
module, or one query, at a time. Unconverted code keeps working.
Run sqlty version: this skill may be newer or older than the installed sqlty.
When it and sqlty --help disagree, trust the command.
uv add sqlty # or: pip install sqlty; sqlty requires Python >= 3.14
Find the dialect from the driver (psycopg or asyncpg: postgres;
sqlite3 or aiosqlite: sqlite; pymysql, mysqlclient or rustmy: mysql) and
the schema. A schema saves a cast in every query. Dump it from the database,
or point at the migrations:
pg_dump --schema-only app > schema.sql # PostgreSQL
sqlite3 app.db .schema > schema.sql # SQLite
mysqldump --no-data app > schema.sql # MySQL
[tool.sqlty]
dialect = "postgres"
schema = "schema.sql" # or a directory of migrations: "migrations/"
schema.sql:
CREATE TABLE public.users (
id integer NOT NULL,
name text NOT NULL,
created_at timestamp with time zone DEFAULT now()
);
Statements that don't shape a table, such as grants and indexes, are ignored, and a schema-qualified table matches an unqualified query:
query = "--sql\nSELECT id, name, created_at FROM users WHERE id = %s"
# rewritten: query = SQL[tuple[int, str, datetime.datetime | None], tuple[int | None]]("--sql\nSELECT id, name, created_at FROM users WHERE id = %s")
Pick a module whose queries are plain string literals. Add --sql and a
newline to the start of each query, then:
sqlty rewrite app/users.py
sqlty check app/users.py
Skip queries built with f-strings, + or %: convert them to parameters
(%s, $1, ?) first, in a separate change. A --sql string that is still
unwrapped after rewrite has a column sqlty can't type: cast it, or add its
table to the schema.
sqlty check lists the --sql strings left unwrapped as stale-rewrite, so
you can see what is left. To make each untyped query visible as
SQL[UNTYPED], set untyped = "sentinel".
Replace tuple[...] with a TypedDict, dataclass or NamedTuple where a
query feeds domain code, starting with the rows passed around the most. With
psycopg, sqlty.drivers.psycopg.execute returns rows of that type:
from dataclasses import dataclass
from datetime import datetime
from sqlty import SQL
from sqlty.drivers.psycopg import execute
@dataclass
class User:
id: int
name: str
created_at: datetime
GET_USER = SQL[User]("--sql\nSELECT id, name, created_at FROM users WHERE id = %s")
def get_user(conn, user_id: int) -> User | None:
return execute(conn, GET_USER, (user_id,)).fetchone()
sqlty check now fails if users changes and User doesn't follow.
Start with what passes today, then tighten:
- run: uv run sqlty rewrite --check .
- run: uv run sqlty check --output-format github .
Promote stale-type to error once the converted modules are clean, and add
sqlty breaking when schema changes start going through pull requests. See
sqlty-ci.
Once the schema declares its primary, unique and foreign keys, fix the
fanout-aggregate and foreign-key-mismatch warnings, promote them to error, and consider turning on
entity-mismatch (off by default). See
Joins and aggregates.
T, and don't strip a file with named row types."--sql SELECT ..." with a space: the query is a SQL comment.What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
npx claudepluginhub jirikuncar/sqlty --plugin sqlty7 skills in this plugin
/sqltyWrite, type and check raw SQL in Python with sqlty. Use when adding or editing a query in a project that uses sqlty (strings starting with --sql, SQL[T] or SQLTemplate[T] wrappers, a [tool.sqlty] section), when generating Python row types for a query, when typing raw SQL in Go as sqlty.SQL[R, P], or when checking that queries still fit a schema migration before deploying. Routes to sqlty-rewrite, sqlty-check, sqlty-breaking, sqlty-ci and sqlty-adopt for each task./sqlty-breakingCheck that a schema change or migration doesn't break the queries running during a rolling deploy, with sqlty breaking, and plan an expand and contract migration. Use when adding, dropping, renaming or changing a table or column, writing a migration, making a column nullable or NOT NULL, reading a sqlty breaking finding (backward or forward), or splitting a schema change into safe pull requests./Other plugins' skills for the same job