Skip to content

pg

Postgres database clients for sync and async operations.

AsyncPgDb

Bases: AsyncDbBase

Postgres database client for async operations.

Source code in src/embar/db/pg.py
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
@final
class AsyncPgDb(AsyncDbBase):
    """
    Postgres database client for async operations.
    """

    db_type = "postgres"
    conn_wrapper: AsyncConnectionWrapper[AsyncConnection | AsyncConnectionPool]
    _commit_after_execute: bool = True

    def __init__(self, connection_or_pool: AsyncConnection | AsyncConnectionPool):
        """
        Create a new AsyncPgDb instance.
        """
        self.conn_wrapper = AsyncConnectionWrapper(connection_or_pool)

    async def close(self):
        """
        Close the database connection.
        """
        if self.conn_wrapper:
            await self.conn_wrapper.close()

    def transaction(self) -> AsyncPgDbTransaction:
        """
        Start an isolated transaction.

        ```python notest
        from embar.db.pg import AsyncPgDb
        db = AsyncPgDb(None)

        async with db.transaction() as tx:
            ...
        ```
        """
        return AsyncPgDbTransaction(self)

    def select[M: BaseModel](self, model: type[M]) -> SelectQuery[M, Self]:
        """
        Create a SELECT query.
        """
        return SelectQuery[M, Self](db=self, model=model)

    def select_distinct[M: BaseModel](self, model: type[M]) -> SelectDistinctQuery[M, Self]:
        """
        Create a SELECT query.
        """
        return SelectDistinctQuery[M, Self](db=self, model=model)

    def insert[T: Table](self, table: type[T]) -> InsertQuery[T, Self]:
        """
        Create an INSERT query.
        """
        return InsertQuery[T, Self](table=table, db=self)

    def update[T: Table](self, table: type[T]) -> UpdateQuery[T, Self]:
        """
        Create an UPDATE query.
        """
        return UpdateQuery[T, Self](table=table, db=self)

    def delete[T: Table](self, table: type[T]) -> DeleteQueryReady[T, Self]:
        """
        Create an UPDATE query.
        """
        return DeleteQueryReady[T, Self](table=table, db=self)

    def sql(self, template: Template) -> DbSql[Self]:
        """
        Execute a raw SQL query using template strings.
        """
        return DbSql(template, self)

    def migrate(self, tables: Sequence[type[Table]], enums: Sequence[type[EnumBase]] | None = None) -> Migration[Self]:
        """
        Create a migration from a list of tables.
        """
        ddls = merge_ddls(MigrationDefs(tables, enums))
        return Migration(ddls, self)

    def migrates(self, schema: types.ModuleType) -> Migration[Self]:
        """
        Create a migration from a schema module.
        """
        defs = get_migration_defs(schema)
        return self.migrate(defs.tables, defs.enums)

    @override
    async def execute(self, query: QuerySingle) -> None:
        """
        Execute a query without returning results.
        """
        async with self.conn_wrapper as conn:
            await conn.execute(query.sql, query.params)  # pyright:ignore[reportArgumentType]
            if self._commit_after_execute:
                await conn.commit()

    @override
    async def executemany(self, query: QueryMany):
        """
        Execute a query with multiple parameter sets.
        """
        params = _jsonify_dicts(query.many_params)
        async with self.conn_wrapper as conn:
            async with conn.cursor() as cur:
                await cur.executemany(query.sql, params)  # pyright:ignore[reportArgumentType]
            if self._commit_after_execute:
                await conn.commit()

    @override
    async def fetch(self, query: QuerySingle | QueryMany) -> list[dict[str, Any]]:
        """
        Execute a query and return results as a list of dicts.
        """
        async with self.conn_wrapper as conn:
            async with conn.cursor() as cur:
                if isinstance(query, QuerySingle):
                    await cur.execute(query.sql, query.params)  # pyright:ignore[reportArgumentType]
                else:
                    await cur.executemany(query.sql, query.many_params, returning=True)  # pyright:ignore[reportArgumentType]

                if cur.description is None:
                    return []
                columns: list[str] = [desc[0] for desc in cur.description]
                results: list[dict[str, Any]] = []

                for row in await cur.fetchall():
                    data = dict(zip(columns, row))
                    results.append(data)
            if self._commit_after_execute:
                await conn.commit()
            return results

    @override
    async def truncate(self, schema: str | None = None):
        """
        Truncate all tables in the schema.
        """
        schema = schema if schema is not None else "public"
        tables = await self._get_live_table_names(schema)
        if tables is None:
            return
        table_names = ", ".join(tables)
        async with self.conn_wrapper as conn:
            async with conn.cursor() as cursor:
                await cursor.execute(f"TRUNCATE TABLE {table_names} CASCADE")  # pyright:ignore[reportArgumentType]
                if self._commit_after_execute:
                    await conn.commit()

    @override
    async def drop_tables(self, schema: str | None = None):
        """
        Drop all tables in the schema.
        """
        schema = schema if schema is not None else "public"
        tables = await self._get_live_table_names(schema)
        if tables is None:
            return
        table_names = ", ".join(tables)
        async with self.conn_wrapper as conn:
            async with conn.cursor() as cursor:
                await cursor.execute(f"DROP TABLE {table_names} CASCADE")  # pyright:ignore[reportArgumentType]
                if self._commit_after_execute:
                    await conn.commit()

    async def _get_live_table_names(self, schema: str) -> list[str] | None:
        async with self.conn_wrapper as conn:
            async with conn.cursor() as cursor:
                # Get all table names from public schema
                await cursor.execute(f"SELECT tablename FROM pg_tables WHERE schemaname = '{schema}'")  # pyright:ignore[reportArgumentType]
                tables = await cursor.fetchall()
                if not tables:
                    return None
                table_names = [f'"{table[0]}"' for table in tables]
            return table_names

