From 379bdfbee5b4266229cdf00c8e76b72cbb650d40 Mon Sep 17 00:00:00 2001 From: Yesterday17 Date: Thu, 5 Sep 2024 22:22:20 +0800 Subject: [PATCH] feat(anni): migrate with commit=false --- anni-metadata/queries/add-album.graphql | 12 ++++++++++-- anni-metadata/schemas/annim.graphql | 17 ++++++++++++++++- anni-metadata/src/annim/client.rs | 2 ++ anni-metadata/src/annim/mutation/add_album.rs | 3 ++- anni/src/subcommands/repo/migrate.rs | 16 ++++++++++++++-- anni/src/subcommands/workspace/add.rs | 2 +- 6 files changed, 45 insertions(+), 7 deletions(-) diff --git a/anni-metadata/queries/add-album.graphql b/anni-metadata/queries/add-album.graphql index 14b17b5..714009e 100644 --- a/anni-metadata/queries/add-album.graphql +++ b/anni-metadata/queries/add-album.graphql @@ -52,8 +52,16 @@ fragment AlbumDetail on Album { } } -mutation addAlbum($album: AddAlbumInput!) { - addAlbum(input: $album) { +fragment TagBase on Tag { + id + name + type + createdAt + updatedAt +} + +mutation addAlbum($album: AddAlbumInput!, $commit: Boolean) { + addAlbum(input: $album, commit: $commit) { ...AlbumDetail } } diff --git a/anni-metadata/schemas/annim.graphql b/anni-metadata/schemas/annim.graphql index 4868224..346337b 100644 --- a/anni-metadata/schemas/annim.graphql +++ b/anni-metadata/schemas/annim.graphql @@ -182,7 +182,7 @@ type MetadataMutation { """ Add the metatada of a full album to annim. """ - addAlbum(input: AddAlbumInput!): Album! + addAlbum(input: AddAlbumInput!, commit: Boolean): Album! """ Update basic album information. Use this method to update basic album information such as title, artist and others. @@ -262,6 +262,7 @@ enum MetadataOrganizeLevel { type MetadataQuery { album(albumId: UUID!): Album albums(by: AlbumsBy!, after: String, first: Int): AlbumConnection! + tracks(keyword: String!): [TrackSearchResult!]! tag(tagName: String!, tagType: TagType): [Tag!]! } @@ -338,6 +339,20 @@ type Track { updatedAt: DateTime! } +type TrackSearchResult { + """ + Return the search score of the track. + """ + score: Float! + """ + Return a `TrackIdentifier` string which represents the track. + """ + identifier: String! + album: Album + disc: Disc + track: Track +} + enum TrackType { NORMAL INSTRUMENTAL diff --git a/anni-metadata/src/annim/client.rs b/anni-metadata/src/annim/client.rs index 504acfd..fb157fd 100644 --- a/anni-metadata/src/annim/client.rs +++ b/anni-metadata/src/annim/client.rs @@ -49,6 +49,7 @@ impl AnnimClient { pub async fn add_album( &self, album: &model::Album, + commit: bool, ) -> anyhow::Result { let discs: Vec<_> = album.iter().collect(); let input = mutation::add_album::AddAlbumInput { @@ -82,6 +83,7 @@ impl AnnimClient { let query = mutation::add_album::AddAlbumMutation::build(mutation::add_album::AddAlbumVariables { album: input, + commit: Some(commit), }); let response = self.client.post(&self.endpoint).run_graphql(query).await?; if let Some(errors) = response.errors { diff --git a/anni-metadata/src/annim/mutation/add_album.rs b/anni-metadata/src/annim/mutation/add_album.rs index 861a13b..8f66690 100644 --- a/anni-metadata/src/annim/mutation/add_album.rs +++ b/anni-metadata/src/annim/mutation/add_album.rs @@ -4,12 +4,13 @@ use crate::annim::{schema, Json, Uuid}; #[derive(cynic::QueryVariables, Debug)] pub struct AddAlbumVariables<'a> { pub album: AddAlbumInput<'a>, + pub commit: Option, } #[derive(cynic::QueryFragment, Debug)] #[cynic(graphql_type = "MetadataMutation", variables = "AddAlbumVariables")] pub struct AddAlbumMutation { - #[arguments(input: $album)] + #[arguments(input: $album, commit: $commit)] pub add_album: AlbumFragment, } diff --git a/anni/src/subcommands/repo/migrate.rs b/anni/src/subcommands/repo/migrate.rs index e093571..53be4ea 100644 --- a/anni/src/subcommands/repo/migrate.rs +++ b/anni/src/subcommands/repo/migrate.rs @@ -58,8 +58,20 @@ async fn repo_migrate(me: RepoMigrateAction, manager: RepositoryManager) -> anyh let mut albums = HashMap::new(); log::info!("Start inserting albums..."); - for album in repo.albums_iter() { - let annim_album = client.add_album(album).await?; + let mut albums_iter = repo.albums_iter(); + let last = albums_iter.next(); // read the first album for further commit + + for album in albums_iter { + let annim_album = client.add_album(album, false).await?; + log::info!( + "Inserted album {}, id = {}", + annim_album.title, + annim_album.id.inner() + ); + albums.insert(album.album_id, annim_album); + } + if let Some(album) = last { + let annim_album = client.add_album(album, true).await?; log::info!( "Inserted album {}, id = {}", annim_album.title, diff --git a/anni/src/subcommands/workspace/add.rs b/anni/src/subcommands/workspace/add.rs index 5b6e1f3..98a670d 100644 --- a/anni/src/subcommands/workspace/add.rs +++ b/anni/src/subcommands/workspace/add.rs @@ -108,7 +108,7 @@ fn handle_workspace_add(me: WorkspaceAddAction) -> anyhow::Result<()> { } anni_workspace::config::WorkspaceMetadata::Remote { endpoint, token } => { let client = AnnimClient::new(endpoint, token.as_deref()); - client.add_album(&album).await?; + client.add_album(&album, true).await?; } } }