DEV Community

William Rodriguez
William Rodriguez

Posted on

Migrations without Alembic: Automatic column additions with TableSync

Why should adding an optional field to a Pydantic model require generating and running an Alembic migration file? wsqlite's TableSync inspects the schema and adds missing columns automatically.

Here is how you use TableSync & Zero-Downtime Column Addition in production with wsqlite:

from typing import Optional
from pydantic import BaseModel
from wsqlite import WSQLite

# Initial Model
class UserV1(BaseModel):
    id: int
    name: str

db = WSQLite(UserV1, "app.db")
db.insert(UserV1(id=1, name="Alice"))

# Later: Add a new column to the model
class UserV2(BaseModel):
    id: int
    name: str
    email: Optional[str] = None  # New column!

# Re-initializing instantly executes ALTER TABLE ADD COLUMN
db2 = WSQLite(UserV2, "app.db")
db2.insert(UserV2(id=2, name="Bob", email="bob@example.com"))
print(db2.get_all())
Enter fullscreen mode Exit fullscreen mode

Why developers love wsqlite:

  • Define database tables using standard Pydantic v2 models.
  • Auto-syncs columns on startup without writing manual migrations.
  • Thread-safe connection pooling with WAL mode enabled by default (5,000+ inserts/sec).
  • Full sync and async/await support.

Installation & Repository

pip install wsqlite
Enter fullscreen mode Exit fullscreen mode

Author: William Steve Rodríguez Villamizar (Wisrovi)

Top comments (0)