__init__(connection_or_pool)

Create a new AsyncPgDb instance.

Source code in src/embar/db/pg.py
328
329
330
331
332
def __init__(self, connection_or_pool: AsyncConnection | AsyncConnectionPool):
    """
    Create a new AsyncPgDb instance.
    """
    self.conn_wrapper = AsyncConnectionWrapper(connection_or_pool)

close() async

Close the database connection.

Source code in src/embar/db/pg.py
334
335
336
337
338
339
async def close(self):
    """
    Close the database connection.
    """
    if self.conn_wrapper:
        await self.conn_wrapper.close()

delete(table)

Create an UPDATE query.

Source code in src/embar/db/pg.py
379
380
381
382
383
def delete[T: Table](self, table: type[T]) -> DeleteQueryReady[T, Self]:
    """
    Create an UPDATE query.
    """
    return DeleteQueryReady[T, Self](table=table, db=self)

drop_tables(schema=None) async

Drop all tables in the schema.

Source code in src/embar/db/pg.py
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
@override
async def drop_tables(self, schema: str | None = None):
    """
    Drop all tables in the schema.
    """
    schema = schema if schema is not None else "public"
    tables = await self._get_live_table_names(schema)
    if tables is None:
        return
    table_names = ", ".join(tables)
    async with self.conn_wrapper as conn:
        async with conn.cursor() as cursor:
            await cursor.execute(f"DROP TABLE {table_names} CASCADE")  # pyright:ignore[reportArgumentType]
            if self._commit_after_execute:
                await conn.commit()

execute(query) async

Execute a query without returning results.

Source code in src/embar/db/pg.py
405
406
407
408
409
410
411
412
413
@override
async def execute(self, query: QuerySingle) -> None:
    """
    Execute a query without returning results.
    """
    async with self.conn_wrapper as conn:
        await conn.execute(query.sql, query.params)  # pyright:ignore[reportArgumentType]
        if self._commit_after_execute:
            await conn.commit()

executemany(query) async

Execute a query with multiple parameter sets.

