use crate::database::migrations::ledger;
use crate::database::{Database, DatabaseConnection};
use std::path::{Path, PathBuf};
pub(super) async fn unapplied(directories: Vec<PathBuf>) -> Vec<PathBuf> {
match connect().await {
Some(connection) => filter(&connection, directories).await,
None => directories,
}
}
pub(super) async fn filter(
connection: &DatabaseConnection,
directories: Vec<PathBuf>,
) -> Vec<PathBuf> {
let mut pending = Vec::new();
for directory in directories {
if is_pending(connection, &directory).await {
pending.push(directory);
}
}
pending
}
async fn is_pending(connection: &DatabaseConnection, directory: &Path) -> bool {
let Some(path) = directory.to_str() else {
return true;
};
let Some(id) = ledger::ledger_id_for_dir(path) else {
return true;
};
!ledger::is_applied(connection, id).await.unwrap_or(false)
}
async fn connect() -> Option<DatabaseConnection> {
let database = Database::get_database_from_rustyroad_toml().ok()?;
let connection = Database::create_database_connection(&database).await.ok()?;
ledger::ensure_table(&connection).await.ok()?;
Some(connection)
}