Restructure job dao

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/5/head
trivernis 3 years ago
parent 0261d2fe3c
commit 272b0bf7dc
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -0,0 +1,45 @@
use crate::dao::job::JobDao;
use mediarepo_core::content_descriptor::{
convert_v1_descriptor_to_v2, encode_content_descriptor, is_v1_content_descriptor,
};
use mediarepo_core::error::RepoResult;
use mediarepo_database::entities::content_descriptor;
use sea_orm::prelude::*;
use sea_orm::ActiveValue::Set;
use sea_orm::ConnectionTrait;
impl JobDao {
pub async fn migrate_content_descriptors(&self) -> RepoResult<()> {
let cds: Vec<content_descriptor::Model> =
content_descriptor::Entity::find().all(&self.ctx.db).await?;
tracing::info!("Converting content descriptors to v2 format...");
let mut converted_count = 0;
for cd in cds {
if is_v1_content_descriptor(&cd.descriptor) {
let trx = self.ctx.db.begin().await?;
let src_cd = cd.descriptor;
let dst_cd = convert_v1_descriptor_to_v2(&src_cd)?;
let _active_model = content_descriptor::ActiveModel {
id: Set(cd.id),
descriptor: Set(dst_cd.clone()),
};
self.ctx.main_storage.rename_file(&src_cd, &dst_cd).await?;
self.ctx
.thumbnail_storage
.rename_parent(
encode_content_descriptor(&src_cd),
encode_content_descriptor(&dst_cd),
)
.await?;
trx.commit().await?;
converted_count += 1;
}
}
tracing::info!("Converted {} descriptors", converted_count);
Ok(())
}
}

@ -1,12 +1,4 @@
use sea_orm::ActiveValue::Set;
use sea_orm::ConnectionTrait;
use sea_orm::prelude::*;
use mediarepo_core::content_descriptor::{
convert_v1_descriptor_to_v2, encode_content_descriptor, is_v1_content_descriptor,
};
use mediarepo_core::error::RepoResult;
use mediarepo_database::entities::content_descriptor;
pub mod migrate_content_descriptors;
use crate::dao::{DaoContext, DaoProvider};
@ -24,38 +16,4 @@ impl JobDao {
pub fn new(ctx: DaoContext) -> JobDao {
Self { ctx }
}
pub async fn migrate_content_descriptors(&self) -> RepoResult<()> {
let cds: Vec<content_descriptor::Model> =
content_descriptor::Entity::find().all(&self.ctx.db).await?;
tracing::info!("Converting content descriptors to v2 format...");
let mut converted_count = 0;
for cd in cds {
if is_v1_content_descriptor(&cd.descriptor) {
let trx = self.ctx.db.begin().await?;
let src_cd = cd.descriptor;
let dst_cd = convert_v1_descriptor_to_v2(&src_cd)?;
let _active_model = content_descriptor::ActiveModel {
id: Set(cd.id),
descriptor: Set(dst_cd.clone()),
};
self.ctx.main_storage.rename_file(&src_cd, &dst_cd).await?;
self.ctx
.thumbnail_storage
.rename_parent(
encode_content_descriptor(&src_cd),
encode_content_descriptor(&dst_cd),
)
.await?;
trx.commit().await?;
converted_count += 1;
}
}
tracing::info!("Converted {} descriptors", converted_count);
Ok(())
}
}

Loading…
Cancel
Save