feat(postgres): dialecto PostgreSQL + connect(dsn) (Fase 2)#8
Merged
Merged
Conversation
Segundo dialecto. La estructura preparada en Fase 0 paga su precio:
sólo se añade — nada del core ni del ORM se mueve.
Driver
- `shiba/dialects/postgres/driver.py` con psycopg v3, `dict_row`,
autocommit, traducción de excepciones nativas a códigos Shiba.
- `psycopg[binary]` declarado como dep **opcional** en
`[project.optional-dependencies] postgres`; también en `dev` para CI.
- Import perezoso: el paquete `shiba.dialects.postgres` se puede
importar sin tener psycopg instalado; el error sale al construir
`Database`.
Dialect
- `PostgresDialect`: comillas dobles para identificadores, placeholder
`%s`, `ON CONFLICT (...) DO UPDATE SET col = EXCLUDED.col` para
upsert, `INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY` para
auto-increment.
- Mapeo de tipos: `DATETIME` → `TIMESTAMP`, `JSON` → `JSONB`,
`BLOB` → `BYTEA`, `DOUBLE` → `DOUBLE PRECISION`.
Cambios menores en el contrato
- `Dialect.compile_upsert_update(update_columns, conflict_columns=None)`.
- Nuevo `Dialect.compile_auto_increment_pk(quoted_col)`.
- `TableBuilder.increments(pk=True)` delega al dialect.
- `QueryBuilder.upsert(..., on=[...])` para Postgres; MySQL lo ignora.
Factory
- `shiba.connect("mysql://...")`/`shiba.connect("postgres://...")` parsea
el DSN y devuelve la `ShibaConnection` adecuada.
- `ShibaConnection` ahora acepta `db=`/`dialect=` ya construidos, sin
romper la firma legacy `(host, port, user, password)`.
Tests
- 14 nuevos (114/114 total) cubren quoting con doble comilla,
rechazo de injection, DDL con IDENTITY, mapping de tipos JSONB/BYTEA,
upsert ON CONFLICT (con y sin `on`), factory resolviendo MySQL vs
Postgres vs alias `postgresql://`, y rechazo de schemes no soportados.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This was referenced May 18, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Resumen
Segundo dialecto sobre la estructura sentada en Fase 0. Sólo añade; el core y el ORM no se mueven.
```python
import shiba
cx = shiba.connect("postgres://user:pass@host/db")
cx.create_table("users")
.increments("id", primary_key=True)
.json("settings")
.build()
→ CREATE TABLE "users" ("id" INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
"settings" JSONB, ...)
cx.table("users").upsert({"id": 1, "name": "X"}, on=["id"])
→ ON CONFLICT ("id") DO UPDATE SET ...
```
Puntos clave
Cambios mínimos al contrato
Test plan
🤖 Generated with Claude Code