degen_sql/db/postgres/models/
migrations_model.rs

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
use crate::db::postgres::postgres_db::Database;

use super::model::PostgresModelError;

use std::str::FromStr;

pub struct Migration {
    name: String,
    executed_at: u64,
}

pub struct MigrationsModel {}

impl MigrationsModel {
    pub async fn find(psql_db: &Database) -> Result<Vec<Migration>, PostgresModelError> {
        let rows = psql_db
            .query(
                "
        SELECT 
            name,
            executed_at  
        FROM migrations
       
        ORDER BY executed_at DESC
        ;
        ",
                &[],
            )
            .await;

        match rows {
            Ok(rows) => {
                let mut migrations = Vec::new();

                for row in rows {
                    let migration = Migration {
                        name: row.get("name"),

                        //change this type ??
                        executed_at: row.get::<_, i64>("executed_at") as u64,
                    };

                    migrations.push(migration);
                }

                Ok(migrations)
            }
            Err(e) => {
                eprintln!("Database error: {:?}", e);
                Err(PostgresModelError::Postgres(e))
            }
        }
    }
}