11use std:: fmt:: Debug ;
22
3+ use anyhow:: Ok ;
34use common:: bootstrap_model:: index:: {
45 database_index:: {
56 DatabaseIndexSpec ,
@@ -21,26 +22,35 @@ use serde::{
2122} ;
2223use 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
3745impl 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
77101impl 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}
0 commit comments