mysql_connector/migrator/
migration.rs

1use {
2    crate::{error::Error, pool::AsyncPoolTrait, Connection},
3    std::{cmp, fmt, future::Future, pin::Pin},
4};
5
6pub trait Migration {
7    fn name(&self) -> &'static str;
8    fn up<'a>(
9        &'a self,
10        pool: &'a dyn AsyncPoolTrait<Connection>,
11    ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + 'a>>;
12    fn down<'a>(
13        &'a self,
14        pool: &'a dyn AsyncPoolTrait<Connection>,
15    ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + 'a>>;
16}
17
18pub struct MigrationList {
19    pub version: Version,
20    pub migrations: &'static [&'static dyn Migration],
21}
22
23impl MigrationList {
24    pub fn ordered(list: &[Self]) -> bool {
25        if list.len() <= 1 {
26            return true;
27        }
28        for i in 1..list.len() {
29            if list[i - 1].version >= list[i].version {
30                return false;
31            }
32        }
33        true
34    }
35}
36
37#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
38pub struct Version(pub u16, pub u16, pub u16);
39
40impl cmp::Ord for Version {
41    fn cmp(&self, other: &Self) -> cmp::Ordering {
42        match self.0.cmp(&other.0) {
43            core::cmp::Ordering::Equal => {}
44            ord => return ord,
45        }
46        match self.1.cmp(&other.1) {
47            core::cmp::Ordering::Equal => {}
48            ord => return ord,
49        }
50        self.2.cmp(&other.2)
51    }
52}
53
54impl PartialOrd for Version {
55    fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
56        Some(self.cmp(other))
57    }
58}
59
60impl fmt::Display for Version {
61    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
62        write!(f, "({}.{}.{})", self.0, self.1, self.2)
63    }
64}