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//! - [`SquashBaselineService`] — locates an extension's source crate in the
23//!   workspace layout and writes squashed migration baselines; filesystem-only,
24//!   with its own [`SquashBaselineError`].
25//! - [`resilience`] — domain-agnostic resilience primitives
26//!   ([`resilience::ResilienceGuard`], [`resilience::CircuitBreaker`],
27//!   [`resilience::Bulkhead`], [`resilience::retry_async`]) wrapping outbound
28//!   calls; the crate's own connection and transaction retries run on them.
29//!
30//! ## Feature flags
31//!
32//! This crate currently has no Cargo features; everything compiles
33//! unconditionally. The `[package.metadata.docs.rs]` block is in place so
34//! `--all-features` documentation builds remain stable as features are added.
35//!
36//! ## sqlx allowlist
37//!
38//! Static SQL goes through the compile-time-verified `sqlx::query!` /
39//! `query_as!` / `query_scalar!` macros. Runtime/dynamic SQL is contained to
40//! two paths whose contract is dynamic SQL by design and that are documented in
41//! the workspace allowlist (`ci/check-sqlx.sh`, `instructions/prompt/rust.md`):
42//!
43//! - `src/admin/` — admin CLI surfaces (introspection, restricted query
44//!   executor) where the SQL is the user input.
45//! - `src/services/postgres/` — the dyn-safe `DatabaseProvider` implementation,
46//!   transaction wrapper, type-erased helpers and `PostgreSQL` schema
47//!   introspection.
48//!
49//! Every other call site uses verified macros.
50
51pub mod admin;
52pub mod error;
53pub mod extension;
54pub mod lifecycle;
55pub mod models;
56#[macro_use]
57pub mod repository;
58pub mod resilience;
59pub mod services;
60pub mod squash_baseline;
61
62pub use extension::DatabaseExtension;
63
64pub use models::{
65    ArtifactId, ClientId, ColumnInfo, ContentId, ContextId, DatabaseInfo, DatabaseQuery,
66    DatabaseTransaction, DbValue, ExecutionStepId, FileId, FromDatabaseRow, FromDbValue, IndexInfo,
67    JsonRow, LogId, QueryResult, QueryRow, QuerySelector, SessionId, SkillId, TableInfo, TaskId,
68    ToDbValue, TokenId, TraceId, UserId, parse_database_datetime,
69};
70
71pub use services::{
72    BoxFuture, Database, DatabaseCliDisplay, DatabaseExt, DatabaseProvider, DatabaseProviderExt,
73    DbPool, PoolConfig, PostgresProvider, SqlExecutor, with_transaction, with_transaction_raw,
74    with_transaction_retry,
75};
76
77pub use error::{DatabaseResult, RepositoryError};
78pub use lifecycle::{
79    AppliedMigration, ChecksumDrift, ExtensionMigrationStatus, MarkAppliedOutcome, MigrationConfig,
80    MigrationResult, MigrationService, MigrationStatus, PendingMigration, RepairResult, SquashPlan,
81    install_extension_schemas, install_extension_schemas_full,
82    install_extension_schemas_with_config, validate_column_exists, validate_database_connection,
83    validate_table_exists,
84};
85pub use repository::{
86    CleanupRepository, CreateServiceInput, PgDbPool, ServiceConfig, ServiceRepository,
87};
88pub use squash_baseline::{SquashBaselineError, SquashBaselineService};
89
90pub use admin::{
91    AdminSql, AdminSqlError, DEFAULT_READONLY_ROW_LIMIT, DatabaseAdminService, IdentifierError,
92    QueryExecutor, QueryExecutorError, SafeIdentifier,
93};
94pub use sqlx::types::Json;
95pub use sqlx::{PgPool, Pool, Postgres, Transaction};
96
97use systemprompt_traits::DatabaseHandle;
98
99impl DatabaseHandle for Database {
100    fn is_connected(&self) -> bool {
101        true
102    }
103
104    fn as_any(&self) -> &dyn std::any::Any {
105        self
106    }
107}