Source code in src/embar/db/pg.py
415
416
417
418
419
420
421
422
423
424
425
@override
async def executemany(self, query: QueryMany):
    """
    Execute a query with multiple parameter sets.
    """
    params = _jsonify_dicts(query.many_params)
    async with self.conn_wrapper as conn:
        async with conn.cursor() as cur:
            await cur.executemany(query.sql, params)  # pyright:ignore[reportArgumentType]
        if self._commit_after_execute:
            await conn.commit()

fetch(query) async

Execute a query and return results as a list of dicts.

Source code in src/embar/db/pg.py
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
@override
async def fetch(self, query: QuerySingle | QueryMany) -> list[dict[str, Any]]:
    """
    Execute a query and return results as a list of dicts.
    """
    async with self.conn_wrapper as conn:
        async with conn.cursor() as cur:
            if isinstance(query, QuerySingle):
                await cur.execute(query.sql, query.params)  # pyright:ignore[reportArgumentType]
            else:
                await cur.executemany(query.sql, query.many_params, returning=True)  # pyright:ignore[reportArgumentType]

            if cur.description is None:
                return []
            columns: list[str] = [desc[0] for desc in cur.description]
            results: list[dict[str, Any]] = []

            for row in await cur.fetchall():
                data = dict(zip(columns, row))
                results.append(data)
        if self._commit_after_execute:
            await conn.commit()
        return results

insert(table)

Create an INSERT query.

Source code in src/embar/db/pg.py
367
368
369
370
371
def insert[T: Table](self, table: type[T]) -> InsertQuery[T, Self]:
    """
    Create an INSERT query.
    """
    return InsertQuery[T, Self](table=table, db=self)

migrate(tables, enums=None)

Create a migration from a list of tables.

Source code in src/embar/db/pg.py
391
392
393
394
395
396
def migrate(self, tables: Sequence[type[Table]], enums: Sequence[type[EnumBase]] | None = None) -> Migration[Self]:
    """
    Create a migration from a list of tables.
    """
    ddls = merge_ddls(MigrationDefs(tables, enums))
    return Migration(ddls, self)

migrates(schema)

Create a migration from a schema module.

Source code in src/embar/db/pg.py
398
399
400
401
402
403
def migrates(self, schema: types.ModuleType) -> Migration[Self]:
    """
    Create a migration from a schema module.
    """
    defs = get_migration_defs(schema)
    return self.migrate(defs.tables, defs.enums)

select(model)

Create a SELECT query.

Source code in src/embar/db/pg.py
355
356
357
358
359
def select[M: BaseModel](self, model: type[M]) -> SelectQuery[M, Self]:
    """
    Create a SELECT query.
    """
    return SelectQuery[M, Self](db=self, model=model)

select_distinct(model)

Create a SELECT query.

Source code in src/embar/db/pg.py
361
362
363
364
365
def select_distinct[M: BaseModel](self, model: type[M]) -> SelectDistinctQuery[M, Self]:
    """
    Create a SELECT query.
    """
    return SelectDistinctQuery[M, Self](db=self, model=model)

sql(template)

Execute a raw SQL query using template strings.

Source code in src/embar/db/pg.py
385
386
387
388
389
def sql(self, template: Template) -> DbSql[Self]:
    """
    Execute a raw SQL query using template strings.
    """
    return DbSql(template, self)

transaction()

Start an isolated transaction.

```python notest from embar.db.pg import AsyncPgDb db = AsyncPgDb(None)

async with db.transaction() as tx: ... ```

Source code in src/embar/db/pg.py
341
342
343
344
345
346
347
348
349
350
351
352
353
def transaction(self) -> AsyncPgDbTransaction:
    """
    Start an isolated transaction.

    ```python notest
    from embar.db.pg import AsyncPgDb
    db = AsyncPgDb(None)

    async with db.transaction() as tx:
        ...
    ```
    """
    return AsyncPgDbTransaction(self)

truncate(schema=None) async

Truncate all tables in the schema.

