Skip to main content

dovecote_sqlx_postgres/
lib.rs

1//! PostgreSQL schema and SQLx boundary for Dovecote.
2//!
3//! This crate publishes versioned PostgreSQL migration artifacts and implements
4//! the caller-transaction-bound enqueue, schema verification, leased lifecycle
5//! operations, and live and finite snapshot paging for Dovecote. The locking,
6//! database-time, fencing, and rollback contracts are covered by repository
7//! tests; release advertisement remains subject to the published support matrix
8//! and release gates.
9#![warn(missing_docs)]
10
11mod delivery_state;
12mod enqueue;
13mod error;
14mod finalize;
15mod hydrate;
16mod import;
17mod lifecycle;
18mod lifecycle_mutation;
19mod migration;
20mod page;
21mod rls;
22mod schema;
23mod scope;
24
25pub use error::{
26    ClaimError, EnqueueError, FinalizeError, ImportError, MutationError, PageError, SchemaError,
27    TransientKind,
28};
29#[allow(deprecated)]
30pub use migration::{
31    CrateVersion, LEGACY_MIGRATION, MIGRATIONS, Migration, MigrationCompatibility,
32    MigrationCompatibilityError, SCHEMA_VERSION, V1_TENANT_ACTIVATE_SQL, V1_TENANT_ACTIVATE_V2_SQL,
33    V1_TENANT_PREPARE_SQL,
34};
35pub use page::SnapshotPager;
36pub use rls::{RLS_PROFILE_SQL, bind_tenant};
37pub use schema::check_schema;
38
39use sqlx::PgPool;
40
41pub use scope::{AdminDovecote, TenantDovecote};
42
43/// PostgreSQL adapter for Dovecote's durable event and delivery schema.
44#[derive(Clone)]
45pub struct PostgresDovecote {
46    pool: PgPool,
47}
48
49impl PostgresDovecote {
50    /// Creates an adapter using the supplied SQLx pool.
51    pub fn new(pool: PgPool) -> Self {
52        Self { pool }
53    }
54
55    /// Borrows the pool used by this adapter.
56    pub fn pool(&self) -> &PgPool {
57        &self.pool
58    }
59
60    /// Creates a handle whose ordinary operations are restricted to `tenant`.
61    pub fn for_tenant(&self, tenant: dovecote::TenantId) -> TenantDovecote {
62        TenantDovecote::new(self.pool.clone(), tenant)
63    }
64
65    /// Creates an explicit all-tenant administrative handle.
66    ///
67    /// This handle does not provide authorization. Applications must construct
68    /// it only around a separately authorized worker or operator pool.
69    pub fn admin(&self) -> AdminDovecote {
70        AdminDovecote::new(self.pool.clone())
71    }
72
73    /// Verifies that the pool's current PostgreSQL schema satisfies Dovecote
74    /// migration version 2.
75    pub async fn check_schema(&self) -> Result<(), SchemaError> {
76        check_schema(&self.pool).await
77    }
78}