Skip to main content

systemprompt_database/
lib.rs

1//! # systemprompt-database
2//!
3//! `PostgreSQL` infrastructure for systemprompt.io: a thin `SQLx`-backed pool,
4//! generic repository traits, dynamic-query primitives for admin tooling, and
5//! lifecycle helpers (schema installation, extension migrations, validation).
6//!
7//! ## Public API surface
8//!
9//! - [`Database`] / [`DbPool`] — owned pool wrapper with optional split
10//!   read/write providers.
11//! - [`DatabaseProvider`] — dyn-safe trait abstracting
12//!   query/execute/transaction primitives across providers (currently only
13//!   `PostgreSQL`).
14//! - [`PostgresProvider`] — the `PostgreSQL` implementation.
15//! - [`RepositoryError`] / [`DatabaseResult`] — canonical typed error/result
16//!   returned from non-trait public APIs.
17//! - [`MigrationService`], [`install_extension_schemas`],
18//!   [`install_extension_schemas_full`] — lifecycle helpers driving
19//!   extension-supplied DDL.
20//! - [`DatabaseAdminService`], [`QueryExecutor`], [`AdminSql`],
21//!   [`SafeIdentifier`] — admin/introspection layer used by the CLI.
22//!
23//! ## Feature flags
24//!
25//! This crate currently has no Cargo features; everything compiles
26//! unconditionally. The `[package.metadata.docs.rs]` block is in place so
27//! `--all-features` documentation builds remain stable as features are added.
28//!
29//! ## sqlx allowlist
30//!
31//! Static SQL goes through the compile-time-verified `sqlx::query!` /
32//! `query_as!` / `query_scalar!` macros. Runtime/dynamic SQL is contained to
33//! two paths whose contract is dynamic SQL by design and that are documented in
34//! the workspace allowlist (`ci/check-sqlx.sh`, `instructions/prompt/rust.md`):
35//!
36//! - `src/admin/` — admin CLI surfaces (introspection, restricted query
37//!   executor) where the SQL is the user input.
38//! - `src/services/postgres/` — the dyn-safe `DatabaseProvider` implementation,
39//!   transaction wrapper, type-erased helpers and `PostgreSQL` schema
40//!   introspection.
41//!
42//! Every other call site uses verified macros.
43
44pub mod admin;
45pub mod error;
46pub mod extension;
47pub mod lifecycle;
48pub mod models;
49#[macro_use]
50pub mod repository;
51pub mod services;
52
53pub use extension::DatabaseExtension;
54
55pub use models::{
56    ArtifactId, ClientId, ColumnInfo, ContentId, ContextId, DatabaseInfo, DatabaseQuery,
57    DatabaseTransaction, DbValue, ExecutionStepId, FileId, FromDatabaseRow, FromDbValue, IndexInfo,
58    JsonRow, LogId, QueryResult, QueryRow, QuerySelector, SessionId, SkillId, TableInfo, TaskId,
59    ToDbValue, TokenId, TraceId, UserId, parse_database_datetime,
60};
61
62pub use services::{
63    BoxFuture, Database, DatabaseCliDisplay, DatabaseExt, DatabaseProvider, DatabaseProviderExt,
64    DbPool, PostgresProvider, SqlExecutor, with_transaction, with_transaction_raw,
65    with_transaction_retry,
66};
67
68pub use error::{DatabaseResult, RepositoryError};
69pub use lifecycle::{
70    AppliedMigration, MigrationConfig, MigrationResult, MigrationService, MigrationStatus,
71    install_extension_schemas, install_extension_schemas_full,
72    install_extension_schemas_with_config, validate_column_exists, validate_database_connection,
73    validate_table_exists,
74};
75pub use repository::{
76    CleanupRepository, CreateServiceInput, DatabaseInfoRepository, PgDbPool, ServiceConfig,
77    ServiceRepository,
78};
79
80pub use admin::{
81    AdminSql, AdminSqlError, DEFAULT_READONLY_ROW_LIMIT, DatabaseAdminService, IdentifierError,
82    QueryExecutor, QueryExecutorError, SafeIdentifier,
83};
84pub use sqlx::types::Json;
85pub use sqlx::{PgPool, Pool, Postgres, Transaction};
86
87use systemprompt_traits::DatabaseHandle;
88
89impl DatabaseHandle for Database {
90    fn is_connected(&self) -> bool {
91        true
92    }
93
94    fn as_any(&self) -> &dyn std::any::Any {
95        self
96    }
97}