footing 0.4.4

Feature-gated foundations for Rust backend services
Documentation
//! Database-independent SQLx migration failure classification.

use sqlx::migrate::MigrateError;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
/// Classification of an SQLx migration failure.
pub enum MigrationFailureKind {
    /// The migration runner failed before identifying a migration version.
    Execute,
    /// A specific migration failed while executing.
    ExecuteMigration {
        /// The affected migration version.
        version: i64,
    },
    /// Migration sources could not be resolved.
    Source,
    /// An applied migration is missing from the resolved source.
    VersionMissing {
        /// The affected migration version.
        version: i64,
    },
    /// An applied migration no longer matches its recorded checksum.
    VersionMismatch {
        /// The affected migration version.
        version: i64,
    },
    /// A requested migration is not present in the source.
    VersionNotPresent {
        /// The affected migration version.
        version: i64,
    },
    /// A migration is older than the latest applied migration.
    VersionTooOld {
        /// The affected migration version.
        version: i64,
        /// The latest applied migration version.
        latest: i64,
    },
    /// A migration is newer than the latest resolved migration.
    VersionTooNew {
        /// The affected migration version.
        version: i64,
        /// The latest resolved migration version.
        latest: i64,
    },
    /// A non-transactional migration was only partially applied.
    Dirty {
        /// The affected migration version.
        version: i64,
    },
    /// The database driver does not support the requested migration operation.
    Unsupported,
    /// The failure is not classified by this version of Footing.
    Unknown,
}

impl MigrationFailureKind {
    #[must_use]
    /// Classifies an SQLx migration error without retaining its source details.
    pub fn from_error(error: &MigrateError) -> Self {
        match error {
            MigrateError::Execute(_) => Self::Execute,
            MigrateError::ExecuteMigration(_, version) => {
                Self::ExecuteMigration { version: *version }
            }
            MigrateError::Source(_) => Self::Source,
            MigrateError::VersionMissing(version) => Self::VersionMissing { version: *version },
            MigrateError::VersionMismatch(version) => Self::VersionMismatch { version: *version },
            MigrateError::VersionNotPresent(version) => {
                Self::VersionNotPresent { version: *version }
            }
            MigrateError::VersionTooOld(version, latest) => Self::VersionTooOld {
                version: *version,
                latest: *latest,
            },
            MigrateError::VersionTooNew(version, latest) => Self::VersionTooNew {
                version: *version,
                latest: *latest,
            },
            #[allow(deprecated)]
            MigrateError::ForceNotSupported | MigrateError::InvalidMixReversibleAndSimple => {
                Self::Unsupported
            }
            MigrateError::Dirty(version) => Self::Dirty { version: *version },
            _ => Self::Unknown,
        }
    }
}