Skip to content

Commit f163395

Browse files
nipunn1313Convex, Inc.
authored andcommitted
Add staged flag to the deployment audit log / API response (#40153)
Added/updated test to make sure this is backwards compatible. GitOrigin-RevId: 53f23a125d2aa9b17bd71f938dcaa797c91c4675
1 parent 834f191 commit f163395

File tree

6 files changed

+96
-49
lines changed

6 files changed

+96
-49
lines changed

crates/common/src/bootstrap_model/index/database_index/index_config.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,14 @@ pub struct SerializedDatabaseIndexSpec {
2121
fields: Vec<String>,
2222
}
2323

24-
impl TryFrom<DatabaseIndexSpec> for SerializedDatabaseIndexSpec {
25-
type Error = anyhow::Error;
26-
27-
fn try_from(config: DatabaseIndexSpec) -> anyhow::Result<Self> {
28-
Ok(Self {
24+
impl From<DatabaseIndexSpec> for SerializedDatabaseIndexSpec {
25+
fn from(config: DatabaseIndexSpec) -> Self {
26+
Self {
2927
fields: Vec::<FieldPath>::from(config.fields)
3028
.into_iter()
3129
.map(String::from)
3230
.collect(),
33-
})
31+
}
3432
}
3533
}
3634

crates/common/src/bootstrap_model/index/index_config.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,21 +212,21 @@ impl TryFrom<IndexConfig> for SerializedIndexConfig {
212212
spec,
213213
on_disk_state,
214214
} => SerializedIndexConfig::Database {
215-
spec: spec.try_into()?,
215+
spec: spec.into(),
216216
on_disk_state: on_disk_state.try_into()?,
217217
},
218218
IndexConfig::Text {
219219
spec,
220220
on_disk_state,
221221
} => SerializedIndexConfig::Search {
222-
spec: spec.try_into()?,
222+
spec: spec.into(),
223223
on_disk_state: on_disk_state.try_into()?,
224224
},
225225
IndexConfig::Vector {
226226
spec,
227227
on_disk_state,
228228
} => SerializedIndexConfig::Vector {
229-
spec: spec.try_into()?,
229+
spec: spec.into(),
230230
on_disk_state: on_disk_state.try_into()?,
231231
},
232232
})

crates/common/src/bootstrap_model/index/text_index/index_config.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,12 @@ pub struct SerializedTextIndexSpec {
2626
filter_fields: Vec<String>,
2727
}
2828

29-
impl TryFrom<TextIndexSpec> for SerializedTextIndexSpec {
30-
type Error = anyhow::Error;
31-
32-
fn try_from(config: TextIndexSpec) -> anyhow::Result<Self> {
33-
Ok(Self {
29+
impl From<TextIndexSpec> for SerializedTextIndexSpec {
30+
fn from(config: TextIndexSpec) -> Self {
31+
Self {
3432
search_field: config.search_field.into(),
3533
filter_fields: config.filter_fields.into_iter().map(String::from).collect(),
36-
})
34+
}
3735
}
3836
}
3937

crates/common/src/bootstrap_model/index/vector_index/index_config.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,13 @@ pub struct SerializedVectorIndexSpec {
3535
filter_fields: Vec<String>,
3636
}
3737

38-
impl TryFrom<VectorIndexSpec> for SerializedVectorIndexSpec {
39-
type Error = anyhow::Error;
40-
41-
fn try_from(config: VectorIndexSpec) -> anyhow::Result<Self> {
42-
Ok(Self {
38+
impl From<VectorIndexSpec> for SerializedVectorIndexSpec {
39+
fn from(config: VectorIndexSpec) -> Self {
40+
Self {
4341
dimensions: u32::from(config.dimensions) as i64,
4442
vector_field: config.vector_field.into(),
4543
filter_fields: config.filter_fields.into_iter().map(String::from).collect(),
46-
})
44+
}
4745
}
4846
}
4947

crates/model/src/deployment_audit_log/developer_index_config.rs

Lines changed: 80 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::fmt::Debug;
22

3+
use anyhow::Ok;
34
use common::bootstrap_model::index::{
45
database_index::{
56
DatabaseIndexSpec,
@@ -21,26 +22,35 @@ use serde::{
2122
};
2223
use value::codegen_convex_serialization;
2324

24-
// Index config that's specified by the developer
25+
// Index config that's specified by the developer - including spec + staged
26+
// state
2527
#[derive(Debug, Clone, PartialEq, Eq)]
2628
#[cfg_attr(any(test, feature = "testing"), derive(proptest_derive::Arbitrary))]
27-
pub enum DeveloperIndexConfig {
29+
pub struct DeveloperIndexConfig {
30+
spec: DeveloperIndexSpec,
31+
staged: bool,
32+
}
33+
34+
#[derive(Debug, Clone, PartialEq, Eq)]
35+
#[cfg_attr(any(test, feature = "testing"), derive(proptest_derive::Arbitrary))]
36+
pub enum DeveloperIndexSpec {
2837
/// Standard database index.
2938
Database(DatabaseIndexSpec),
30-
3139
/// Full text search index.
3240
Search(TextIndexSpec),
33-
41+
/// Vector search index.
3442
Vector(VectorIndexSpec),
3543
}
3644

3745
impl From<IndexConfig> for DeveloperIndexConfig {
3846
fn from(value: IndexConfig) -> Self {
39-
match value {
40-
IndexConfig::Database { spec, .. } => DeveloperIndexConfig::Database(spec),
41-
IndexConfig::Text { spec, .. } => DeveloperIndexConfig::Search(spec),
42-
IndexConfig::Vector { spec, .. } => DeveloperIndexConfig::Vector(spec),
43-
}
47+
let staged = value.is_staged();
48+
let spec = match value {
49+
IndexConfig::Database { spec, .. } => DeveloperIndexSpec::Database(spec),
50+
IndexConfig::Text { spec, .. } => DeveloperIndexSpec::Search(spec),
51+
IndexConfig::Vector { spec, .. } => DeveloperIndexSpec::Vector(spec),
52+
};
53+
Self { spec, staged }
4454
}
4555
}
4656

@@ -56,32 +66,56 @@ pub struct SerializedNamedDeveloperIndexConfig {
5666
#[derive(Serialize, Deserialize, Debug, Clone)]
5767
#[serde(tag = "type", rename_all = "camelCase")]
5868
#[cfg_attr(any(test, feature = "testing"), derive(proptest_derive::Arbitrary))]
59-
pub enum SerializedDeveloperIndexConfig {
69+
pub struct SerializedDeveloperIndexConfig {
70+
#[serde(flatten)]
71+
spec: SerializedDeveloperIndexSpec,
72+
#[serde(default)]
73+
staged: bool,
74+
}
75+
76+
#[derive(Serialize, Deserialize, Debug, Clone)]
77+
#[serde(tag = "type", rename_all = "camelCase")]
78+
#[cfg_attr(any(test, feature = "testing"), derive(proptest_derive::Arbitrary))]
79+
pub enum SerializedDeveloperIndexSpec {
6080
Database(SerializedDatabaseIndexSpec),
6181
Search(SerializedTextIndexSpec),
6282
Vector(SerializedVectorIndexSpec),
6383
}
6484

65-
impl TryFrom<DeveloperIndexConfig> for SerializedDeveloperIndexConfig {
66-
type Error = anyhow::Error;
67-
68-
fn try_from(index_config: DeveloperIndexConfig) -> anyhow::Result<Self> {
69-
Ok(match index_config {
70-
DeveloperIndexConfig::Database(config) => Self::Database(config.try_into()?),
71-
DeveloperIndexConfig::Search(config) => Self::Search(config.try_into()?),
72-
DeveloperIndexConfig::Vector(config) => Self::Vector(config.try_into()?),
73-
})
85+
impl From<DeveloperIndexConfig> for SerializedDeveloperIndexConfig {
86+
fn from(index_config: DeveloperIndexConfig) -> Self {
87+
let spec = match index_config.spec {
88+
DeveloperIndexSpec::Database(spec) => {
89+
SerializedDeveloperIndexSpec::Database(spec.into())
90+
},
91+
DeveloperIndexSpec::Search(spec) => SerializedDeveloperIndexSpec::Search(spec.into()),
92+
DeveloperIndexSpec::Vector(spec) => SerializedDeveloperIndexSpec::Vector(spec.into()),
93+
};
94+
Self {
95+
spec,
96+
staged: index_config.staged,
97+
}
7498
}
7599
}
76100

77101
impl TryFrom<SerializedDeveloperIndexConfig> for DeveloperIndexConfig {
78102
type Error = anyhow::Error;
79103

80104
fn try_from(index_config: SerializedDeveloperIndexConfig) -> anyhow::Result<Self> {
81-
Ok(match index_config {
82-
SerializedDeveloperIndexConfig::Database(config) => Self::Database(config.try_into()?),
83-
SerializedDeveloperIndexConfig::Search(config) => Self::Search(config.try_into()?),
84-
SerializedDeveloperIndexConfig::Vector(config) => Self::Vector(config.try_into()?),
105+
let spec = match index_config.spec {
106+
SerializedDeveloperIndexSpec::Database(spec) => {
107+
DeveloperIndexSpec::Database(spec.try_into()?)
108+
},
109+
SerializedDeveloperIndexSpec::Search(spec) => {
110+
DeveloperIndexSpec::Search(spec.try_into()?)
111+
},
112+
SerializedDeveloperIndexSpec::Vector(spec) => {
113+
DeveloperIndexSpec::Vector(spec.try_into()?)
114+
},
115+
};
116+
Ok(Self {
117+
spec,
118+
staged: index_config.staged,
85119
})
86120
}
87121
}
@@ -96,16 +130,35 @@ mod tests {
96130

97131
#[test]
98132
fn test_developer_index_config_serialization() -> anyhow::Result<()> {
99-
let config = DeveloperIndexConfig::Database(DatabaseIndexSpec {
100-
fields: IndexedFields::creation_time(),
101-
});
102-
let serialized = SerializedDeveloperIndexConfig::try_from(config.clone()).unwrap();
133+
let config = DeveloperIndexConfig {
134+
spec: DeveloperIndexSpec::Database(DatabaseIndexSpec {
135+
fields: IndexedFields::creation_time(),
136+
}),
137+
staged: false,
138+
};
139+
let serialized = SerializedDeveloperIndexConfig::from(config.clone());
103140
let json = serde_json::to_value(&serialized)?;
104141
let expected = serde_json::json!({
105142
"type": "database",
106143
"fields": ["_creationTime"],
144+
"staged": false,
107145
});
108146
assert_eq!(json, expected);
147+
148+
assert_eq!(
149+
config,
150+
serde_json::from_value::<SerializedDeveloperIndexConfig>(expected)?.try_into()?,
151+
);
152+
153+
let old_format = serde_json::json!({
154+
"type": "database",
155+
"fields": ["_creationTime"],
156+
});
157+
assert_eq!(
158+
config,
159+
serde_json::from_value::<SerializedDeveloperIndexConfig>(old_format)?.try_into()?,
160+
);
161+
109162
Ok(())
110163
}
111164
}

crates/model/src/deployment_audit_log/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ impl TryFrom<AuditLogIndexDiff> for SerializedIndexDiff {
537537
.into_iter()
538538
.map(|(name, config)| {
539539
let name = name.to_string();
540-
let index_config = SerializedDeveloperIndexConfig::try_from(config)?;
540+
let index_config = SerializedDeveloperIndexConfig::from(config);
541541
anyhow::Ok(SerializedNamedDeveloperIndexConfig { name, index_config })
542542
})
543543
.try_collect()

0 commit comments

Comments
 (0)