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//!
51//! Copyright (c) systemprompt.io — Business Source License 1.1.
52//! See <https://systemprompt.io> for licensing details.
53
54pub mod admin;
55pub mod error;
56pub mod extension;
57pub mod lifecycle;
58pub mod models;
59#[macro_use]
60pub mod repository;
61pub mod resilience;
62pub mod services;
63pub mod squash_baseline;
64
65pub use extension::DatabaseExtension;
66
67pub use models::{
68    ArtifactId, ClientId, ColumnInfo, ContentId, ContextId, DatabaseInfo, DatabaseQuery,
69    DatabaseTransaction, DbValue, ExecutionStepId, FileId, FromDatabaseRow, FromDbValue, IndexInfo,
70    JsonRow, LogId, QueryResult, QueryRow, QuerySelector, SessionId, SkillId, TableInfo, TaskId,
71    ToDbValue, TokenId, TraceId, UserId, parse_database_datetime,
72};
73
74pub use services::{
75    BoxFuture, Database, DatabaseCliDisplay, DatabaseExt, DatabaseProvider, DatabaseProviderExt,
76    DbPool, PoolConfig, PostgresProvider, SqlExecutor, with_transaction, with_transaction_raw,
77    with_transaction_retry,
78};
79
80pub use error::{DatabaseResult, RepositoryError};
81pub use lifecycle::{
82    AppliedMigration, ChecksumDrift, ExtensionMigrationStatus, MarkAppliedOutcome, MigrationConfig,
83    MigrationResult, MigrationService, MigrationStatus, PendingMigration, RepairResult, SquashPlan,
84    install_extension_schemas, install_extension_schemas_full,
85    install_extension_schemas_with_config, validate_column_exists, validate_database_connection,
86    validate_table_exists,
87};
88pub use repository::{
89    CleanupRepository, CreateServiceInput, PgDbPool, ServiceConfig, ServiceRepository,
90};
91pub use squash_baseline::{SquashBaselineError, SquashBaselineService};
92
93pub use admin::{
94    AdminSql, AdminSqlError, DEFAULT_READONLY_ROW_LIMIT, DatabaseAdminService, IdentifierError,
95    QueryExecutor, QueryExecutorError, SafeIdentifier,
96};
97pub use sqlx::types::Json;
98pub use sqlx::{PgPool, Pool, Postgres, Transaction};
99
100use systemprompt_traits::DatabaseHandle;
101
102impl DatabaseHandle for Database {
103    fn is_connected(&self) -> bool {
104        true
105    }
106
107    fn as_any(&self) -> &dyn std::any::Any {
108        self
109    }
110}