use crate::{
database::{
DatabaseTy,
schema_manager::{
Repeatability,
migration::migration_common::MigrationCommon,
misc::{calc_checksum, is_sorted_and_unique},
},
},
misc::{ArrayVector, Lease},
};
use alloc::string::String;
pub type UserMigrationOwned = UserMigration<ArrayVector<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,
version: i32,
) -> Self {
Self {
dbs,
common: MigrationCommon { checksum, name, repeatability, version },
sql_down,
sql_up,
}
}
#[inline]
pub fn from_user_parts(
dbs: DBS,
name: S,
repeatability: Option<Repeatability>,
[sql_up, sql_down]: [S; 2],
version: i32,
) -> crate::Result<Self> {
is_sorted_and_unique(dbs.lease())?;
let checksum = calc_checksum(name.lease(), sql_up.lease(), sql_down.lease(), version);
Ok(Self {
dbs,
common: MigrationCommon { checksum, name, repeatability, version },
sql_down,
sql_up,
})
}
#[inline]
pub 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 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 fn version(&self) -> i32 {
self.common.version
}
}