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#![forbid(unsafe_code)]
11
12mod delivery_state;
13mod enqueue;
14mod error;
15mod finalize;
16mod hydrate;
17mod import;
18mod lifecycle;
19mod lifecycle_mutation;
20mod migration;
21mod page;
22mod rls;
23mod schema;
24mod scope;
25
26pub use error::{
27    ClaimError, EnqueueError, FinalizeError, ImportError, MutationError, PageError, SchemaError,
28    TransientKind,
29};
30#[allow(
31    deprecated,
32    reason = "Preserve the published 0.2.0 migration symbol and its bytes."
33)]
34pub use migration::V1_TENANT_ACTIVATE_SQL;
35pub use migration::{
36    CrateVersion, LEGACY_MIGRATION, MIGRATIONS, Migration, MigrationCompatibility,
37    MigrationCompatibilityError, SCHEMA_VERSION, V1_TENANT_ACTIVATE_V2_SQL, V1_TENANT_PREPARE_SQL,
38};
39pub use page::SnapshotPager;
40pub use rls::{RLS_PROFILE_SQL, bind_tenant};
41pub use schema::check_schema;
42
43use sqlx::PgPool;
44
45pub use scope::{AdminDovecote, TenantDovecote};
46
47/// `PostgreSQL` adapter for Dovecote's durable event and delivery schema.
48#[derive(Clone)]
49pub struct PostgresDovecote {
50    pool: PgPool,
51}
52
53impl PostgresDovecote {
54    /// Creates an adapter using the supplied `SQLx` pool.
55    #[must_use]
56    pub const fn new(pool: PgPool) -> Self {
57        Self { pool }
58    }
59
60    /// Borrows the pool used by this adapter.
61    #[must_use]
62    pub const fn pool(&self) -> &PgPool {
63        &self.pool
64    }
65
66    /// Creates a handle whose ordinary operations are restricted to `tenant`.
67    #[must_use]
68    pub fn for_tenant(&self, tenant: dovecote::TenantId) -> TenantDovecote {
69        TenantDovecote::new(self.pool.clone(), tenant)
70    }
71
72    /// Creates an explicit all-tenant administrative handle.
73    ///
74    /// This handle does not provide authorization. Applications must construct
75    /// it only around a separately authorized worker or operator pool.
76    #[must_use]
77    pub fn admin(&self) -> AdminDovecote {
78        AdminDovecote::new(self.pool.clone())
79    }
80
81    /// Verifies that the pool's current `PostgreSQL` schema satisfies Dovecote
82    /// migration version 2.
83    ///
84    /// # Errors
85    /// Returns an error for an unsupported backend, missing or incompatible
86    /// migration markers, tables, constraints or indexes, or failed catalog reads.
87    pub async fn check_schema(&self) -> Result<(), SchemaError> {
88        check_schema(&self.pool).await
89    }
90}