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
67
68
69
70
71
72
use {
    super::Version,
    crate::{
        connection::types::Column,
        error::ParseError,
        model::{FromQueryResult, FromQueryResultMapping, ModelData},
        types::Value,
    },
};

#[derive(Debug)]
pub(super) struct MigrationModel {
    pub(super) version: Version,
    pub(super) name: String,
}

impl ModelData for MigrationModel {
    const TABLE: &'static str = "migrations";
    const TABLE_WITH_POINT: &'static str = "migrations.";
}

impl FromQueryResult for MigrationModel {
    type Mapping = MigrationMapping;

    fn from_mapping_and_row(
        mapping: &Self::Mapping,
        row: &mut Vec<Value>,
    ) -> std::result::Result<Self, crate::error::ParseError> {
        Ok(Self {
            version: Version(
                row[mapping
                    .version_0
                    .ok_or(ParseError::MissingField("version_0"))?]
                .take()
                .try_into()?,
                row[mapping
                    .version_1
                    .ok_or(ParseError::MissingField("version_1"))?]
                .take()
                .try_into()?,
                row[mapping
                    .version_2
                    .ok_or(ParseError::MissingField("version_2"))?]
                .take()
                .try_into()?,
            ),
            name: row[mapping.name.ok_or(ParseError::MissingField("name"))?]
                .take()
                .try_into()?,
        })
    }
}

#[derive(Default)]
pub(super) struct MigrationMapping {
    version_0: Option<usize>,
    version_1: Option<usize>,
    version_2: Option<usize>,
    name: Option<usize>,
}

impl FromQueryResultMapping<MigrationModel> for MigrationMapping {
    fn set_mapping_inner(&mut self, column: &Column, _name: &str, index: usize) {
        *match column.org_name() {
            "version_0" => &mut self.version_0,
            "version_1" => &mut self.version_1,
            "version_2" => &mut self.version_2,
            "name" => &mut self.name,
            _ => return,
        } = Some(index);
    }
}