Metadata-Version: 2.4
Name: irides-core
Version: 0.2.0
Summary: A Python library for extracting structured database metadata to provide reliable context to AI systems and data-driven applications.
Author-email: Gian Andrea Sechi <me@gianandreasechi.com>
License: Apache-2.0
Project-URL: Source Code, https://github.com/GianAndreaSechi/irides/tree/main/packages/core
Project-URL: Homepage, https://www.gianandreasechi.com
Keywords: database,introspection,metadata,irides,schema,redis
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Database
Requires-Python: >=3.11
Description-Content-Type: text/markdown
Requires-Dist: pydantic>=2.0
Requires-Dist: loguru>=0.7.0
Requires-Dist: python-dotenv>=1.0.0
Requires-Dist: PyYAML>=6.0
Provides-Extra: redis
Requires-Dist: redis>=4.0; extra == "redis"
Provides-Extra: postgres
Requires-Dist: psycopg2-binary>=2.9.0; extra == "postgres"
Provides-Extra: mysql
Requires-Dist: mysql-connector-python>=8.0.0; extra == "mysql"
Provides-Extra: mongo
Requires-Dist: pymongo>=4.0.0; extra == "mongo"
Provides-Extra: duckdb
Requires-Dist: duckdb>=1.1.0; extra == "duckdb"
Provides-Extra: aws
Requires-Dist: boto3>=1.26.0; extra == "aws"
Provides-Extra: trino
Requires-Dist: trino>=0.320.0; extra == "trino"
Provides-Extra: ai
Requires-Dist: litellm>=1.0.0; extra == "ai"
Provides-Extra: all
Requires-Dist: redis>=4.0; extra == "all"
Requires-Dist: psycopg2-binary>=2.9.0; extra == "all"
Requires-Dist: mysql-connector-python>=8.0.0; extra == "all"
Requires-Dist: pymongo>=4.0.0; extra == "all"
Requires-Dist: duckdb>=1.1.0; extra == "all"
Requires-Dist: boto3>=1.26.0; extra == "all"
Requires-Dist: trino>=0.320.0; extra == "all"
Requires-Dist: litellm>=1.0.0; extra == "all"
Provides-Extra: dev
Requires-Dist: pytest>=7.0.0; extra == "dev"
Requires-Dist: pytest-asyncio>=0.20.0; extra == "dev"

# Core (`irides_core` — v0.2.0)

Shared Python library used by the **CLI**, **Prism**, **Enrichment**, **API**, and **Workers** (`worker`, `worker_prism`, and `worker_enrichment`). As of `0.2.0`, `irides_core` is the canonical domain-organized package (`db/`, `contracts/`, `ai/`, `jobs/`, `config/`) and `redis` is an optional extra (`irides-core[redis]`).

Both canonical `import irides_core...` and legacy `import core.db_connector...` (via a backward-compatible shim) are supported.

---

## Package Structure

```text
packages/core/
├── irides_core/                    # Canonical domain-organized package (v0.2.0)
│   ├── ai/
│   │   └── ai_service.py           # LLMJsonClient + AIDocumentationService (LiteLLM)
│   ├── config/
│   │   ├── configurations.py       # DB & runtime config loading (YAML/JSON/.env)
│   │   ├── cache_manager.py        # Optional Redis cache (soft-imports redis)
│   │   ├── config_service.py       # ConfigService — resolves configs & instances
│   │   └── storage.py              # BaseMetadataStore + FileMetadataStore
│   ├── contracts/
│   │   ├── discovery_models.py     # Shared database.md / technology.md contracts
│   │   ├── enrichment_models.py    # Shared EnrichmentMetadata / WikiLink / relationship models
│   │   ├── manifest.py             # Project manifest validator & reverse-link builder
│   │   ├── plugins.py              # Entry-point plugin Protocols (`irides.plugins`)
│   │   └── exporting/              # Markdown & OKF v0.2 artifact exporters
│   ├── db/
│   │   ├── connectors/             # mysql, postgres, sqlite, duckdb, dynamodb, mongodb, athena, trino
│   │   ├── models/                 # Instance, Schema, Table, Column, TableDescription
│   │   ├── catalog.py              # Public Catalog facade and iter_describe orchestration
│   │   ├── interface.py            # BaseConnector abstract class
│   │   ├── manager.py              # ConnectorManager (auto-discovers BaseConnector)
│   │   └── sql_utils.py
│   └── jobs/
│       ├── models/                 # ScanJob, ScanScope, ScanStatus, PrismJob, EnrichmentJob, EnrichmentJobScope
│       ├── job_store.py            # JobStore — Redis Stream queue for DB scans (requires [redis])
│       ├── prism_job_store.py      # PrismJobStore — Redis Stream queue for Prism scans
│       └── enrichment_job_store.py # EnrichmentJobStore — Redis Stream queue for Enrichment runs
└── db_connector/                   # Thin backward-compatible shim aliasing irides_core.*
```

