use id_effect_sql::migrate::Migrator;
pub(crate) const SCHEMA: &str = include_str!("../migrations/0001_id_effect_messaging.up.sql");
const SCHEMA_DOWN: &str = include_str!("../migrations/0001_id_effect_messaging.down.sql");
pub const MIGRATIONS_TABLE: &str = "_id_effect_messaging_migrations";
pub fn migrator() -> Migrator {
Migrator::from_files([
("0001_id_effect_messaging.up.sql", SCHEMA),
("0001_id_effect_messaging.down.sql", SCHEMA_DOWN),
])
.expect("the embedded id_effect_messaging migrations are valid")
.with_table(MIGRATIONS_TABLE)
}
pub(crate) fn statements(script: &str) -> Vec<String> {
let code: String = script
.lines()
.filter(|line| !line.trim_start().starts_with("--"))
.map(|line| format!("{line}\n"))
.collect();
code
.split(';')
.map(str::trim)
.filter(|s| !s.is_empty())
.map(str::to_owned)
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_migrator_has_its_own_tracking_table() {
let m = migrator();
assert_eq!(m.table(), MIGRATIONS_TABLE);
assert_eq!(m.migrations().len(), 1);
let first = &m.migrations()[0];
assert_eq!((first.version(), first.is_reversible()), (1, true));
assert_eq!(first.sql(), SCHEMA);
}
#[test]
fn statements_drop_comments_and_split() {
let sql = statements(SCHEMA);
assert_eq!(sql.len(), 5, "{sql:?}");
assert!(sql[0].starts_with("CREATE TABLE IF NOT EXISTS id_effect_outbox"));
assert!(sql[1].contains("id_effect_outbox_pending_idx"));
assert!(sql[2].contains("id_effect_outbox_sent_idx"));
assert!(sql[3].starts_with("CREATE TABLE IF NOT EXISTS id_effect_inbox"));
assert!(sql[4].contains("id_effect_inbox_processed_idx"));
assert!(sql.iter().all(|s| !s.contains("--") && !s.ends_with(';')));
}
}