You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
This file contains hidden or 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
/// * `change`: The range-adjusted change to use for statement changes
/// * `original_change`: The original change to use for text changes (yes, this is a bit confusing, and we might want to refactor this entire thing at some point.)
fn apply_change(
&mut self,
change: &ChangeParams,
original_change: &ChangeParams,
) -> Vec<StatementChange> {
// if range is none, we have a full change
if change.range.is_none() {
// doesnt matter what change since range is null
return self.apply_full_change(change);
}
Expand All
@@ -255,7 +265,7 @@ impl Document {
let change_range = change.range.unwrap();
let previous_content = self.content.clone();
let new_content =change.apply_to_text(&self.content);
let new_content =original_change.apply_to_text(&self.content);
// we first need to determine the affected range and all affected statements, as well as
// the index of the prev and the next statement, if any. The full affected range is the
Expand DownExpand Up
@@ -1560,28 +1570,29 @@ mod tests {
fn multiple_deletions_at_once() {
let path = PgTPath::new("test.sql");
let mut doc = Document::new("\n\n\n\nALTER TABLE ONLY \"public\".\"sendout\"\n ADD CONSTRAINT \"sendout_organisation_id_fkey\" FOREIGN
KEY (\"organisation_id\") REFERENCES \"public\".\"organisation\"(\"id\") ON UPDATE RESTRICT ON DELETE CASCADE;\n".to_string(), 0);
let mut doc = Document::new("ALTER TABLE ONLY public.omni_channel_message ADD CONSTRAINT omni_channel_message_organisation_id_fkey FOREIGN KEY (organisation_id) REFERENCES public.organisation(id) ON UPDATE RESTRICT ON DELETE CASCADE;".to_string(), 0);
let change = ChangeFileParams {
path: path.clone(),
version: 1,
changes: vec![
ChangeParams {
range: Some(TextRange::new(31.into(),38.into())),
text: "te".to_string(),
range: Some(TextRange::new(60.into(),80.into())),
text: "sendout".to_string(),
},
ChangeParams {
range: Some(TextRange::new(60.into(),67.into())),
text: "te".to_string(),
range: Some(TextRange::new(24.into(),44.into())),
text: "sendout".to_string(),
},
],
};
let changed = doc.apply_file_change(&change);
assert_eq!(doc.content, "\n\n\n\nALTER TABLE ONLY \"public\".\"te\"\n ADD CONSTRAINT \"te_organisation_id_fkey\" FOREIGN
KEY (\"organisation_id\") REFERENCES \"public\".\"organisation\"(\"id\") ON UPDATE RESTRICT ON DELETE CASCADE;\n");
assert_eq!(
doc.content,
"ALTER TABLE ONLY public.sendout ADD CONSTRAINT sendout_organisation_id_fkey FOREIGN KEY (organisation_id) REFERENCES public.organisation(id) ON UPDATE RESTRICT ON DELETE CASCADE;"
);
assert_eq!(changed.len(), 2);
Expand All
@@ -1592,28 +1603,29 @@ KEY (\"organisation_id\") REFERENCES \"public\".\"organisation\"(\"id\") ON UPDA
fn multiple_additions_at_once() {
let path = PgTPath::new("test.sql");
let mut doc = Document::new("\n\n\n\nALTER TABLE ONLY \"public\".\"sendout\"\n ADD CONSTRAINT \"sendout_organisation_id_fkey\" FOREIGN
KEY (\"organisation_id\") REFERENCES \"public\".\"organisation\"(\"id\") ON UPDATE RESTRICT ON DELETE CASCADE;\n".to_string(), 0);
let mut doc = Document::new("ALTER TABLE ONLY public.sendout ADD CONSTRAINT sendout_organisation_id_fkey FOREIGN KEY (organisation_id) REFERENCES public.organisation(id) ON UPDATE RESTRICT ON DELETE CASCADE;".to_string(), 0);
let change = ChangeFileParams {
path: path.clone(),
version: 1,
changes: vec![
ChangeParams {
range: Some(TextRange::new(31.into(),38.into())),
range: Some(TextRange::new(47.into(),54.into())),
text: "omni_channel_message".to_string(),
},
ChangeParams {
range: Some(TextRange::new(60.into(),67.into())),
range: Some(TextRange::new(24.into(),31.into())),
text: "omni_channel_message".to_string(),
},
],
};
let changed = doc.apply_file_change(&change);
assert_eq!(doc.content, "\n\n\n\nALTER TABLE ONLY \"public\".\"omni_channel_message\"\n ADD CONSTRAINT \"omni_channel_message_organisation_id_fkey\" FOREIGN
KEY (\"organisation_id\") REFERENCES \"public\".\"organisation\"(\"id\") ON UPDATE RESTRICT ON DELETE CASCADE;\n");
assert_eq!(
doc.content,
"ALTER TABLE ONLY public.omni_channel_message ADD CONSTRAINT omni_channel_message_organisation_id_fkey FOREIGN KEY (organisation_id) REFERENCES public.organisation(id) ON UPDATE RESTRICT ON DELETE CASCADE;"
);
assert_eq!(changed.len(), 2);
Expand DownExpand Up
@@ -1663,6 +1675,38 @@ KEY (\"organisation_id\") REFERENCES \"public\".\"organisation\"(\"id\") ON UPDA
assert_document_integrity(&doc);
}
#[test]
fn test_content_out_of_sync() {
let path = PgTPath::new("test.sql");
let initial_content = "select 1, 2, 2232231313393319 from unknown_users;\n";
let mut doc = Document::new(initial_content.to_string(), 0);
"select 1, 2, 223223131339331931 from unknown_users;\n"
);
assert_document_integrity(&doc);
}
#[test]
fn test_comments_only() {
let path = PgTPath::new("test.sql");
Expand Down
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.