---

## Key Modules

### `exporting/`

Provides multi-format artifact exporting decoupled from raw JSON storage.

- **`ExportFormat`**: Enum supporting `markdown` and `okf` (Open Knowledge Format v0.2). Both are generated by default.
- **`ExportOptions`**: Controls derived artifact generation (`formats: list[ExportFormat]`, `preformat: bool = True`). Supports explicit opt-out.
- **`preformatters.py` (`essential_record`)**: Deterministic view preserving identity, summary, columns, keys, relations, unique non-primary indexes, partitions, owner, tags, and status. Excludes non-unique secondary indexes and verbose internal metadata to optimize token usage.
- **`markdown.py` (`render_markdown`)**: Generates structured Markdown tables, keys, relationships, indexes, and partitions. Used both for standalone Markdown output and the OKF document body.
- **`okf.py` (`render_okf`)**: Renders OKF v0.2 documents with YAML frontmatter (`type: Database Table`, title, description, tags, generator metadata, identifiers) followed by the Markdown body.
- **`artifact_store.py` (`FileArtifactStore`)**: Persists artifacts to `STORAGE_EXPORT_DIR` via atomic writes with readable `0644` file permissions, maintaining separate directory hierarchies for Markdown and OKF catalog bundles with an auto-updated `index.md`:

```text
storage/
  metadata/
    database/{config}/{instance}/{schema}/{table}.json
    prism/{project}/database.json & technology.json
  exports/
    markdown/
      database/{config}/{instance}/{schema}/{table}.md
      prism/{project}/database.md & technology.md
    okf/
      catalog/
        index.md                                  # Unified OKF v0.2 index (DB tables + Prism projects)
        database/{config}/{instance}/{schema}/{table}.md
        prism/{project}/database.md & technology.md
```

### `storage.py`

Provides metadata persistence abstractions.

**`BaseMetadataStore`** — abstract interface with these methods:

| Method | Description |
|---|---|
| `save_table_metadata(...)` | Write or update a table metadata document and derived exports |
| `get_table_metadata(...)` | Read a stored document by config+instance+schema+table |
| `list_instances(page, page_size)` | Paginated list of all stored instance names |
| `list_databases(instance_name, page, page_size)` | Paginated list of databases for an instance |
| `list_tables_metadata(instance_name, database_name, page, page_size)` | Paginated list of table names |
| `find_table_metadata(instance_name, database_name, table_name)` | Look up a table across all configs |
| `update_table_metadata(instance_name, database_name, table_name, payload)` | Merge custom fields into a stored document and regenerate exports |

**`FileMetadataStore`** (default) — saves canonical JSON documents under `STORAGE_METADATA_DIR` and derived exports under `STORAGE_EXPORT_DIR`.

Key behaviours:

- **Decoupled export pipeline**: Derived artifacts (Markdown, OKF) are generated independently from JSON metadata persistence. Setting `save_metadata=False` still generates exports if requested.
- **Automatic export regeneration**: Calling `update_table_metadata` to merge human annotations automatically regenerates the corresponding Markdown and OKF documents.
- **Custom field carry-forward**: any key not in `_SYSTEM_KEYS` (`metadata_key`, `config_name`, `instance_name`, `schema_name`, `table_name`, `updated_at`, `schema_description`, `ai_documentation`) is preserved across re-describe calls. Human-added fields such as `owner`, `tags`, and `notes` survive schema refreshes.
- **`only_if_changed`**: when `True`, `save_table_metadata` skips the JSON write if `schema_description` is identical to the stored version, leaving `updated_at` and human annotations untouched, but exports the current state.
- **Unified Prism Catalog Support (`save_prism_metadata`)**: `FileMetadataStore` and `FileArtifactStore` also persist canonical Prism JSON (`metadata/prism/...`) and render Markdown + OKF v0.2 exports (`exports/okf/catalog/prism/...`), indexing both DB tables and Prism project docs in the single `okf/catalog/index.md` without exposing `"prism"` in DB `list_configs()` / `list_instances()` queries. `export_all()` regenerates both DB and Prism exports offline.
- **`ai_documentation` preservation**: if `ai_documentation=None` is passed, the existing stored AI doc is kept rather than overwritten.
- **Protected fields**: `update_table_metadata` silently ignores `metadata_key`, `config_name`, `instance_name`, `schema_name`, `table_name`, and `updated_at` in the payload — these are always managed by the system.