Source code in src/embar/db/pg.py
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
@override
async def truncate(self, schema: str | None = None):
    """
    Truncate all tables in the schema.
    """
    schema = schema if schema is not None else "public"
    tables = await self._get_live_table_names(schema)
    if tables is None:
        return
    table_names = ", ".join(tables)
    async with self.conn_wrapper as conn:
        async with conn.cursor() as cursor:
            await cursor.execute(f"TRUNCATE TABLE {table_names} CASCADE")  # pyright:ignore[reportArgumentType]
            if self._commit_after_execute:
                await conn.commit()

update(table)

Create an UPDATE query.

Source code in src/embar/db/pg.py
373
374
375
376
377
def update[T: Table](self, table: type[T]) -> UpdateQuery[T, Self]:
    """
    Create an UPDATE query.
    """
    return UpdateQuery[T, Self](table=table, db=self)

AsyncPgDbTransaction

Transaction context manager for AsyncPgDb.

Source code in src/embar/db/pg.py
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
class AsyncPgDbTransaction:
    """
    Transaction context manager for AsyncPgDb.
    """

    _db: AsyncPgDb
    _conn_cm: AbstractAsyncContextManager[AsyncConnection] | None = None
    _tx: AbstractAsyncContextManager[AsyncTransaction] | None = None

    def __init__(self, db: AsyncPgDb):
        self._db = db

    async def __aenter__(self) -> AsyncPgDb:
        pool_or_conn = self._db.conn_wrapper.conn_or_pool

        if isinstance(pool_or_conn, AsyncConnectionPool):
            # Ensure pool is open
            await pool_or_conn.open()

            # Check out a dedicated connection for the transaction
            self._conn_cm = pool_or_conn.connection()
            conn = await self._conn_cm.__aenter__()
        else:
            conn = pool_or_conn

        # Create an AsyncPgDb that uses this single connection (no auto-commit)
        tx_db = AsyncPgDb(conn)
        tx_db._commit_after_execute = False  # pyright: ignore[reportPrivateUsage]
        self._db = tx_db

        self._tx = conn.transaction()
        await self._tx.__aenter__()
        return tx_db

    async def __aexit__(
        self,
        exc_type: type[BaseException] | None,
        exc_val: BaseException | None,
        exc_tb: types.TracebackType | None,
    ):
        result = None
        if self._tx is not None:
            result = await self._tx.__aexit__(exc_type, exc_val, exc_tb)
        if self._conn_cm is not None:
            await self._conn_cm.__aexit__(exc_type, exc_val, exc_tb)
        return result

PgDb

Bases: DbBase

Postgres database client for synchronous operations.

