Files
turso/bindings/python/tests
Pekka Enberg e0b306bfd6 Merge 'Add SQLAlchemy dialect for Python bindings' from Orthel Toralen
This PR adds a SQLAlchemy dialect for pyturso with two connection modes:
- `sqlite+turso://` — local database connections via `turso.connect()`
- `sqlite+turso_sync://` — sync-enabled connections via
`turso.sync.connect()` with `pull()`/`push()` support
 This PR adds a SQLAlchemy dialect for pyturso with two connection
modes:
- `sqlite+turso://` — local database connections via `turso.connect()`
- `sqlite+turso_sync://` — sync-enabled connections via
`turso.sync.connect()` with `pull()`/`push()` support
Claude Code provided invaluable assistance in development and
documentation of this PR. Claude assisted in research to close gaps in
my knowledge of SQLAlchemy dialects and DB-API, helped evaluate design
trade-offs between syntactic consistency and correctness, and generated
code and tests once decisions were made. Claude and I also spent
considerable time in chats, imagining the foot-guns developers may
encounter and devising solutions to neutralize them.
## Implementation Details
### Summary of Changes
- **`turso/sqlalchemy/dialect.py`** — `TursoDialect`,
`TursoSyncDialect`, shared `_TursoDialectMixin` for PRAGMA overrides,
and `get_sync_connection()` helper
- **`turso/sqlalchemy/__init__.py`** — public API exports
- **DB-API 2.0 compliance** — added
`sqlite_version`/`sqlite_version_info` to `turso/lib.py`, re-exported
DB-API attributes and exception hierarchy in `turso/sync/__init__.py`
- **Entry points** in `pyproject.toml` for automatic dialect
registration
- **Documentation** — `SQLALCHEMY_DIALECT.md` covering URL formats, sync
operations, architecture, overrides, and limitations
- **74 tests** — unit tests for URL parsing, dialect attributes,
reflection overrides, pool selection, isolation levels; integration
tests for CRUD, ORM, Pandas `to_sql`, transactions, joins, unicode,
error propagation
### Design decisions
- **Inherits from `SQLiteDialect_pysqlite`** — reuses SQLAlchemy's
battle-tested SQLite dialect, overriding only what Turso needs
  - **`_TursoDialectMixin`** — Shared mixin overriding reflection
methods that rely on unsupported PRAGMAs (foreign_key_list, index_list)
and sqlite_temp_master. Returns empty results with debug logging instead
of raising errors, preventing MetaData.reflect(), Alembic, and Pandas
  df.to_sql(if_exists='replace') from breaking.
- **`on_connect()` returns `None`** — skips pysqlite's REGEXP function
setup since Turso doesn't support `create_function`
- **Isolation level** — `get_isolation_level()` returns SERIALIZABLE,
`set_isolation_level()` is a no-op (set at connection time)
- **Pool selection** — `SingletonThreadPool` for `:memory:`, `QueuePool`
for file databases
- **get_sync_connection()** - A helper function that exposes the
underlying turso_sync connection.
### Test results
| Test Suite | Result |
|------------|--------|
| SQLAlchemy unit/integration | 69 passed, 5 skipped (sync needs
server), 97.5% dialect coverage |
| Existing Python binding tests | 210 passed, no regressions |
| Real-world tests (Turso Cloud/local embedded replicas) | 31 passed —
ORM, Pandas, sync push/pull |
### Files changed
| File | Change |
|------|--------|
| `bindings/python/turso/sqlalchemy/dialect.py` | New — dialect
implementations |
| `bindings/python/turso/sqlalchemy/__init__.py` | New — module exports
|
| `bindings/python/turso/lib.py` | Added `sqlite_version`,
`sqlite_version_info` |
| `bindings/python/turso/__init__.py` | Re-export new version attrs |
| `bindings/python/turso/sync/__init__.py` | Re-export DB-API 2.0 attrs
+ exceptions |
| `bindings/python/pyproject.toml` | Entry points + sqlalchemy optional
dep |
| `bindings/python/tests/test_sqlalchemy.py` | New — 71 tests |
| `bindings/python/SQLALCHEMY_DIALECT.md` | New — documentation |
| `licenses/bindings/python/sqlalchemy-mit-license.md` | New —
SQLAlchemy MIT license |
| `NOTICE.md` | Added SQLAlchemy dependency entry |
### Usage
```python
from sqlalchemy import create_engine, text

# Local / in-memory
engine = create_engine("sqlite+turso:///:memory:")

# Sync (connect_args — familiar to libsql-sqlalchemy users. We accept sync_url as well for compatibility)
engine = create_engine(
    "sqlite+turso_sync:///local.db",
    connect_args={
        "remote_url": "https://your-db.turso.io",
        "auth_token": "your-token",
    }
)

# Sync (URL query parameters)
engine = create_engine(
    "sqlite+turso_sync:///local.db"
    "?remote_url=https://your-db.turso.io"
    "&auth_token=your-token"
)

# Access sync operations
from turso.sqlalchemy import get_sync_connection
with engine.connect() as conn:
    sync = get_sync_connection(conn) # Unwraps the underlying turso.sync connection
    sync.pull()
    result = conn.execute(text("SELECT * FROM users"))
    conn.commit()
    sync.push()
```

Closes #5590
2026-02-27 09:54:53 +02:00
..
2024-08-11 08:58:18 +02:00
2025-12-26 17:22:34 +04:00
2026-01-28 11:27:39 +05:30
2026-02-26 04:42:57 -05:00