From 124e9ae2d8bb6d01aba970c1981fda3647bbc9b4 Mon Sep 17 00:00:00 2001 From: Vo Van Nghia Date: Sun, 14 Jan 2024 10:17:02 +0700 Subject: [PATCH] os(browsing): use insert_many instead of multiple insert --- .../browsing/refresh_music_folders.rs | 58 ++++++++++--------- 1 file changed, 31 insertions(+), 27 deletions(-) diff --git a/src/open_subsonic/browsing/refresh_music_folders.rs b/src/open_subsonic/browsing/refresh_music_folders.rs index 130d803e4..6efd2c5b0 100644 --- a/src/open_subsonic/browsing/refresh_music_folders.rs +++ b/src/open_subsonic/browsing/refresh_music_folders.rs @@ -1,7 +1,6 @@ use crate::entity::{prelude::*, *}; use crate::utils::fs::folders::build_music_folders; -use concat_string::concat_string; use futures::stream::{self, StreamExt}; use sea_orm::{DatabaseConnection, EntityTrait, *}; use std::path::Path; @@ -13,35 +12,31 @@ pub async fn refresh_music_folders>( ) -> (Vec, u64) { let update_start_time = time::OffsetDateTime::now_utc(); - let upserted_folders: Vec = - stream::iter(build_music_folders(top_paths, depth_levels).await) - .then(|music_folder| async move { - MusicFolder::insert(music_folder::ActiveModel { - path: Set(music_folder.to_string_lossy().to_string()), - updated_at: Set(time::OffsetDateTime::now_utc()), - ..Default::default() - }) - .on_conflict( - sea_query::OnConflict::column(music_folder::Column::Path) - .update_column(music_folder::Column::UpdatedAt) - .to_owned(), - ) - .exec_with_returning(conn) - .await - .expect(&concat_string!( - "can not upsert music folder ", - music_folder.to_string_lossy() - )) - }) - .collect() - .await; - for upserted_folder in &upserted_folders { - tracing::info!("new music folder added: {}", &upserted_folder.path); - } + let music_folder_models = stream::iter(build_music_folders(top_paths, depth_levels).await) + .map(|music_folder| music_folder::ActiveModel { + path: Set(music_folder.to_string_lossy().to_string()), + updated_at: Set(time::OffsetDateTime::now_utc()), + ..Default::default() + }) + .collect::>() + .await; + let music_folder_len = music_folder_models.len(); + + // TODO: use `exec_with_retuning` with `insert_many`. + // https://github.com/SeaQL/sea-orm/issues/1862 + MusicFolder::insert_many(music_folder_models) + .on_conflict( + sea_query::OnConflict::column(music_folder::Column::Path) + .update_column(music_folder::Column::UpdatedAt) + .to_owned(), + ) + .exec(conn) + .await + .expect("can not upsert music folder"); // TODO: return more information about what are deleted. // https://github.com/SeaQL/sea-orm/discussions/2059 - let deleted_folder_count = music_folder::Entity::delete_many() + let deleted_folder_count = MusicFolder::delete_many() .filter(music_folder::Column::UpdatedAt.lt(update_start_time)) .exec(conn) .await @@ -49,6 +44,15 @@ pub async fn refresh_music_folders>( .rows_affected; tracing::info!("{} old music folders deleted", deleted_folder_count); + let upserted_folders = MusicFolder::find() + .all(conn) + .await + .expect("can not get list of upserted folders"); + for upserted_folder in &upserted_folders { + tracing::info!("new music folder added: {}", &upserted_folder.path); + } + assert_eq!(upserted_folders.len(), music_folder_len); + (upserted_folders, deleted_folder_count) }