Database Schemas¶
schemas ¶
Database schema definitions for Egregora V2 (Clean Break).
This module defines the strictly typed, append-only tables for the new architecture.
create_table_if_not_exists ¶
create_table_if_not_exists(
conn: DatabaseConnection,
table_name: str,
schema: Schema,
*,
overwrite: bool = False,
check_constraints: dict[str, str] | None = None,
primary_key: str | list[str] | None = None,
foreign_keys: list[str] | None = None,
) -> None
Create a table using Ibis if it doesn't already exist.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
conn | DatabaseConnection | Database connection (Ibis or raw DuckDB) | required |
table_name | str | Name of the table to create | required |
schema | Schema | Ibis schema definition | required |
overwrite | bool | If True, drop existing table first | False |
check_constraints | dict[str, str] | None | Optional dict of constraint_name -> check_expression Example: {"chk_status": "status IN ('draft', 'published')"} | None |
primary_key | str | list[str] | None | Optional column name (or list of names) to set as PRIMARY KEY | None |
foreign_keys | list[str] | None | Optional list of foreign key constraint strings Example: ["FOREIGN KEY (parent_id) REFERENCES documents(id)"] | None |
Source code in src/egregora/database/schemas.py
ibis_to_duckdb_type ¶
Convert Ibis data type to DuckDB SQL type string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ibis_type | DataType | Ibis data type | required |
Returns:
| Type | Description |
|---|---|
str | DuckDB SQL type string |
Source code in src/egregora/database/schemas.py
add_primary_key ¶
Add a primary key constraint to an existing table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
conn | DatabaseConnection | Database connection (Ibis or raw DuckDB) | required |
table_name | str | Name of the table | required |
column_name | str | list[str] | Column(s) to use as primary key | required |
Note
DuckDB requires ALTER TABLE for primary key constraints.
Source code in src/egregora/database/schemas.py
ensure_identity_column ¶
ensure_identity_column(
conn: DatabaseConnection,
table_name: str,
column_name: str,
*,
generated: str = "ALWAYS",
) -> None
Ensure a column is configured as an identity column in DuckDB.
Source code in src/egregora/database/schemas.py
create_index ¶
create_index(
conn: DatabaseConnection,
table_name: str,
index_name: str,
column_name: str,
index_type: str = "HNSW",
) -> None
Create an index on a table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
conn | DatabaseConnection | Database connection (Ibis or raw DuckDB) | required |
table_name | str | Name of the table | required |
index_name | str | Name for the index | required |
column_name | str | Column to index | required |
index_type | str | Type of index (HNSW for vector search, standard otherwise) | 'HNSW' |
Note
For vector columns, use index_type='HNSW' with cosine metric (optimized for 768-dim embeddings). Uses CREATE INDEX IF NOT EXISTS to handle already-existing indexes.
Source code in src/egregora/database/schemas.py
add_check_constraint ¶
add_check_constraint(
conn: DatabaseConnection,
table_name: str,
constraint_name: str,
check_expression: str,
) -> None
Add a CHECK constraint to an existing table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
conn | DatabaseConnection | Database connection (Ibis or raw DuckDB) | required |
table_name | str | Name of the table | required |
constraint_name | str | Name for the constraint | required |
check_expression | str | SQL expression for the constraint (e.g., "status IN ('draft', 'published')") | required |
Note
DuckDB requires ALTER TABLE for check constraints. Idempotent: silently succeeds if constraint already exists.
Source code in src/egregora/database/schemas.py
get_table_check_constraints ¶
Get CHECK constraints for a table based on business logic.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table_name | str | Name of the table | required |
Returns:
| Type | Description |
|---|---|
dict[str, str] | Dictionary mapping constraint names to CHECK expressions |
Note
This function defines business rules at the database level by specifying CHECK constraints for enum-like fields. Currently supports: - posts.status: Must be one of VALID_POST_STATUSES - tasks.status: Must be one of VALID_TASK_STATUSES
Source code in src/egregora/database/schemas.py
get_table_foreign_keys ¶
Get Foreign Key constraints for a table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table_name | str | Name of the table | required |
Returns:
| Name | Type | Description |
|---|---|---|
list[str] | List of FOREIGN KEY clause strings. | |
Example | list[str] | ["FOREIGN KEY (parent_id) REFERENCES documents(id)"] |