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 scope;
63pub mod services;
64pub mod squash_baseline;
65
66pub use extension::DatabaseExtension;
67
68pub use models::{
69    ArtifactId, ClientId, ColumnInfo, ContentId, ContextId, DatabaseInfo, DatabaseQuery,
70    DatabaseTransaction, DbValue, ExecutionStepId, FileId, FromDatabaseRow, FromDbValue, IndexInfo,
71    JsonRow, LogId, QueryResult, QueryRow, QuerySelector, SessionId, SkillId, TableInfo, TaskId,
72    ToDbValue, TokenId, TraceId, UserId, parse_database_datetime,
73};
74
75pub use scope::{ConnectionScopeProvider, ScopeError, ScopeSetting, SharedScopeProvider};
76pub use services::{
77    BoxFuture, Database, DatabaseCliDisplay, DatabaseExt, DatabaseProvider, DatabaseProviderExt,
78    DbPool, PoolConfig, PostgresProvider, SqlExecutor, begin_scoped, with_scoped_transaction,
79    with_scoped_transaction_raw, with_transaction, with_transaction_raw, with_transaction_retry,
80};
81pub use systemprompt_models::RequestScope;
82
83pub use error::{DatabaseResult, RepositoryError};
84pub use lifecycle::{
85    AppliedMigration, ChecksumDrift, ExtensionMigrationStatus, MarkAppliedOutcome, MigrationConfig,
86    MigrationResult, MigrationService, MigrationStatus, PendingMigration, RepairResult, SquashPlan,
87    install_extension_schemas, install_extension_schemas_full,
88    install_extension_schemas_with_config, validate_column_exists, validate_database_connection,
89    validate_table_exists,
90};
91pub use repository::{
92    CleanupRepository, CreateServiceInput, PgDbPool, ServiceConfig, ServiceRepository,
93};
94pub use squash_baseline::{SquashBaselineError, SquashBaselineService};
95
96pub use admin::{
97    AdminSql, AdminSqlError, DEFAULT_READONLY_ROW_LIMIT, DatabaseAdminService, IdentifierError,
98    QueryExecutor, QueryExecutorError, SafeIdentifier,
99};
100pub use sqlx::types::Json;
101pub use sqlx::{PgPool, Pool, Postgres, Transaction};
102
103use systemprompt_traits::DatabaseHandle;
104
105impl DatabaseHandle for Database {
106    fn is_connected(&self) -> bool {
107        true
108    }
109
110    fn as_any(&self) -> &dyn std::any::Any {
111        self
112    }
113}