@@ -2,7 +2,8 @@ use std::{cmp::Ordering, collections::HashSet};
2
2
3
3
use sqlparser:: ast:: {
4
4
AlterTableOperation , AlterType , AlterTypeAddValue , AlterTypeAddValuePosition ,
5
- AlterTypeOperation , CreateTable , Ident , ObjectName , Statement , UserDefinedTypeRepresentation ,
5
+ AlterTypeOperation , CreateIndex , CreateTable , Ident , ObjectName , ObjectType , Statement ,
6
+ UserDefinedTypeRepresentation ,
6
7
} ;
7
8
8
9
pub trait Diff : Sized {
@@ -21,6 +22,7 @@ impl Diff for Vec<Statement> {
21
22
// CreateTable: compare against another CreateTable with the same name
22
23
// TODO: handle renames (e.g. use comments to tag a previous name for a table in a schema)
23
24
Statement :: CreateTable ( a) => find_and_compare_create_table ( sa, a, other) ,
25
+ Statement :: CreateIndex ( a) => find_and_compare_create_index ( sa, a, other) ,
24
26
Statement :: CreateType { name, .. } => find_and_compare_create_type ( sa, name, other) ,
25
27
Statement :: CreateExtension {
26
28
name,
@@ -37,6 +39,10 @@ impl Diff for Vec<Statement> {
37
39
Statement :: CreateTable ( a) => a. name == b. name ,
38
40
_ => false ,
39
41
} ) ,
42
+ Statement :: CreateIndex ( b) => self . iter ( ) . find ( |sa| match sa {
43
+ Statement :: CreateIndex ( a) => a. name == b. name ,
44
+ _ => false ,
45
+ } ) ,
40
46
Statement :: CreateType { name : b_name, .. } => self . iter ( ) . find ( |sa| match sa {
41
47
Statement :: CreateType { name : a_name, .. } => a_name == b_name,
42
48
_ => false ,
@@ -107,6 +113,36 @@ fn find_and_compare_create_table(
107
113
)
108
114
}
109
115
116
+ fn find_and_compare_create_index (
117
+ sa : & Statement ,
118
+ a : & CreateIndex ,
119
+ other : & [ Statement ] ,
120
+ ) -> Option < Vec < Statement > > {
121
+ find_and_compare (
122
+ sa,
123
+ other,
124
+ |sb| match sb {
125
+ Statement :: CreateIndex ( b) => a. name == b. name ,
126
+ _ => false ,
127
+ } ,
128
+ || {
129
+ let name = a
130
+ . name
131
+ . clone ( )
132
+ . expect ( "can't drop an unnamed index, please ensure all indexes are named" ) ;
133
+ Some ( vec ! [ Statement :: Drop {
134
+ object_type: sqlparser:: ast:: ObjectType :: Index ,
135
+ if_exists: a. if_not_exists,
136
+ names: vec![ name] ,
137
+ cascade: false ,
138
+ restrict: false ,
139
+ purge: false ,
140
+ temporary: false ,
141
+ } ] )
142
+ } ,
143
+ )
144
+ }
145
+
110
146
fn find_and_compare_create_type (
111
147
sa : & Statement ,
112
148
a_name : & ObjectName ,
@@ -170,6 +206,10 @@ impl Diff for Statement {
170
206
Self :: CreateTable ( b) => compare_create_table ( a, b) ,
171
207
_ => None ,
172
208
} ,
209
+ Self :: CreateIndex ( a) => match other {
210
+ Self :: CreateIndex ( b) => compare_create_index ( a, b) ,
211
+ _ => None ,
212
+ } ,
173
213
Self :: CreateType {
174
214
name : a_name,
175
215
representation : a_rep,
@@ -233,6 +273,30 @@ fn compare_create_table(a: &CreateTable, b: &CreateTable) -> Option<Vec<Statemen
233
273
} ] )
234
274
}
235
275
276
+ fn compare_create_index ( a : & CreateIndex , b : & CreateIndex ) -> Option < Vec < Statement > > {
277
+ if a == b {
278
+ return None ;
279
+ }
280
+
281
+ if a. name . is_none ( ) || b. name . is_none ( ) {
282
+ panic ! ( "can't diff unnamed indexes!" ) ;
283
+ }
284
+ let name = a. name . clone ( ) . unwrap ( ) ;
285
+
286
+ Some ( vec ! [
287
+ Statement :: Drop {
288
+ object_type: ObjectType :: Index ,
289
+ if_exists: a. if_not_exists,
290
+ names: vec![ name] ,
291
+ cascade: false ,
292
+ restrict: false ,
293
+ purge: false ,
294
+ temporary: false ,
295
+ } ,
296
+ Statement :: CreateIndex ( b. clone( ) ) ,
297
+ ] )
298
+ }
299
+
236
300
fn compare_create_type (
237
301
a_name : & ObjectName ,
238
302
a_rep : & UserDefinedTypeRepresentation ,
0 commit comments