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