Paginated list responses follow this envelope:
```json
{
  "items": ["name_a", "name_b"],
  "total": 2,
  "page": 1,
  "page_size": 20,
  "pages": 1
}
```

Use `get_metadata_store()` (factory function) to obtain the configured store. Set `METADATA_STORE_TYPE=file` (default) or extend with future backends (`s3`, `athena`).

### `ai_service.py`
- **`LLMJsonClient`**: Fault-tolerant LiteLLM wrapper (`LITELLM_MODEL`, default `gpt-4o-mini`) with `has_api_key()` check, retry/timeout handling, and JSON parsing. Shared by both DB documentation and `irides-prism` AI refinement.
- **`AIDocumentationService`**: Uses `LLMJsonClient` to generate high-level domain summaries and column descriptions. When requested, generated docs are attached to `TableDescription.ai_documentation` with `ai_generation_status`; failures also include `ai_generation_error`.

### `discovery_models.py`
Shared Pydantic v2 contracts for `irides-prism` (`DatabaseOverviewDoc`, `DatabaseInstanceEntry`, `DatabaseTableEntry`, `TechnologyOverviewDoc`, `TechnologyEntry`, `DocEvidence`) and `is_irides_managed_markdown()` ownership validation (`managed_by: irides`).

### `enrichment_models.py`
Shared Pydantic v2 contracts for `irides-enrichment` (`EnrichmentMetadata`, `WikiLink`, `InferredRelationship`), storing explicit/implicit PK/FK relationships, bidirectional `used_by` backlinks, and optional AI TL;DR summaries alongside canonical table and Prism metadata.

### `plugins.py`
Entry-point plugin Protocols (`IridesPlugin`, `DiscoveryPlugin`, `EnrichmentPlugin`) and discovery helpers (`discover_plugins()`, `list_plugin_names()`, `load_plugin()`) using the `irides.plugins` entry-point group.

### `prism_job_store.py`
Manages asynchronous `irides-prism` multi-repository discovery jobs via Redis (`{prefix}:prism:queue`, `prism-workers` consumer group, `{prefix}:prism:job:{job_id}`, `{prefix}:prism:results:{job_id}`, and `{prefix}:prism:jobs`).

### `enrichment_job_store.py`
Manages asynchronous `irides-enrichment` catalog and Markdown enrichment jobs via Redis (`{prefix}:enrichment:queue`, `enrichment-workers` consumer group, `{prefix}:enrichment:job:{job_id}`, `{prefix}:enrichment:results:{job_id}`, and `{prefix}:enrichment:jobs`).

### `configurations.py`
Reads database connection parameters from environment variables. `DB_TARGETS` supports any number of named targets for any connector.
- Supports `DB_CONFIG_FILE` environment variable to explicitly specify the path to a container `.env` file (e.g. `/app/api/.env`), falling back to default `load_dotenv()` discovery when unset.
- Target names from `DB_TARGETS` become API/MCP `config_name` values. Example: `DB_TARGETS=sales_mysql,analytics_pg` creates `sales_mysql` and `analytics_pg` configurations.
- Each target uses `DB_TARGET_<TARGET_KEY>_*` variables, where `<TARGET_KEY>` is the uppercased target name with non-alphanumeric characters replaced by underscores.
- Exact required and optional keys for each connector type are documented in the root README under **DB Configuration & Activation**.

### `config_service.py`
Wraps `ConnectorManager` and `configurations`.
- **`list_instances(config_name, no_cache)`**: Uniformly lists instances for both multi-host configurations (MySQL/MariaDB) and flat configurations (Athena, DynamoDB, Trino, MongoDB, SQLite).
- **`resolve_instance_names(config_name, instance_name, no_cache)`**: Returns `[instance_name]` if specified, or all discovered instances if `instance_name` is `None`.
- **`_get_hosts(config_name)`**: Returns explicitly configured hosts, correctly recognizing both multi-host `hosts` collections and flat `host` parameters.
- **`configuration_matches_instance(config_name, instance_name, no_cache)`**: Checks whether an instance belongs to a given configuration.
- **`resolve_configurations_for_instance(instance_name, no_cache)`**: Centralized lookup returning all configuration names matching a target instance.
- **`_get_connector_for_host(config_name, host)`**: Returns the connector for a specific host, falling back to flat connection parameters when static host definitions are omitted.

