Skip to content

Commit 7bc6a70

Browse files
authored
refactor(setup): allow notes added into setup message (#793)
1 parent a304fef commit 7bc6a70

File tree

10 files changed

+78
-44
lines changed

10 files changed

+78
-44
lines changed

src/execution/db_tracking_setup.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -103,38 +103,43 @@ impl TrackingTableSetupStatus {
103103
}
104104

105105
impl ResourceSetupStatus for TrackingTableSetupStatus {
106-
fn describe_changes(&self) -> Vec<String> {
107-
let mut changes: Vec<String> = vec![];
106+
fn describe_changes(&self) -> Vec<setup::ChangeDescription> {
107+
let mut changes: Vec<setup::ChangeDescription> = vec![];
108108
if self.desired_state.is_some() && !self.legacy_table_names.is_empty() {
109-
changes.push(format!(
109+
changes.push(setup::ChangeDescription::Action(format!(
110110
"Rename legacy tracking tables: {}. ",
111111
self.legacy_table_names.join(", ")
112-
));
112+
)));
113113
}
114114
match (self.min_existing_version_id, &self.desired_state) {
115115
(None, Some(state)) => {
116-
changes.push(format!("Create the tracking table: {}. ", state.table_name))
116+
changes.push(setup::ChangeDescription::Action(format!(
117+
"Create the tracking table: {}. ",
118+
state.table_name
119+
)));
117120
}
118121
(Some(min_version_id), Some(desired)) => {
119122
if min_version_id < desired.version_id {
120-
changes.push("Update the tracking table. ".into());
123+
changes.push(setup::ChangeDescription::Action(
124+
"Update the tracking table. ".into(),
125+
));
121126
}
122127
}
123-
(Some(_), None) => changes.push(format!(
128+
(Some(_), None) => changes.push(setup::ChangeDescription::Action(format!(
124129
"Drop existing tracking table: {}. ",
125130
self.legacy_table_names.join(", ")
126-
)),
131+
))),
127132
(None, None) => (),
128133
}
129134
if !self.source_ids_to_delete.is_empty() {
130-
changes.push(format!(
135+
changes.push(setup::ChangeDescription::Action(format!(
131136
"Delete source IDs: {}. ",
132137
self.source_ids_to_delete
133138
.iter()
134139
.map(|id| id.to_string())
135140
.collect::<Vec<String>>()
136141
.join(", ")
137-
));
142+
)));
138143
}
139144
changes
140145
}

src/ops/targets/kuzu.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ struct GraphElementDataSetupStatus {
187187
}
188188

189189
impl setup::ResourceSetupStatus for GraphElementDataSetupStatus {
190-
fn describe_changes(&self) -> Vec<String> {
190+
fn describe_changes(&self) -> Vec<setup::ChangeDescription> {
191191
self.actions.describe_changes()
192192
}
193193

src/ops/targets/neo4j.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -840,7 +840,7 @@ impl GraphElementDataSetupStatus {
840840
}
841841

842842
impl ResourceSetupStatus for GraphElementDataSetupStatus {
843-
fn describe_changes(&self) -> Vec<String> {
843+
fn describe_changes(&self) -> Vec<setup::ChangeDescription> {
844844
let mut result = vec![];
845845
if let Some(data_clear) = &self.data_clear {
846846
let mut desc = "Clear data".to_string();
@@ -856,7 +856,7 @@ impl ResourceSetupStatus for GraphElementDataSetupStatus {
856856
)
857857
.unwrap();
858858
}
859-
result.push(desc);
859+
result.push(setup::ChangeDescription::Action(desc));
860860
}
861861
result
862862
}

src/ops/targets/postgres.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -504,26 +504,28 @@ fn describe_index_spec(index_name: &str, index_spec: &VectorIndexDef) -> String
504504
}
505505

506506
impl setup::ResourceSetupStatus for SetupStatus {
507-
fn describe_changes(&self) -> Vec<String> {
507+
fn describe_changes(&self) -> Vec<setup::ChangeDescription> {
508508
let mut descriptions = self.actions.table_action.describe_changes();
509509
if self.create_pgvector_extension {
510-
descriptions.push("Create pg_vector extension (if not exists)".to_string());
510+
descriptions.push(setup::ChangeDescription::Action(
511+
"Create pg_vector extension (if not exists)".to_string(),
512+
));
511513
}
512514
if !self.actions.indexes_to_delete.is_empty() {
513-
descriptions.push(format!(
515+
descriptions.push(setup::ChangeDescription::Action(format!(
514516
"Delete indexes from table: {}",
515517
self.actions.indexes_to_delete.iter().join(", "),
516-
));
518+
)));
517519
}
518520
if !self.actions.indexes_to_create.is_empty() {
519-
descriptions.push(format!(
521+
descriptions.push(setup::ChangeDescription::Action(format!(
520522
"Create indexes in table: {}",
521523
self.actions
522524
.indexes_to_create
523525
.iter()
524526
.map(|(index_name, index_spec)| describe_index_spec(index_name, index_spec))
525527
.join(", "),
526-
));
528+
)));
527529
}
528530
descriptions
529531
}

src/ops/targets/qdrant.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,12 @@ struct SetupStatus {
104104
}
105105

106106
impl setup::ResourceSetupStatus for SetupStatus {
107-
fn describe_changes(&self) -> Vec<String> {
107+
fn describe_changes(&self) -> Vec<setup::ChangeDescription> {
108108
let mut result = vec![];
109109
if self.delete_collection {
110-
result.push("Delete collection".to_string());
110+
result.push(setup::ChangeDescription::Action(
111+
"Delete collection".to_string(),
112+
));
111113
}
112114
if let Some(add_collection) = &self.add_collection {
113115
let vector_descriptions = add_collection
@@ -121,14 +123,14 @@ impl setup::ResourceSetupStatus for SetupStatus {
121123
})
122124
.collect::<Vec<_>>()
123125
.join("; ");
124-
result.push(format!(
126+
result.push(setup::ChangeDescription::Action(format!(
125127
"Create collection{}",
126128
if vector_descriptions.is_empty() {
127129
"".to_string()
128130
} else {
129131
format!(" with vectors: {vector_descriptions}")
130132
}
131-
));
133+
)));
132134
}
133135
result
134136
}

src/ops/targets/shared/table_columns.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,41 +104,41 @@ impl<T: Eq + Serialize + DeserializeOwned> TableMainSetupAction<T> {
104104
}
105105
}
106106

107-
pub fn describe_changes(&self) -> Vec<String>
107+
pub fn describe_changes(&self) -> Vec<setup::ChangeDescription>
108108
where
109109
T: std::fmt::Display,
110110
{
111111
let mut descriptions = vec![];
112112
if self.drop_existing {
113-
descriptions.push("Drop table".to_string());
113+
descriptions.push(setup::ChangeDescription::Action("Drop table".to_string()));
114114
}
115115
if let Some(table_upsertion) = &self.table_upsertion {
116116
match table_upsertion {
117117
TableUpsertionAction::Create { keys, values } => {
118-
descriptions.push(format!(
118+
descriptions.push(setup::ChangeDescription::Action(format!(
119119
"Create table:\n key columns: {}\n value columns: {}\n",
120120
keys.iter().map(|(k, v)| format!("{k} {v}")).join(", "),
121121
values.iter().map(|(k, v)| format!("{k} {v}")).join(", "),
122-
));
122+
)));
123123
}
124124
TableUpsertionAction::Update {
125125
columns_to_delete,
126126
columns_to_upsert,
127127
} => {
128128
if !columns_to_delete.is_empty() {
129-
descriptions.push(format!(
129+
descriptions.push(setup::ChangeDescription::Action(format!(
130130
"Delete column from table: {}",
131131
columns_to_delete.iter().join(", "),
132-
));
132+
)));
133133
}
134134
if !columns_to_upsert.is_empty() {
135-
descriptions.push(format!(
135+
descriptions.push(setup::ChangeDescription::Action(format!(
136136
"Add / update columns in table: {}",
137137
columns_to_upsert
138138
.iter()
139139
.map(|(k, v)| format!("{k} {v}"))
140140
.join(", "),
141-
));
141+
)));
142142
}
143143
}
144144
}

src/setup/components.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,23 +110,26 @@ impl<D: SetupOperator> SetupStatus<D> {
110110
}
111111

112112
impl<D: SetupOperator + Send + Sync> ResourceSetupStatus for SetupStatus<D> {
113-
fn describe_changes(&self) -> Vec<String> {
113+
fn describe_changes(&self) -> Vec<setup::ChangeDescription> {
114114
let mut result = vec![];
115115

116116
for key in &self.keys_to_delete {
117-
result.push(format!("Delete {}", self.desc.describe_key(key)));
117+
result.push(setup::ChangeDescription::Action(format!(
118+
"Delete {}",
119+
self.desc.describe_key(key)
120+
)));
118121
}
119122

120123
for state in &self.states_to_upsert {
121-
result.push(format!(
124+
result.push(setup::ChangeDescription::Action(format!(
122125
"{} {}",
123126
if state.already_exists {
124127
"Update"
125128
} else {
126129
"Create"
127130
},
128131
self.desc.describe_state(&state.state)
129-
));
132+
)));
130133
}
131134

132135
result
@@ -171,7 +174,7 @@ pub async fn apply_component_changes<D: SetupOperator>(
171174
}
172175

173176
impl<A: ResourceSetupStatus, B: ResourceSetupStatus> ResourceSetupStatus for (A, B) {
174-
fn describe_changes(&self) -> Vec<String> {
177+
fn describe_changes(&self) -> Vec<setup::ChangeDescription> {
175178
let mut result = vec![];
176179
result.extend(self.0.describe_changes());
177180
result.extend(self.1.describe_changes());

src/setup/db_metadata.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -331,11 +331,11 @@ impl MetadataTableSetup {
331331
}
332332

333333
impl ResourceSetupStatus for MetadataTableSetup {
334-
fn describe_changes(&self) -> Vec<String> {
334+
fn describe_changes(&self) -> Vec<setup::ChangeDescription> {
335335
if self.metadata_table_missing {
336-
vec![format!(
336+
vec![setup::ChangeDescription::Action(format!(
337337
"Create the cocoindex metadata table {SETUP_METADATA_TABLE_NAME}"
338-
)]
338+
))]
339339
} else {
340340
vec![]
341341
}

src/setup/driver.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,12 @@ async fn maybe_update_resource_setup<
387387
});
388388
writeln!(write, "{}:", resource.description)?;
389389
for change in setup_status.describe_changes() {
390-
writeln!(write, " - {change}")?;
390+
match change {
391+
setup::ChangeDescription::Action(action) => {
392+
writeln!(write, " - {action}")?;
393+
}
394+
setup::ChangeDescription::Note(_) => {}
395+
}
391396
}
392397
}
393398
}

src/setup/states.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -244,14 +244,19 @@ pub enum SetupChangeType {
244244
Invalid,
245245
}
246246

247+
pub enum ChangeDescription {
248+
Action(String),
249+
Note(String),
250+
}
251+
247252
pub trait ResourceSetupStatus: Send + Sync + Debug + Any + 'static {
248-
fn describe_changes(&self) -> Vec<String>;
253+
fn describe_changes(&self) -> Vec<ChangeDescription>;
249254

250255
fn change_type(&self) -> SetupChangeType;
251256
}
252257

253258
impl ResourceSetupStatus for Box<dyn ResourceSetupStatus> {
254-
fn describe_changes(&self) -> Vec<String> {
259+
fn describe_changes(&self) -> Vec<ChangeDescription> {
255260
self.as_ref().describe_changes()
256261
}
257262

@@ -261,7 +266,7 @@ impl ResourceSetupStatus for Box<dyn ResourceSetupStatus> {
261266
}
262267

263268
impl ResourceSetupStatus for std::convert::Infallible {
264-
fn describe_changes(&self) -> Vec<String> {
269+
fn describe_changes(&self) -> Vec<ChangeDescription> {
265270
unreachable!()
266271
}
267272

@@ -302,7 +307,19 @@ impl<K, S, C: ResourceSetupStatus> std::fmt::Display for ResourceSetupInfo<K, S,
302307
let mut f = indented(f).with_str(INDENT);
303308
writeln!(f, "{}", "TODO:".color(AnsiColors::BrightBlack))?;
304309
for change in changes {
305-
writeln!(f, " - {}", change.color(AnsiColors::BrightBlack))?;
310+
match change {
311+
ChangeDescription::Action(action) => {
312+
writeln!(f, " - {}", action.color(AnsiColors::BrightBlack))?;
313+
}
314+
ChangeDescription::Note(note) => {
315+
writeln!(
316+
f,
317+
" {} {}",
318+
"NOTE:".color(AnsiColors::Yellow).bold(),
319+
note.color(AnsiColors::Yellow)
320+
)?;
321+
}
322+
}
306323
}
307324
writeln!(f)?;
308325
}

0 commit comments

Comments
 (0)