use crate::{
collection::ArrayVectorU8,
database::{
DatabaseTy,
schema_manager::{
Repeatability, Uid,
migration::migration_common::MigrationCommon,
misc::{calc_checksum, is_sorted_and_unique},
},
},
misc::Lease,
};
use alloc::string::String;
pub type UserMigrationOwned =
UserMigration<ArrayVectorU8<DatabaseTy, { DatabaseTy::len() }>, String>;
pub type UserMigrationRef<'dbs, 'str> = UserMigration<&'dbs [DatabaseTy], &'str str>;
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub struct UserMigration<DBS, S> {
common: MigrationCommon<S>,
dbs: DBS,
sql_down: S,
sql_up: S,
}
impl<DBS, S> UserMigration<DBS, S>
where
DBS: Lease<[DatabaseTy]>,
S: Lease<str>,
{
#[inline]
pub const fn from_all_parts(
checksum: u64,
dbs: DBS,
name: S,
repeatability: Option<Repeatability>,
sql_down: S,
sql_up: S,
uid: Uid,
) -> Self {
Self { dbs, common: MigrationCommon { checksum, name, repeatability, uid }, sql_down, sql_up }
}
#[inline]
pub fn from_user_parts(
dbs: DBS,
name: S,
repeatability: Option<Repeatability>,
[sql_up, sql_down]: [S; 2],
uid: Uid,
) -> crate::Result<Self> {
is_sorted_and_unique(dbs.lease())?;
let checksum = calc_checksum(name.lease(), sql_up.lease(), sql_down.lease(), uid);
Ok(Self {
dbs,
common: MigrationCommon { checksum, name, repeatability, uid },
sql_down,
sql_up,
})
}
#[inline]
pub const fn checksum(&self) -> u64 {
self.common.checksum
}
#[inline]
pub fn dbs(&self) -> &[DatabaseTy] {
self.dbs.lease()
}
#[inline]
pub fn name(&self) -> &str {
self.common.name.lease()
}
#[inline]
pub const fn repeatability(&self) -> Option<Repeatability> {
self.common.repeatability
}
#[inline]
pub fn sql_down(&self) -> &str {
self.sql_down.lease()
}
#[inline]
pub fn sql_up(&self) -> &str {
self.sql_up.lease()
}
#[inline]
pub const fn uid(&self) -> Uid {
self.common.uid
}
}