Skip to main content

lenso_postgres_kit/
error.rs

1use thiserror::Error;
2
3use crate::PlanError;
4
5/// A `PostgreSQL` schema lifecycle operation failed without applying a fallback.
6#[derive(Debug, Error)]
7pub enum PostgresKitError {
8    /// The authored schema plan is invalid.
9    #[error(transparent)]
10    InvalidPlan(#[from] PlanError),
11    /// Connection setup or a database operation failed.
12    #[error("PostgreSQL operation `{operation}` failed")]
13    Database {
14        operation: &'static str,
15        #[source]
16        source: sqlx::Error,
17    },
18    /// The schema exists, but was not created by this lifecycle protocol.
19    #[error("schema `{schema}` exists without the Lenso migration ledger")]
20    UnmanagedSchema { schema: String },
21    /// The configured database role does not own the Module schema.
22    #[error("schema `{schema}` is owned by `{owner}`, not current role `{current_role}`")]
23    OwnershipMismatch {
24        schema: String,
25        owner: String,
26        current_role: String,
27    },
28    /// Runtime preparation requires an explicit setup operation first.
29    #[error("schema `{schema}` has not been set up")]
30    SetupRequired { schema: String },
31    /// Runtime preparation never applies pending migrations.
32    #[error(
33        "schema `{schema}` is at version {current}; explicit upgrade to {expected} is required"
34    )]
35    UpgradeRequired {
36        schema: String,
37        current: u64,
38        expected: u64,
39    },
40    /// Applied migration history no longer matches the immutable authored plan.
41    #[error("schema `{schema}` migration history diverged at version {version}")]
42    HistoryDiverged { schema: String, version: u64 },
43    /// The database is newer than the linked Module implementation.
44    #[error("schema `{schema}` is at version {actual}, newer than supported version {expected}")]
45    SchemaAhead {
46        schema: String,
47        actual: u64,
48        expected: u64,
49    },
50}
51
52impl PostgresKitError {
53    pub(crate) const fn database(operation: &'static str, source: sqlx::Error) -> Self {
54        Self::Database { operation, source }
55    }
56}
57
58/// Result of explicitly setting up an owned schema.
59#[derive(Clone, Copy, Debug, PartialEq, Eq)]
60pub enum SetupOutcome {
61    /// A new schema was created at the current authored version.
62    Created { version: u64, applied: usize },
63    /// The existing managed schema already matched the authored plan.
64    AlreadyCurrent { version: u64 },
65}
66
67/// Result of explicitly upgrading an owned schema.
68#[derive(Clone, Copy, Debug, PartialEq, Eq)]
69pub enum UpgradeOutcome {
70    /// Pending migrations were applied atomically.
71    Applied { from: u64, to: u64, applied: usize },
72    /// The existing managed schema already matched the authored plan.
73    AlreadyCurrent { version: u64 },
74}