Source code in src/embar/db/pg.py
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
@final
class PgDb(DbBase):
    """
    Postgres database client for synchronous operations.
    """

    db_type = "postgres"
    conn_wrapper: ConnectionWrapper[Connection | ConnectionPool]
    _commit_after_execute: bool = True

    def __init__(self, connection_or_pool: Connection | ConnectionPool):
        """
        Create a new PgDb instance.
        """
        self.conn_wrapper = ConnectionWrapper(connection_or_pool)

    def close(self):
        """
        Close the database connection.
        """
        if self.conn_wrapper:
            self.conn_wrapper.close()

    def transaction(self) -> PgDbTransaction:
        """
        Start an isolated transaction.

        ```python notest
        from embar.db.pg import PgDb
        db = PgDb(None)

        with db.transaction() as tx:
            ...
        ```
        """
        return PgDbTransaction(self)

    def select[M: BaseModel](self, model: type[M]) -> SelectQuery[M, Self]:
        """
        Create a SELECT query.
        """
        return SelectQuery[M, Self](db=self, model=model)

    def select_distinct[M: BaseModel](self, model: type[M]) -> SelectDistinctQuery[M, Self]:
        """
        Create a SELECT query.
        """
        return SelectDistinctQuery[M, Self](db=self, model=model)

    def insert[T: Table](self, table: type[T]) -> InsertQuery[T, Self]:
        """
        Create an INSERT query.
        """
        return InsertQuery[T, Self](table=table, db=self)

    def update[T: Table](self, table: type[T]) -> UpdateQuery[T, Self]:
        """
        Create an UPDATE query.
        """
        return UpdateQuery[T, Self](table=table, db=self)

    def delete[T: Table](self, table: type[T]) -> DeleteQueryReady[T, Self]:
        """
        Create an UPDATE query.
        """
        return DeleteQueryReady[T, Self](table=table, db=self)

    def sql(self, template: Template) -> DbSql[Self]:
        """
        Execute a raw SQL query using template strings.
        """
        return DbSql(template, self)

    def migrate(self, tables: Sequence[type[Table]], enums: Sequence[type[EnumBase]] | None = None) -> Migration[Self]:
        """
        Create a migration from a list of tables.
        """
        ddls = merge_ddls(MigrationDefs(tables, enums))
        return Migration(ddls, self)

    def migrates(self, schema: types.ModuleType) -> Migration[Self]:
        """
        Create a migration from a schema module.
        """
        defs = get_migration_defs(schema)
        return self.migrate(defs.tables, defs.enums)

    @override
    def execute(self, query: QuerySingle) -> None:
        """
        Execute a query without returning results.
        """
        with self.conn_wrapper as conn:
            conn.execute(query.sql, query.params)  # pyright:ignore[reportArgumentType]
            if self._commit_after_execute:
                conn.commit()

    @override
    def executemany(self, query: QueryMany):
        """
        Execute a query with multiple parameter sets.
        """
        params = _jsonify_dicts(query.many_params)
        with self.conn_wrapper as conn:
            with conn.cursor() as cur:
                cur.executemany(query.sql, params)  # pyright:ignore[reportArgumentType]
            if self._commit_after_execute:
                conn.commit()

    @override
    def fetch(self, query: QuerySingle | QueryMany) -> list[dict[str, Any]]:
        """
        Execute a query and return results as a list of dicts.
        """
        with self.conn_wrapper as conn:
            with conn.cursor() as cur:
                if isinstance(query, QuerySingle):
                    cur.execute(query.sql, query.params)  # pyright:ignore[reportArgumentType]
                else:
                    cur.executemany(query.sql, query.many_params, returning=True)  # pyright:ignore[reportArgumentType]

                if cur.description is None:
                    return []
                columns: list[str] = [desc[0] for desc in cur.description]
                results: list[dict[str, Any]] = []
                for row in cur.fetchall():
                    data = dict(zip(columns, row))
                    results.append(data)
            if self._commit_after_execute:
                conn.commit()  # Commit after SELECT
            return results

    @override
    def truncate(self, schema: str | None = None):
        """
        Truncate all tables in the schema.
        """
        schema = schema if schema is not None else "public"
        tables = self._get_live_table_names(schema)
        if tables is None:
            return
        table_names = ", ".join(tables)
        with self.conn_wrapper as conn:
            with conn.cursor() as cursor:
                cursor.execute(f"TRUNCATE TABLE {table_names} CASCADE")  # pyright:ignore[reportArgumentType]
                if self._commit_after_execute:
                    conn.commit()

    @override
    def drop_tables(self, schema: str | None = None):
        """
        Drop all tables in the schema.
        """
        schema = schema if schema is not None else "public"
        tables = self._get_live_table_names(schema)
        if tables is None:
            return
        table_names = ", ".join(tables)
        with self.conn_wrapper as conn:
            with conn.cursor() as cursor:
                cursor.execute(f"DROP TABLE {table_names} CASCADE")  # pyright:ignore[reportArgumentType]
                if self._commit_after_execute:
                    conn.commit()

    def _get_live_table_names(self, schema: str) -> list[str] | None:
        with self.conn_wrapper as conn:
            with conn.cursor() as cursor:
                # Get all table names from public schema
                cursor.execute(f"SELECT tablename FROM pg_tables WHERE schemaname = '{schema}'")  # pyright:ignore[reportArgumentType]
                tables = cursor.fetchall()
                if not tables:
                    return None
                table_names = [f'"{table[0]}"' for table in tables]
            return table_names

__init__(connection_or_pool)

Create a new PgDb instance.

