migrio 1.1.0

A drop-in database migration library for PostgreSQL
Documentation
/// Represents a migration step: either up, down, or basic.
#[derive(Debug, PartialEq)]
pub enum MigrateStep {
    /// Represents `add` or `update` steps in a reversible migration
    ///
    /// Each `Up` step should have a corresponding `Down` step.
    Up,
    /// Represents `remove` or `undo` steps in a reversible migration
    ///
    /// Each `Down` step should have a corresponding `Up` step.
    Down,
    /// Single file migration with no specific direction
    Basic,
}

impl MigrateStep {
    /// Returns the migration step from a filename
    pub fn from_filename(filename: &str) -> Self {
        if filename.ends_with(MigrateStep::Up.extension()) {
            Self::Up
        } else if filename.ends_with(MigrateStep::Down.extension()) {
            Self::Down
        } else {
            Self::Basic
        }
    }

    /// Returns the file extension for the migration step
    pub fn extension(&self) -> &str {
        match self {
            Self::Up => ".up.sql",
            Self::Down => ".down.sql",
            Self::Basic => ".sql",
        }
    }

    /// Returns true if the migration step is a down migration
    pub fn is_down_migration(&self) -> bool {
        matches!(self, Self::Down)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_migrate_step_up() {
        let filename = "0001_create_users.up.sql";
        let step = MigrateStep::from_filename(filename);
        assert_eq!(step, MigrateStep::Up);
        assert_eq!(step.extension(), ".up.sql");
        assert!(!step.is_down_migration());
    }

    #[test]
    fn test_migrate_step_down() {
        let filename = "0002_create_users.down.sql";
        let step = MigrateStep::from_filename(filename);
        assert_eq!(step, MigrateStep::Down);
        assert_eq!(step.extension(), ".down.sql");
        assert!(step.is_down_migration());
    }

    #[test]
    fn test_migrate_step_basic() {
        let filename = "0003_create_users.sql";
        let step = MigrateStep::from_filename(filename);
        assert_eq!(step, MigrateStep::Basic);
        assert_eq!(step.extension(), ".sql");
        assert!(!step.is_down_migration());
    }
}