|
| 1 | +## pygres |
| 2 | + |
| 3 | +[](https://codecov.io/gh/OWNER/REPO) |
| 4 | + |
| 5 | +**pygres** is a small helper library around SQLAlchemy that focuses on: |
| 6 | + |
| 7 | +- **Declarative base and registry**: a ready‑to‑use `Base` and `registry` for your models. |
| 8 | +- **PostgreSQL materialized views**: a `MaterializedView` base class and helpers to create, refresh, and drop materialized views. |
| 9 | +- **Schema diffing**: utilities to compare your SQLAlchemy models with an existing PostgreSQL database and render SQL statements to bring the schema in sync. |
| 10 | + |
| 11 | +The goal is to keep the API **minimal, explicit, and easy to reason about** while staying close to plain SQLAlchemy. |
| 12 | + |
| 13 | +--- |
| 14 | + |
| 15 | +### Installation |
| 16 | + |
| 17 | +Install via `pip` (or any PEP 621/pyproject-aware tool like `uv`, `pip-tools`, etc.): |
| 18 | + |
| 19 | +```bash |
| 20 | +pip install pygres |
| 21 | +``` |
| 22 | + |
| 23 | +Or, in a `uv`‑managed project: |
| 24 | + |
| 25 | +```bash |
| 26 | +uv add pygres |
| 27 | +``` |
| 28 | + |
| 29 | +You are expected to install and configure **SQLAlchemy** and a **PostgreSQL driver** (e.g. `psycopg2`) separately, as usual. |
| 30 | + |
| 31 | +--- |
| 32 | + |
| 33 | +### Quick start |
| 34 | + |
| 35 | +#### 1. Define models using the shared `Base` and `registry` |
| 36 | + |
| 37 | +```python |
| 38 | +from sqlalchemy import Column, ForeignKey, Integer, String, Table |
| 39 | +from sqlalchemy.orm import Mapped, mapped_column, relationship |
| 40 | + |
| 41 | +from pygres import Base, registry |
| 42 | + |
| 43 | + |
| 44 | +class User(Base): |
| 45 | + __tablename__ = "users" |
| 46 | + id: Mapped[int] = mapped_column(primary_key=True) |
| 47 | + name: Mapped[str] = mapped_column(String(120)) |
| 48 | + |
| 49 | + |
| 50 | +class AuditLog: |
| 51 | + pass |
| 52 | + |
| 53 | + |
| 54 | +audit_logs_table = Table( |
| 55 | + "audit_logs", |
| 56 | + Base.metadata, |
| 57 | + Column("id", Integer, primary_key=True), |
| 58 | + Column("action", String(120), nullable=False), |
| 59 | +) |
| 60 | +registry.map_imperatively(AuditLog, audit_logs_table) |
| 61 | +``` |
| 62 | + |
| 63 | +You can mix **declarative** mappings (via `Base`) and **imperative** mappings (via `registry.map_imperatively`) in the same metadata. |
| 64 | + |
| 65 | +--- |
| 66 | + |
| 67 | +#### 2. Define a materialized view |
| 68 | + |
| 69 | +`pygres` provides a `MaterializedView` base class. You declare a materialized view by: |
| 70 | + |
| 71 | +- Giving it a `__tablename__` |
| 72 | +- Providing a SQLAlchemy `select()` query in `__mv_query__` |
| 73 | +- Declaring the primary key column names in `__mv_primary_key__` |
| 74 | + |
| 75 | +```python |
| 76 | +from sqlalchemy import select |
| 77 | +from pygres import MaterializedView |
| 78 | + |
| 79 | + |
| 80 | +class UserSummaryMV(MaterializedView): |
| 81 | + __tablename__ = "mv_user_summary" |
| 82 | + __mv_query__ = select(User.id.label("id"), User.name.label("name")) |
| 83 | + __mv_primary_key__ = ("id",) |
| 84 | +``` |
| 85 | + |
| 86 | +The base class will: |
| 87 | + |
| 88 | +- Create a `Table` with the right columns and types derived from the query. |
| 89 | +- Mark the mapped table with `info["is_materialized_view"] = True` so that schema diffing and helpers know how to treat it. |
| 90 | + |
| 91 | +You can control schema and “WITH DATA” behavior with: |
| 92 | + |
| 93 | +- `__mv_schema__`: optional schema name. |
| 94 | +- `__mv_with_data__`: `True` (default) or `False` to create the view `WITH NO DATA`. |
| 95 | + |
| 96 | +--- |
| 97 | + |
| 98 | +#### 3. Create and compare the schema against PostgreSQL |
| 99 | + |
| 100 | +Below is the full example previously implemented in `example.py`, showing how to: |
| 101 | + |
| 102 | +- Spin up a test PostgreSQL instance with `testcontainers`. |
| 103 | +- Create tables and materialized views. |
| 104 | +- Evolve the models. |
| 105 | +- Compute and print SQL statements to migrate the live database schema. |
| 106 | + |
| 107 | +```python |
| 108 | +from sqlalchemy import Column, ForeignKey, Integer, String, Table, create_engine, select |
| 109 | +from sqlalchemy.orm import Mapped, mapped_column, relationship |
| 110 | + |
| 111 | +from pygres import Base, registry, compare_database_schema, MaterializedView |
| 112 | +from testcontainers.postgres import PostgresContainer |
| 113 | + |
| 114 | + |
| 115 | +class User(Base): |
| 116 | + __tablename__ = "users" |
| 117 | + id: Mapped[int] = mapped_column(primary_key=True) |
| 118 | + name: Mapped[str] = mapped_column(String(120)) |
| 119 | + |
| 120 | + |
| 121 | +class AuditLog: |
| 122 | + pass |
| 123 | + |
| 124 | + |
| 125 | +audit_logs_table = Table( |
| 126 | + "audit_logs", |
| 127 | + Base.metadata, |
| 128 | + Column("id", Integer, primary_key=True), |
| 129 | + Column("action", String(120), nullable=False), |
| 130 | +) |
| 131 | +registry.map_imperatively(AuditLog, audit_logs_table) |
| 132 | + |
| 133 | + |
| 134 | +class UserSummaryMV(MaterializedView): |
| 135 | + __tablename__ = "mv_user_summary" |
| 136 | + __mv_query__ = select(User.id.label("id"), User.name.label("name")) |
| 137 | + __mv_primary_key__ = ("id",) |
| 138 | + |
| 139 | + |
| 140 | +if __name__ == "__main__": |
| 141 | + # Start a temporary PostgreSQL instance |
| 142 | + with PostgresContainer("postgres:latest") as postgres: |
| 143 | + engine = create_engine(postgres.get_connection_url()) |
| 144 | + |
| 145 | + # Create the initial schema (tables + materialized views) in the database |
| 146 | + registry.metadata.create_all(engine) |
| 147 | + |
| 148 | + # Evolve the models: add columns and a new table |
| 149 | + class UserV2(Base): |
| 150 | + __tablename__ = "users" |
| 151 | + id: Mapped[int] = mapped_column(primary_key=True) |
| 152 | + name: Mapped[str] = mapped_column(String(120)) |
| 153 | + email: Mapped[str] = mapped_column(String(120)) |
| 154 | + family_id: Mapped[int] = mapped_column(ForeignKey("families.id")) |
| 155 | + family: Mapped["Family"] = relationship("Family", back_populates="users") |
| 156 | + __table_args__ = {"extend_existing": True} |
| 157 | + |
| 158 | + class Family(Base): |
| 159 | + __tablename__ = "families" |
| 160 | + id: Mapped[int] = mapped_column(primary_key=True) |
| 161 | + name: Mapped[str] = mapped_column(String(120)) |
| 162 | + users: Mapped[list[UserV2]] = relationship("UserV2", back_populates="family") |
| 163 | + |
| 164 | + # Compare metadata with the live database and get SQL migration statements |
| 165 | + sql_statements = compare_database_schema( |
| 166 | + engine, |
| 167 | + compare_server_default=False, |
| 168 | + return_sql=True, |
| 169 | + ) |
| 170 | + for stmt in sql_statements: |
| 171 | + print(stmt) |
| 172 | +``` |
| 173 | + |
| 174 | +Running this script will print SQL `ALTER`/`CREATE`/`DROP` statements that would bring the PostgreSQL schema in line with your current SQLAlchemy models and materialized views. |
| 175 | + |
| 176 | +--- |
| 177 | + |
| 178 | +### API overview |
| 179 | + |
| 180 | +- **`pygres.Base`**: Declarative base for your models. |
| 181 | +- **`pygres.registry`**: SQLAlchemy registry used to create `Base` and for imperative mappings. |
| 182 | +- **`pygres.MaterializedView`**: Base class for PostgreSQL materialized view mappings. |
| 183 | +- **`pygres.create_materialized_view(bind, model, if_not_exists=True)`**: Execute and return the `CREATE MATERIALIZED VIEW ...` SQL for the given mapped view. |
| 184 | +- **`pygres.refresh_materialized_view(bind, model, concurrently=False, with_data=True)`**: Execute and return the `REFRESH MATERIALIZED VIEW ...` SQL. |
| 185 | +- **`pygres.drop_materialized_view(bind, model, if_exists=True, cascade=False)`**: Execute and return the `DROP MATERIALIZED VIEW ...` SQL. |
| 186 | +- **`pygres.compare_database_schema(bind, **options)`**: |
| 187 | + - With `return_sql=False` (default): returns a list of `SchemaDiff` objects. |
| 188 | + - With `return_sql=True`: returns a list of SQL strings that can be applied to migrate the schema. |
| 189 | +- **`pygres.render_compensation_sql(diffs, dialect)`**: Convert a list of `SchemaDiff` instances into SQL strings for a specific dialect. |
| 190 | + |
| 191 | +All APIs are intentionally thin wrappers around SQLAlchemy primitives so that you can always drop down to plain SQLAlchemy when needed. |
| 192 | + |
| 193 | +--- |
| 194 | + |
| 195 | +### Notes |
| 196 | + |
| 197 | +- Materialized view support currently targets **PostgreSQL**; attempting to use it with other dialects will raise a `ValueError`. |
| 198 | +- `compare_database_schema` is designed for migration/compensation SQL generation, not as a full migration framework. You can integrate the generated SQL into your own deployment/migration tooling as you see fit. |
0 commit comments