Source code in src/embar/db/pg.py
104
105
106
107
108
def __init__(self, connection_or_pool: Connection | ConnectionPool):
    """
    Create a new PgDb instance.
    """
    self.conn_wrapper = ConnectionWrapper(connection_or_pool)

close()

Close the database connection.

Source code in src/embar/db/pg.py
110
111
112
113
114
115
def close(self):
    """
    Close the database connection.
    """
    if self.conn_wrapper:
        self.conn_wrapper.close()

delete(table)

Create an UPDATE query.

Source code in src/embar/db/pg.py
155
156
157
158
159
def delete[T: Table](self, table: type[T]) -> DeleteQueryReady[T, Self]:
    """
    Create an UPDATE query.
    """
    return DeleteQueryReady[T, Self](table=table, db=self)

drop_tables(schema=None)

Drop all tables in the schema.

Source code in src/embar/db/pg.py
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
@override
def drop_tables(self, schema: str | None = None):
    """
    Drop all tables in the schema.
    """
    schema = schema if schema is not None else "public"
    tables = self._get_live_table_names(schema)
    if tables is None:
        return
    table_names = ", ".join(tables)
    with self.conn_wrapper as conn:
        with conn.cursor() as cursor:
            cursor.execute(f"DROP TABLE {table_names} CASCADE")  # pyright:ignore[reportArgumentType]
            if self._commit_after_execute:
                conn.commit()

execute(query)

Execute a query without returning results.

Source code in src/embar/db/pg.py
181
182
183
184
185
186
187
188
189
@override
def execute(self, query: QuerySingle) -> None:
    """
    Execute a query without returning results.
    """
    with self.conn_wrapper as conn:
        conn.execute(query.sql, query.params)  # pyright:ignore[reportArgumentType]
        if self._commit_after_execute:
            conn.commit()

executemany(query)

Execute a query with multiple parameter sets.

Source code in src/embar/db/pg.py
191
192
193
194
195
196
197
198
199
200
201
@override
def executemany(self, query: QueryMany):
    """
    Execute a query with multiple parameter sets.
    """
    params = _jsonify_dicts(query.many_params)
    with self.conn_wrapper as conn:
        with conn.cursor() as cur:
            cur.executemany(query.sql, params)  # pyright:ignore[reportArgumentType]
        if self._commit_after_execute:
            conn.commit()

fetch(query)

Execute a query and return results as a list of dicts.

Source code in src/embar/db/pg.py
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
@override
def fetch(self, query: QuerySingle | QueryMany) -> list[dict[str, Any]]:
    """
    Execute a query and return results as a list of dicts.
    """
    with self.conn_wrapper as conn:
        with conn.cursor() as cur:
            if isinstance(query, QuerySingle):
                cur.execute(query.sql, query.params)  # pyright:ignore[reportArgumentType]
            else:
                cur.executemany(query.sql, query.many_params, returning=True)  # pyright:ignore[reportArgumentType]

            if cur.description is None:
                return []
            columns: list[str] = [desc[0] for desc in cur.description]
            results: list[dict[str, Any]] = []
            for row in cur.fetchall():
                data = dict(zip(columns, row))
                results.append(data)
        if self._commit_after_execute:
            conn.commit()  # Commit after SELECT
        return results

insert(table)

Create an INSERT query.

Source code in src/embar/db/pg.py
143
144
145
146
147
def insert[T: Table](self, table: type[T]) -> InsertQuery[T, Self]:
    """
    Create an INSERT query.
    """
    return InsertQuery[T, Self](table=table, db=self)

migrate(tables, enums=None)

Create a migration from a list of tables.

Source code in src/embar/db/pg.py
167
168
169
170
171
172
def migrate(self, tables: Sequence[type[Table]], enums: Sequence[type[EnumBase]] | None = None) -> Migration[Self]:
    """
    Create a migration from a list of tables.
    """
    ddls = merge_ddls(MigrationDefs(tables, enums))
    return Migration(ddls, self)

migrates(schema)

Create a migration from a schema module.

