fix: 修复重构引入的若干 bug (#126)

This commit is contained in:
ᴀᴍᴛᴏᴀᴇʀ
2024-07-04 01:00:41 +08:00
committed by GitHub
parent 940abd4f3b
commit ff1150e863
4 changed files with 10 additions and 24 deletions
+1 -9
View File
@@ -7,7 +7,6 @@ use std::pin::Pin;
use anyhow::Result; use anyhow::Result;
use async_trait::async_trait; use async_trait::async_trait;
use bili_sync_migration::IntoIden;
pub use collection::collection_from; pub use collection::collection_from;
pub use favorite::favorite_from; pub use favorite::favorite_from;
use futures::Stream; use futures::Stream;
@@ -32,18 +31,11 @@ pub async fn video_list_from<'a>(
} }
} }
pub const fn unique_video_columns() -> impl IntoIterator<Item = impl IntoIden> {
[
bili_sync_entity::video::Column::CollectionId,
bili_sync_entity::video::Column::FavoriteId,
bili_sync_entity::video::Column::Bvid,
]
}
#[async_trait] #[async_trait]
pub trait VideoListModel { pub trait VideoListModel {
/* 逻辑相关 */ /* 逻辑相关 */
/// 获取与视频列表关联的视频总数
async fn video_count(&self, connection: &DatabaseConnection) -> Result<u64>; async fn video_count(&self, connection: &DatabaseConnection) -> Result<u64>;
/// 获取未填充的视频 /// 获取未填充的视频
+2 -1
View File
@@ -12,8 +12,9 @@ impl VideoInfo {
Some(base_model) => base_model.into_active_model(), Some(base_model) => base_model.into_active_model(),
None => { None => {
let mut tmp_model = bili_sync_entity::video::Model::default().into_active_model(); let mut tmp_model = bili_sync_entity::video::Model::default().into_active_model();
// 注意此处要把 id 设置 NotSet否则 id 会是 Unchanged(0) // 注意此处要把 id 和 created_at 设置 NotSet方便在 sql 中忽略这些字段,交由数据库自动生成
tmp_model.id = NotSet; tmp_model.id = NotSet;
tmp_model.created_at = NotSet;
tmp_model tmp_model
} }
}; };
+3 -2
View File
@@ -4,7 +4,7 @@ use bili_sync_migration::OnConflict;
use sea_orm::entity::prelude::*; use sea_orm::entity::prelude::*;
use sea_orm::ActiveValue::Set; use sea_orm::ActiveValue::Set;
use crate::adapter::{unique_video_columns, VideoListModel}; use crate::adapter::VideoListModel;
use crate::bilibili::{PageInfo, VideoInfo}; use crate::bilibili::{PageInfo, VideoInfo};
/// 尝试创建 Video Model,如果发生冲突则忽略 /// 尝试创建 Video Model,如果发生冲突则忽略
@@ -18,7 +18,8 @@ pub async fn create_videos(
.map(|v| video_list_model.video_model_by_info(v, None)) .map(|v| video_list_model.video_model_by_info(v, None))
.collect::<Vec<_>>(); .collect::<Vec<_>>();
video::Entity::insert_many(video_models) video::Entity::insert_many(video_models)
.on_conflict(OnConflict::columns(unique_video_columns()).do_nothing().to_owned()) // 这里想表达的是 on 索引名,但 sea-orm 的 api 似乎只支持列名而不支持索引名,好在留空可以达到相同的目的
.on_conflict(OnConflict::new().do_nothing().to_owned())
.do_nothing() .do_nothing()
.exec(connection) .exec(connection)
.await?; .await?;
@@ -87,18 +87,10 @@ impl MigrationTrait for Migration {
.to_owned(), .to_owned(),
) )
.await?; .await?;
manager // 在唯一索引中,NULL 不等于 NULL,所以需要使用 ifnull 函数排除空的情况
.create_index( db.execute_unprepared("CREATE UNIQUE INDEX `idx_video_cid_fid_bvid` ON `video` (ifnull(`collection_id`, -1), ifnull(`favorite_id`, -1), `bvid`)")
Index::create() .await?;
.table(Video::Table) Ok(())
.name("idx_video_cid_fid_bvid")
.col(Video::CollectionId)
.col(Video::FavoriteId)
.col(Video::Bvid)
.unique()
.to_owned(),
)
.await
} }
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {