so6 0.2.0

Framework for manage background data migration with PostgreSQL
Documentation
pub use crate::index::test_setup;
pub use crate::index::Index;
use glob::glob;
use serde_yaml;
use std::{collections::HashMap, fs, path::Path, path::PathBuf};

mod database;
mod index;

fn read_index_file(file_path: &PathBuf) -> HashMap<String, Index> {
    serde_yaml::from_str(
        fs::read_to_string(file_path.to_str().unwrap())
            .unwrap()
            .as_str(),
    )
    .expect(format!("Couldn't deserialize {}", file_path.to_str().unwrap()).as_str())
}

pub fn list<P: AsRef<Path>>(prj_path: PathBuf, glob_pattern: P) -> Vec<(PathBuf, String, Index)> {
    let mut indexes: Vec<(PathBuf, String, Index)> = [].to_vec();
    for index_file in
        glob(prj_path.join(glob_pattern).to_str().unwrap()).expect("No So6 index file found")
    {
        let index_file_path: PathBuf = index_file.unwrap();
        for (index_creation_name, idx) in read_index_file(&index_file_path).iter() {
            if idx.required() {
                indexes.push((
                    index_file_path.clone(),
                    index_creation_name.to_string(),
                    idx.clone(),
                ));
            }
        }
    }
    indexes.sort_by_key(|(path, _name, idx)| {
        format!("{} {}", path.to_str().unwrap(), idx.name.clone())
    });
    indexes
}

pub fn create_index(index_file_path: PathBuf, key: String, locked: bool) {
    let idx = read_index_file(&index_file_path)[&key].clone();
    if !idx.required() {
        panic!("The index creation {key} isn't required");
    }
    if idx.exists() {
        println!("The index creation {key} already exist");
        return;
    }
    if locked {
        idx.final_run();
    } else {
        idx.background_run();
    }
}