Source code in src/embar/db/pg.py
174
175
176
177
178
179
def migrates(self, schema: types.ModuleType) -> Migration[Self]:
    """
    Create a migration from a schema module.
    """
    defs = get_migration_defs(schema)
    return self.migrate(defs.tables, defs.enums)

select(model)

Create a SELECT query.

Source code in src/embar/db/pg.py
131
132
133
134
135
def select[M: BaseModel](self, model: type[M]) -> SelectQuery[M, Self]:
    """
    Create a SELECT query.
    """
    return SelectQuery[M, Self](db=self, model=model)

select_distinct(model)

Create a SELECT query.

Source code in src/embar/db/pg.py
137
138
139
140
141
def select_distinct[M: BaseModel](self, model: type[M]) -> SelectDistinctQuery[M, Self]:
    """
    Create a SELECT query.
    """
    return SelectDistinctQuery[M, Self](db=self, model=model)

sql(template)

Execute a raw SQL query using template strings.

Source code in src/embar/db/pg.py
161
162
163
164
165
def sql(self, template: Template) -> DbSql[Self]:
    """
    Execute a raw SQL query using template strings.
    """
    return DbSql(template, self)

transaction()

Start an isolated transaction.

```python notest from embar.db.pg import PgDb db = PgDb(None)

with db.transaction() as tx: ... ```

Source code in src/embar/db/pg.py
117
118
119
120
121
122
123
124
125
126
127
128
129
def transaction(self) -> PgDbTransaction:
    """
    Start an isolated transaction.

    ```python notest
    from embar.db.pg import PgDb
    db = PgDb(None)

    with db.transaction() as tx:
        ...
    ```
    """
    return PgDbTransaction(self)

truncate(schema=None)

Truncate all tables in the schema.

Source code in src/embar/db/pg.py
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
@override
def truncate(self, schema: str | None = None):
    """
    Truncate all tables in the schema.
    """
    schema = schema if schema is not None else "public"
    tables = self._get_live_table_names(schema)
    if tables is None:
        return
    table_names = ", ".join(tables)
    with self.conn_wrapper as conn:
        with conn.cursor() as cursor:
            cursor.execute(f"TRUNCATE TABLE {table_names} CASCADE")  # pyright:ignore[reportArgumentType]
            if self._commit_after_execute:
                conn.commit()

update(table)

Create an UPDATE query.

Source code in src/embar/db/pg.py
149
150
151
152
153
def update[T: Table](self, table: type[T]) -> UpdateQuery[T, Self]:
    """
    Create an UPDATE query.
    """
    return UpdateQuery[T, Self](table=table, db=self)

PgDbTransaction

Transaction context manager for PgDb.

Source code in src/embar/db/pg.py
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
class PgDbTransaction:
    """
    Transaction context manager for PgDb.
    """

    _db: PgDb
    _conn_cm: AbstractContextManager[Connection] | None = None
    _tx: AbstractContextManager[Transaction] | None = None

    def __init__(self, db: PgDb):
        self._db = db

    def __enter__(self) -> PgDb:
        pool_or_conn = self._db.conn_wrapper.conn_or_pool

        if isinstance(pool_or_conn, ConnectionPool):
            # Ensure pool is open (idempotent if already open)
            pool_or_conn.open()

            # Check out a dedicated connection for the transaction
            self._conn_cm = pool_or_conn.connection()
            conn = self._conn_cm.__enter__()
        else:
            conn = pool_or_conn

        # Create a PgDb that uses this single connection (no auto-commit)
        tx_db = PgDb(conn)
        tx_db._commit_after_execute = False  # pyright: ignore[reportPrivateUsage]
        self._db = tx_db

        self._tx = conn.transaction()
        self._tx.__enter__()
        return tx_db

    def __exit__(
        self,
        exc_type: type[BaseException] | None,
        exc_val: BaseException | None,
        exc_tb: types.TracebackType | None,
    ):
        result = None
        if self._tx is not None:
            result = self._tx.__exit__(exc_type, exc_val, exc_tb)
        if self._conn_cm is not None:
            self._conn_cm.__exit__(exc_type, exc_val, exc_tb)
        return result