Skip to content

Commit

Permalink
os(browsing): use insert_many instead of multiple insert
Browse files Browse the repository at this point in the history
  • Loading branch information
vnghia committed Jan 14, 2024
1 parent e7da954 commit 124e9ae
Showing 1 changed file with 31 additions and 27 deletions.
58 changes: 31 additions & 27 deletions src/open_subsonic/browsing/refresh_music_folders.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -13,42 +12,47 @@ pub async fn refresh_music_folders<P: AsRef<Path>>(
) -> (Vec<music_folder::Model>, u64) {
let update_start_time = time::OffsetDateTime::now_utc();

let upserted_folders: Vec<music_folder::Model> =
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::<Vec<_>>()
.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
.expect("can not delete old music folder")
.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)
}

Expand Down

0 comments on commit 124e9ae

Please sign in to comment.