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//! - [`DatabaseResult`] — result alias over the workspace's single
16//!   [`systemprompt_traits::RepositoryError`].
17//! - [`MigrationService`], [`install_extension_schemas_full`] — lifecycle
18//!   helpers driving extension-supplied DDL.
19//! - [`DatabaseAdminService`], [`QueryExecutor`], [`AdminSql`],
20//!   [`SafeIdentifier`] — admin/introspection layer used by the CLI.
21//! - [`resilience`] — domain-agnostic resilience primitives
22//!   ([`resilience::ResilienceGuard`], [`resilience::CircuitBreaker`],
23//!   [`resilience::Bulkhead`], [`resilience::retry_async`]) wrapping outbound
24//!   calls; the crate's own connection and transaction retries run on them.
25//!
26//! ## Feature flags
27//!
28//! This crate currently has no Cargo features; everything compiles
29//! unconditionally. The `[package.metadata.docs.rs]` block is in place so
30//! `--all-features` documentation builds remain stable as features are added.
31//!
32//! ## sqlx allowlist
33//!
34//! Static SQL goes through the compile-time-verified `sqlx::query!` /
35//! `query_as!` / `query_scalar!` macros. Runtime/dynamic SQL is contained to
36//! two paths whose contract is dynamic SQL by design; the `lint-sqlx` gate
37//! allows runtime `sqlx::query` only there:
38//!
39//! - `src/admin/` — admin CLI surfaces (introspection, restricted query
40//!   executor) where the SQL is the user input.
41//! - `src/services/postgres/` — the dyn-safe `DatabaseProvider` implementation,
42//!   transaction wrapper, type-erased helpers and `PostgreSQL` schema
43//!   introspection.
44//!
45//! Every other call site uses verified macros.
46//!
47//! Copyright (c) systemprompt.io — Business Source License 1.1.
48//! See <https://systemprompt.io> for licensing details.
49
50pub mod admin;
51pub mod error;
52pub mod extension;
53pub mod lifecycle;
54pub mod models;
55#[macro_use]
56pub mod repository;
57pub mod resilience;
58pub mod services;
59
60pub use extension::DatabaseExtension;
61
62pub use models::{
63    ColumnInfo, DatabaseInfo, DatabaseQuery, DatabaseTransaction, DbValue, FromDbValue, IndexInfo,
64    JsonRow, QueryResult, QueryRow, QuerySelector, TableInfo, ToDbValue, parse_database_datetime,
65};
66
67pub use services::{
68    BoxFuture, Database, DatabaseCliDisplay, DatabaseExt, DatabaseProvider, DbPool, PoolConfig,
69    PostgresProvider, SqlExecutor, with_transaction_retry,
70};
71
72pub use error::DatabaseResult;
73pub use lifecycle::{
74    AppliedMigration, BOOTSTRAP_ADVISORY_LOCK_KEY, BaselineStamp, BootstrapLockGuard,
75    ChecksumDrift, DeferredForeignKey, ExpensiveStatement, ExtensionMigrationStatus,
76    FkDeferralError, ForeignKeyDrift, FreshnessCheck, HOT_TABLES, MarkAppliedOutcome,
77    MigrationConfig, MigrationCost, MigrationResult, MigrationService, MigrationStatus,
78    OrphanMigrationLedger, OrphanedMigration, PendingMigration, RepairResult, ReplicaStatus,
79    SchemaCurrency, SchemaInstallReport, SchemaResidue, SlotCollision, SplitCreateTable,
80    TombstonedSlot, UndeclaredTable, audit_migration_cost, audit_one, audit_schema_residue,
81    check_migration_references, check_trigger_routines, install_extension_schemas_full,
82    is_retirement, replica_status, schema_currency, split_create_table_foreign_keys,
83    validate_column_exists, validate_database_connection, validate_table_exists,
84    validate_write_pool_is_primary,
85};
86pub use repository::{
87    CreateServiceInput, INSTANCE_CLAIM_CLASS, InstanceClaim, InstanceClaimError, PgDbPool,
88    ServiceConfig, ServiceModule, ServiceRepository, ServiceStatus, UpsertServiceProcessInput,
89};
90
91pub use admin::{
92    AdminSql, AdminSqlError, DEFAULT_READONLY_ROW_LIMIT, DatabaseAdminService, IdentifierError,
93    QueryExecutor, QueryExecutorError, SafeIdentifier,
94};
95
96use systemprompt_traits::DatabaseHandle;
97
98impl DatabaseHandle for Database {
99    fn is_connected(&self) -> bool {
100        !self.pool().is_closed() && !self.write_pool().is_closed()
101    }
102
103    fn as_any(&self) -> &dyn std::any::Any {
104        self
105    }
106}