@@ -89,24 +89,6 @@ fn format_action(action: &MigrationAction) -> String {
8989 column. bright_cyan( ) . bold( )
9090 )
9191 }
92- MigrationAction :: AddIndex { table, index } => {
93- format ! (
94- "{} {} {} {}" ,
95- "Add index:" . bright_green( ) ,
96- index. name. bright_cyan( ) . bold( ) ,
97- "on" . bright_white( ) ,
98- table. bright_cyan( )
99- )
100- }
101- MigrationAction :: RemoveIndex { table, name } => {
102- format ! (
103- "{} {} {} {}" ,
104- "Remove index:" . bright_red( ) ,
105- name. bright_cyan( ) . bold( ) ,
106- "from" . bright_white( ) ,
107- table. bright_cyan( )
108- )
109- }
11092 MigrationAction :: RenameTable { from, to } => {
11193 format ! (
11294 "{} {} {} {}" ,
@@ -171,6 +153,13 @@ fn format_constraint_type(constraint: &vespertide_core::TableConstraint) -> Stri
171153 vespertide_core:: TableConstraint :: Check { name, expr } => {
172154 format ! ( "{} CHECK ({})" , name, expr)
173155 }
156+ vespertide_core:: TableConstraint :: Index { name, columns } => {
157+ if let Some ( n) = name {
158+ format ! ( "{} INDEX ({})" , n, columns. join( ", " ) )
159+ } else {
160+ format ! ( "INDEX ({})" , columns. join( ", " ) )
161+ }
162+ }
174163 }
175164}
176165
@@ -230,7 +219,6 @@ mod tests {
230219 auto_increment: false ,
231220 columns: vec![ "id" . into( ) ] ,
232221 } ] ,
233- indexes : vec ! [ ] ,
234222 } ;
235223 let path = models_dir. join ( format ! ( "{name}.json" ) ) ;
236224 fs:: write ( path, serde_json:: to_string_pretty ( & table) . unwrap ( ) ) . unwrap ( ) ;
@@ -284,19 +272,24 @@ mod tests {
284272 format!( "{} {}.{}" , "Modify column type:" . bright_yellow( ) , "users" . bright_cyan( ) , "id" . bright_cyan( ) . bold( ) )
285273 ) ]
286274 #[ case(
287- MigrationAction :: AddIndex {
275+ MigrationAction :: AddConstraint {
288276 table: "users" . into( ) ,
289- index : vespertide_core:: IndexDef {
290- name: "idx" . into( ) ,
277+ constraint : vespertide_core:: TableConstraint :: Index {
278+ name: Some ( "idx" . into( ) ) ,
291279 columns: vec![ "id" . into( ) ] ,
292- unique: false ,
293280 } ,
294281 } ,
295- format!( "{} {} {} {}" , "Add index :" . bright_green( ) , "idx" . bright_cyan( ) . bold( ) , "on" . bright_white( ) , "users" . bright_cyan( ) )
282+ format!( "{} {} {} {}" , "Add constraint :" . bright_green( ) , "idx INDEX (id) " . bright_cyan( ) . bold( ) , "on" . bright_white( ) , "users" . bright_cyan( ) )
296283 ) ]
297284 #[ case(
298- MigrationAction :: RemoveIndex { table: "users" . into( ) , name: "idx" . into( ) } ,
299- format!( "{} {} {} {}" , "Remove index:" . bright_red( ) , "idx" . bright_cyan( ) . bold( ) , "from" . bright_white( ) , "users" . bright_cyan( ) )
285+ MigrationAction :: RemoveConstraint {
286+ table: "users" . into( ) ,
287+ constraint: vespertide_core:: TableConstraint :: Index {
288+ name: Some ( "idx" . into( ) ) ,
289+ columns: vec![ "id" . into( ) ] ,
290+ } ,
291+ } ,
292+ format!( "{} {} {} {}" , "Remove constraint:" . bright_red( ) , "idx INDEX (id)" . bright_cyan( ) . bold( ) , "from" . bright_white( ) , "users" . bright_cyan( ) )
300293 ) ]
301294 #[ case(
302295 MigrationAction :: RenameTable { from: "users" . into( ) , to: "accounts" . into( ) } ,
@@ -433,4 +426,24 @@ mod tests {
433426 let result = cmd_diff ( ) ;
434427 assert ! ( result. is_ok( ) ) ;
435428 }
429+
430+ #[ test]
431+ fn test_constraint_display_unnamed_index ( ) {
432+ let constraint = TableConstraint :: Index {
433+ name : None ,
434+ columns : vec ! [ "email" . into( ) , "username" . into( ) ] ,
435+ } ;
436+ let display = format_constraint_type ( & constraint) ;
437+ assert_eq ! ( display, "INDEX (email, username)" ) ;
438+ }
439+
440+ #[ test]
441+ fn test_constraint_display_named_index ( ) {
442+ let constraint = TableConstraint :: Index {
443+ name : Some ( "ix_users_email" . into ( ) ) ,
444+ columns : vec ! [ "email" . into( ) ] ,
445+ } ;
446+ let display = format_constraint_type ( & constraint) ;
447+ assert_eq ! ( display, "ix_users_email INDEX (email)" ) ;
448+ }
436449}
0 commit comments