use crate::db::identity::EntityName;
use std::collections::BTreeSet;
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub struct MigrationRegistryKey {
migration_id: EntityName,
version: u64,
}
impl MigrationRegistryKey {
#[must_use]
pub const fn new(migration_id: EntityName, version: u64) -> Self {
Self {
migration_id,
version,
}
}
#[must_use]
pub const fn migration_id(self) -> EntityName {
self.migration_id
}
#[must_use]
pub const fn version(self) -> u64 {
self.version
}
}
#[derive(Clone, Debug, Default)]
pub struct MigrationRegistry {
completed: BTreeSet<MigrationRegistryKey>,
}
impl MigrationRegistry {
#[must_use]
pub const fn new() -> Self {
Self {
completed: BTreeSet::new(),
}
}
#[must_use]
pub fn is_applied(&self, migration_id: EntityName, version: u64) -> bool {
self.completed
.contains(&MigrationRegistryKey::new(migration_id, version))
}
pub fn record_applied(&mut self, migration_id: EntityName, version: u64) {
self.completed
.insert(MigrationRegistryKey::new(migration_id, version));
}
#[must_use]
pub fn len(&self) -> usize {
self.completed.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.completed.is_empty()
}
}