@@ -307,33 +307,13 @@ func (s *state) alterTable(table *schema.Table, changes []schema.Change) error {
307307func (s * state ) addIndexes (src schema.Change , table * schema.Table , indexes ... * schema.AddIndex ) error {
308308 for _ , add := range indexes {
309309 index := add .I
310- indexAttrs := IndexAttributes {}
311- hasAttrs := sqlx .Has (index .Attrs , & indexAttrs )
312310
313311 builder := s .Build ("ALTER TABLE" ).
314312 Ident (s .tablePath (table )).
315313 P ("ADD INDEX" ).
316- Ident (index .Name ).
317- P ("GLOBAL" )
318-
319- if index .Unique {
320- builder .P ("UNIQUE" )
321- }
322-
323- if hasAttrs && indexAttrs .Async {
324- builder .P ("ASYNC" )
325- } else {
326- builder .P ("SYNC" )
327- }
328-
329- builder .P ("ON" )
314+ Ident (index .Name )
330315
331- s .indexParts (builder , index .Parts )
332-
333- if hasAttrs && len (indexAttrs .CoverColumns ) > 0 {
334- builder .P ("COVER" )
335- s .indexCoverColumns (builder , indexAttrs .CoverColumns )
336- }
316+ s .buildIndexSpec (builder , index )
337317
338318 reverseOp := s .Build ("ALTER TABLE" ).
339319 Ident (s .tablePath (table )).
@@ -413,9 +393,36 @@ func (s *state) column(builder *sqlx.Builder, column *schema.Column) error {
413393}
414394
415395// indexDef writes an inline index definition for CREATE TABLE.
416- func (s * state ) indexDef (builder * sqlx.Builder , idx * schema.Index ) {
417- builder .P ("INDEX" ).Ident (idx .Name ).P ("GLOBAL ON" )
396+ func (s * state ) indexDef (builder * sqlx.Builder , index * schema.Index ) {
397+ builder .P ("INDEX" ).Ident (index .Name )
398+ s .buildIndexSpec (builder , index )
399+ }
400+
401+ // buildIndexSpec writes the common index specification:
402+ // GLOBAL [UNIQUE] [SYNC|ASYNC] ON (columns) [COVER (columns)].
403+ func (s * state ) buildIndexSpec (builder * sqlx.Builder , idx * schema.Index ) {
404+ indexAttrs := IndexAttributes {}
405+ hasAttrs := sqlx .Has (idx .Attrs , & indexAttrs )
406+
407+ builder .P ("GLOBAL" )
408+
409+ if idx .Unique {
410+ builder .P ("UNIQUE" )
411+ }
412+
413+ if hasAttrs && indexAttrs .Async {
414+ builder .P ("ASYNC" )
415+ } else {
416+ builder .P ("SYNC" )
417+ }
418+
419+ builder .P ("ON" )
418420 s .indexParts (builder , idx .Parts )
421+
422+ if hasAttrs && len (indexAttrs .CoverColumns ) > 0 {
423+ builder .P ("COVER" )
424+ s .indexCoverColumns (builder , indexAttrs .CoverColumns )
425+ }
419426}
420427
421428// indexParts writes the index parts (columns) to the builder.
0 commit comments