-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
2562 name merging handle name merge sync messages #2674
Merged
Chris-Petty
merged 5 commits into
feature/merging
from
2562-Name-merging-handle-name-merge-sync-messages
Jan 8, 2024
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7ce7ec8
Find many name links by name id
roxy-dao b63cb24
Name merge translation
roxy-dao fabeb7e
Remove logger
roxy-dao 2d1d06c
Get rid of unwrap and use ok_or for item link
roxy-dao 8975465
Merge branch 'feature/merging' into 2562-Name-merging-handle-name-merβ¦
Chris-Petty File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,7 +42,10 @@ impl SyncTranslation for ItemMergeTranslation { | |
} | ||
let indirect_link = item_link_repo | ||
.find_one_by_id(&data.merge_id_to_keep)? | ||
.unwrap(); | ||
.ok_or(anyhow::anyhow!( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove unwrap and use clearer error message |
||
"Could not find item link with id {}", | ||
data.merge_id_to_keep | ||
))?; | ||
|
||
let upsert_records: Vec<PullUpsertRecord> = item_links | ||
.into_iter() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
155 changes: 155 additions & 0 deletions
155
server/service/src/sync/translations/special/name_merge.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
use repository::{NameLinkRow, NameLinkRowRepository, StorageConnection, SyncBufferRow}; | ||
|
||
use serde::Deserialize; | ||
|
||
use crate::sync::translations::{ | ||
IntegrationRecords, LegacyTableName, PullDependency, PullUpsertRecord, SyncTranslation, | ||
}; | ||
|
||
#[derive(Deserialize)] | ||
pub struct NameMergeMessage { | ||
#[serde(rename = "mergeIdToKeep")] | ||
pub merge_id_to_keep: String, | ||
#[serde(rename = "mergeIdToDelete")] | ||
pub merge_id_to_delete: String, | ||
} | ||
|
||
pub(crate) struct NameMergeTranslation {} | ||
impl SyncTranslation for NameMergeTranslation { | ||
fn pull_dependencies(&self) -> PullDependency { | ||
PullDependency { | ||
table: LegacyTableName::NAME, | ||
dependencies: vec![], | ||
} | ||
} | ||
|
||
fn try_translate_pull_merge( | ||
&self, | ||
connection: &StorageConnection, | ||
sync_record: &SyncBufferRow, | ||
) -> Result<Option<IntegrationRecords>, anyhow::Error> { | ||
if sync_record.table_name != LegacyTableName::NAME { | ||
return Ok(None); | ||
} | ||
|
||
let data = serde_json::from_str::<NameMergeMessage>(&sync_record.data)?; | ||
|
||
let name_link_repo = NameLinkRowRepository::new(connection); | ||
let name_links = name_link_repo.find_many_by_name_id(&data.merge_id_to_delete)?; | ||
if name_links.len() == 0 { | ||
return Ok(None); | ||
} | ||
let indirect_link = name_link_repo | ||
.find_one_by_id(&data.merge_id_to_keep)? | ||
.ok_or(anyhow::anyhow!( | ||
"Could not find name link with id {}", | ||
data.merge_id_to_keep | ||
))?; | ||
|
||
let upsert_records: Vec<PullUpsertRecord> = name_links | ||
.into_iter() | ||
.map(|NameLinkRow { id, .. }| { | ||
PullUpsertRecord::NameLink(NameLinkRow { | ||
id, | ||
name_id: indirect_link.name_id.clone(), | ||
}) | ||
}) | ||
.collect(); | ||
|
||
Ok(Some(IntegrationRecords::from_upserts(upsert_records))) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::sync::synchroniser::integrate_and_translate_sync_buffer; | ||
|
||
use super::*; | ||
use repository::{ | ||
mock::MockDataInserts, test_db::setup_all, NameLinkRowRepository, SyncBufferAction, | ||
SyncBufferRow, SyncBufferRowRepository, | ||
}; | ||
|
||
#[actix_rt::test] | ||
async fn test_name_merge() { | ||
let mut sync_records = vec![ | ||
SyncBufferRow { | ||
record_id: "name_b_merge".to_string(), | ||
table_name: LegacyTableName::NAME.to_string(), | ||
action: SyncBufferAction::Merge, | ||
data: r#"{ | ||
"mergeIdToKeep": "name_b", | ||
"mergeIdToDelete": "name_a" | ||
}"# | ||
.to_string(), | ||
..SyncBufferRow::default() | ||
}, | ||
SyncBufferRow { | ||
record_id: "name_c_merge".to_string(), | ||
table_name: LegacyTableName::NAME.to_string(), | ||
action: SyncBufferAction::Merge, | ||
data: r#"{ | ||
"mergeIdToKeep": "name_c", | ||
"mergeIdToDelete": "name_b" | ||
}"# | ||
.to_string(), | ||
..SyncBufferRow::default() | ||
}, | ||
]; | ||
|
||
let expected_name_links = vec![ | ||
NameLinkRow { | ||
id: "name_a".to_string(), | ||
name_id: "name_c".to_string(), | ||
}, | ||
NameLinkRow { | ||
id: "name_b".to_string(), | ||
name_id: "name_c".to_string(), | ||
}, | ||
NameLinkRow { | ||
id: "name_c".to_string(), | ||
name_id: "name_c".to_string(), | ||
}, | ||
]; | ||
|
||
let (_, connection, _, _) = setup_all( | ||
"test_name_merge_message_translation_in_order", | ||
MockDataInserts::none().units().names(), | ||
) | ||
.await; | ||
|
||
SyncBufferRowRepository::new(&connection) | ||
.upsert_many(&sync_records) | ||
.unwrap(); | ||
integrate_and_translate_sync_buffer(&connection, true).unwrap(); | ||
|
||
let name_link_repo = NameLinkRowRepository::new(&connection); | ||
let mut name_links = name_link_repo | ||
.find_many_by_name_id(&"name_c".to_string()) | ||
.unwrap(); | ||
|
||
name_links.sort_by_key(|i| i.id.to_owned()); | ||
assert_eq!(name_links, expected_name_links); | ||
|
||
let (_, connection, _, _) = setup_all( | ||
"test_name_merge_message_translation_in_reverse_order", | ||
MockDataInserts::none().units().names(), | ||
) | ||
.await; | ||
|
||
sync_records.reverse(); | ||
SyncBufferRowRepository::new(&connection) | ||
.upsert_many(&sync_records) | ||
.unwrap(); | ||
|
||
integrate_and_translate_sync_buffer(&connection, true).unwrap(); | ||
|
||
let name_link_repo = NameLinkRowRepository::new(&connection); | ||
let mut name_links = name_link_repo | ||
.find_many_by_name_id(&"name_c".to_string()) | ||
.unwrap(); | ||
|
||
name_links.sort_by_key(|i| i.id.to_owned()); | ||
assert_eq!(name_links, expected_name_links); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed to match pattern (return optional)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea probably for the best, though not sure when it'd ever not exist without something being quite wrong haha.