mysql_connector/migrator/
model.rs

1use {
2    super::Version,
3    crate::{
4        connection::types::Column,
5        error::ParseError,
6        model::{FromQueryResult, FromQueryResultMapping, ModelData},
7        types::Value,
8    },
9};
10
11#[derive(Debug)]
12pub(super) struct MigrationModel {
13    pub(super) version: Version,
14    pub(super) name: String,
15}
16
17impl ModelData for MigrationModel {
18    const TABLE: &'static str = "migrations";
19    const TABLE_WITH_POINT: &'static str = "migrations.";
20}
21
22impl FromQueryResult for MigrationModel {
23    type Mapping = MigrationMapping;
24
25    fn from_mapping_and_row(
26        mapping: &Self::Mapping,
27        row: &mut Vec<Value>,
28    ) -> std::result::Result<Self, crate::error::ParseError> {
29        Ok(Self {
30            version: Version(
31                row[mapping
32                    .version_0
33                    .ok_or(ParseError::MissingField("version_0"))?]
34                .take()
35                .try_into()?,
36                row[mapping
37                    .version_1
38                    .ok_or(ParseError::MissingField("version_1"))?]
39                .take()
40                .try_into()?,
41                row[mapping
42                    .version_2
43                    .ok_or(ParseError::MissingField("version_2"))?]
44                .take()
45                .try_into()?,
46            ),
47            name: row[mapping.name.ok_or(ParseError::MissingField("name"))?]
48                .take()
49                .try_into()?,
50        })
51    }
52}
53
54#[derive(Default)]
55pub(super) struct MigrationMapping {
56    version_0: Option<usize>,
57    version_1: Option<usize>,
58    version_2: Option<usize>,
59    name: Option<usize>,
60}
61
62impl FromQueryResultMapping<MigrationModel> for MigrationMapping {
63    fn set_mapping_inner(&mut self, column: &Column, _name: &str, index: usize) {
64        *match column.org_name() {
65            "version_0" => &mut self.version_0,
66            "version_1" => &mut self.version_1,
67            "version_2" => &mut self.version_2,
68            "name" => &mut self.name,
69            _ => return,
70        } = Some(index);
71    }
72}