1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/// Migration Type represents the type of migration
#[derive(Debug, Copy, Clone)]
pub enum MigrationType {
    /// Simple migration are single file migrations with no up / down queries
    Simple,

    /// ReversibleUp migrations represents the  add or update part of a reversible migrations
    /// It is expected the every migration of this type will have a corresponding down file
    ReversibleUp,

    /// ReversibleDown migrations represents the  delete or downgrade part of a reversible migrations
    /// It is expected the every migration of this type will have a corresponding up file
    ReversibleDown,
}

impl MigrationType {
    pub fn from_filename(filename: &str) -> Self {
        if filename.ends_with(MigrationType::ReversibleUp.suffix()) {
            MigrationType::ReversibleUp
        } else if filename.ends_with(MigrationType::ReversibleDown.suffix()) {
            MigrationType::ReversibleDown
        } else {
            MigrationType::Simple
        }
    }

    pub fn is_reversible(&self) -> bool {
        match self {
            MigrationType::Simple => false,
            MigrationType::ReversibleUp => true,
            MigrationType::ReversibleDown => true,
        }
    }

    pub fn is_down_migration(&self) -> bool {
        match self {
            MigrationType::Simple => false,
            MigrationType::ReversibleUp => false,
            MigrationType::ReversibleDown => true,
        }
    }

    pub fn label(&self) -> &'static str {
        match self {
            MigrationType::Simple => "migrate",
            MigrationType::ReversibleUp => "migrate",
            MigrationType::ReversibleDown => "revert",
        }
    }

    pub fn suffix(&self) -> &'static str {
        match self {
            MigrationType::Simple => ".sql",
            MigrationType::ReversibleUp => ".up.sql",
            MigrationType::ReversibleDown => ".down.sql",
        }
    }

    pub fn file_content(&self) -> &'static str {
        match self {
            MigrationType::Simple => "-- Add migration script here\n",
            MigrationType::ReversibleUp => "-- Add up migration script here\n",
            MigrationType::ReversibleDown => "-- Add down migration script here\n",
        }
    }
}