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};
29pub use migration::{
30    CrateVersion, LEGACY_MIGRATION, MIGRATIONS, Migration, MigrationCompatibility,
31    MigrationCompatibilityError, SCHEMA_VERSION, V1_TENANT_ACTIVATE_SQL, V1_TENANT_PREPARE_SQL,
32};
33pub use page::SnapshotPager;
34pub use rls::{RLS_PROFILE_SQL, bind_tenant};
35pub use schema::check_schema;
36
37use sqlx::PgPool;
38
39pub use scope::{AdminDovecote, TenantDovecote};
40
41/// PostgreSQL adapter for Dovecote's durable event and delivery schema.
42#[derive(Clone)]
43pub struct PostgresDovecote {
44    pool: PgPool,
45}
46
47impl PostgresDovecote {
48    /// Creates an adapter using the supplied SQLx pool.
49    pub fn new(pool: PgPool) -> Self {
50        Self { pool }
51    }
52
53    /// Borrows the pool used by this adapter.
54    pub fn pool(&self) -> &PgPool {
55        &self.pool
56    }
57
58    /// Creates a handle whose ordinary operations are restricted to `tenant`.
59    pub fn for_tenant(&self, tenant: dovecote::TenantId) -> TenantDovecote {
60        TenantDovecote::new(self.pool.clone(), tenant)
61    }
62
63    /// Creates an explicit all-tenant administrative handle.
64    ///
65    /// This handle does not provide authorization. Applications must construct
66    /// it only around a separately authorized worker or operator pool.
67    pub fn admin(&self) -> AdminDovecote {
68        AdminDovecote::new(self.pool.clone())
69    }
70
71    /// Verifies that the pool's current PostgreSQL schema satisfies Dovecote
72    /// migration version 2.
73    pub async fn check_schema(&self) -> Result<(), SchemaError> {
74        check_schema(&self.pool).await
75    }
76}