### `cache_manager.py`
Redis-backed cache for introspection results. All keys are prefixed with `CACHE_KEY_PREFIX`. Cache can be bypassed per-call with `no_cache=True`.

### `job_store.py`
Manages async scan jobs via Redis:
- **Stream** (`{prefix}:scan:queue`) — job queue for workers (`scan-workers` consumer group). Stream messages serialize `export_formats` and `export_preformat` parameters.
- **Hash** (`{prefix}:scan:job:{job_id}`) — job metadata, scope, and status. Supports deserialization of new export options with backwards compatibility for legacy jobs with `save_markdown`.
- **List** (`{prefix}:scan:results:{job_id}`) — serialized `TableDescription` results with automatic TTL extensions on writes.
- **Sorted Set** (`{prefix}:scan:jobs`) — job index ordered by creation timestamp, automatically pruned of entries older than `RESULTS_TTL` via `zremrangebyscore` to prevent Redis memory leaks.

---

## Supported Databases

| Database | Connector Type | Configuration Style |
|---|---|---|
| MySQL / MariaDB | `mysql` | Named `DB_TARGETS` |
| PostgreSQL | `postgres` | Named `DB_TARGETS` |
| SQLite | `sqlite` | Named `DB_TARGETS` |
| DuckDB | `duckdb` | Named `DB_TARGETS` |
| Amazon DynamoDB | `dynamodb` | Named `DB_TARGETS` |
| Amazon Athena | `athena` | Named `DB_TARGETS` |
| MongoDB | `mongodb` | Named `DB_TARGETS` |
| Trino | `trino` | Named `DB_TARGETS` |
| Presto | `presto` | Named `DB_TARGETS` |

---

## Environment Variables

Copy `.env.example` to `.env` and configure as needed.

| Variable | Default | Description |
|---|---|---|
| `REDIS_HOST` | `localhost` | Redis host |
| `REDIS_PORT` | `6379` | Redis port |
| `REDIS_DB` | `0` | Redis database index |
| `REDIS_TTL_SECONDS` | `86400` | Introspection cache TTL (1 day) |
| `CACHE_KEY_PREFIX` | `irides` | Prefix for all Redis keys |
| `SCAN_RESULTS_TTL_SECONDS` | `604800` | Scan result retention in Redis (7 days) |
| `DB_CONFIG_FILE` | *(none)* | Explicit path to `.env` configuration file |
| `DB_TARGETS` | *(none)* | Comma-separated list of named DB targets |
| `STORAGE_METADATA_DIR` | `storage/metadata` | Metadata JSON output directory |
| `STORAGE_EXPORT_DIR` | `storage/exports` | Directory for generated Markdown and OKF exports |
| `STORAGE_PRISM_DIR` | `storage/prism` | Default vault output directory for Prism worker |
| `METADATA_STORE_TYPE` | `file` | Metadata store backend (`file`; `s3`/`athena` planned) |
| `LITELLM_MODEL` | `gpt-4o-mini` | LiteLLM model for AI documentation |
| `LITELLM_API_KEY` | *(none)* | Optional provider API key override |
| `LITELLM_API_BASE` | *(none)* | Optional custom LiteLLM API base URL |
| `GITHUB_TOKEN` | *(none)* | GitHub token for private clones and PR creation |
| `GITLAB_TOKEN` | *(none)* | GitLab token for private clones and MR creation |
| `IRIDES_GIT_TOKEN` | *(none)* | Generic fallback Git token for Prism |

DB activation vars — see [root README](https://github.com/GianAndreaSechi/irides/blob/main/README.md#db-configuration--activation).

---

## Installation

```bash
# Install from PyPI (SQLite built-in, no Redis required):
pip install irides-core

# With optional database connectors, AI documentation, or Redis extras:
pip install "irides-core[postgres,mysql,duckdb,mongo,aws,trino,ai,redis]"
# Or install everything:
pip install "irides-core[all]"

# Or editable install from this monorepo:
pip install -e ./packages/core
```

---

## Adding a New Connector

1. Create `packages/core/irides_core/db/connectors/mydb.py` implementing `BaseConnector`.
2. Export it from `packages/core/irides_core/db/connectors/__init__.py`.
3. Add its activation env var and config block to `packages/core/irides_core/config/configurations.py`.

The `ConnectorManager` automatically discovers all classes that extend `BaseConnector`.
