Skip to content

Commit 602fa7c

Browse files
sql/ydb: fixed index definition in create table (i hope so....)
1 parent 0a78dfb commit 602fa7c

File tree

1 file changed

+31
-24
lines changed

1 file changed

+31
-24
lines changed

sql/ydb/migrate.go

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -317,33 +317,13 @@ func (s *state) alterTable(table *schema.Table, changes []schema.Change) error {
317317
func (s *state) addIndexes(src schema.Change, table *schema.Table, indexes ...*schema.AddIndex) error {
318318
for _, add := range indexes {
319319
index := add.I
320-
indexAttrs := IndexAttributes{}
321-
hasAttrs := sqlx.Has(index.Attrs, &indexAttrs)
322320

323321
builder := s.Build("ALTER TABLE").
324322
Ident(s.tablePath(table)).
325323
P("ADD INDEX").
326-
Ident(index.Name).
327-
P("GLOBAL")
328-
329-
if index.Unique {
330-
builder.P("UNIQUE")
331-
}
332-
333-
if hasAttrs && indexAttrs.Async {
334-
builder.P("ASYNC")
335-
} else {
336-
builder.P("SYNC")
337-
}
338-
339-
builder.P("ON")
324+
Ident(index.Name)
340325

341-
s.indexParts(builder, index.Parts)
342-
343-
if hasAttrs && len(indexAttrs.CoverColumns) > 0 {
344-
builder.P("COVER")
345-
s.indexCoverColumns(builder, indexAttrs.CoverColumns)
346-
}
326+
s.buildIndexSpec(builder, index)
347327

348328
reverseOp := s.Build("ALTER TABLE").
349329
Ident(s.tablePath(table)).
@@ -423,9 +403,36 @@ func (s *state) column(builder *sqlx.Builder, column *schema.Column) error {
423403
}
424404

425405
// indexDef writes an inline index definition for CREATE TABLE.
426-
func (s *state) indexDef(builder *sqlx.Builder, idx *schema.Index) {
427-
builder.P("INDEX").Ident(idx.Name).P("GLOBAL ON")
406+
func (s *state) indexDef(builder *sqlx.Builder, index *schema.Index) {
407+
builder.P("INDEX").Ident(index.Name)
408+
s.buildIndexSpec(builder, index)
409+
}
410+
411+
// buildIndexSpec writes the common index specification:
412+
// GLOBAL [UNIQUE] [SYNC|ASYNC] ON (columns) [COVER (columns)].
413+
func (s *state) buildIndexSpec(builder *sqlx.Builder, idx *schema.Index) {
414+
indexAttrs := IndexAttributes{}
415+
hasAttrs := sqlx.Has(idx.Attrs, &indexAttrs)
416+
417+
builder.P("GLOBAL")
418+
419+
if idx.Unique {
420+
builder.P("UNIQUE")
421+
}
422+
423+
if hasAttrs && indexAttrs.Async {
424+
builder.P("ASYNC")
425+
} else {
426+
builder.P("SYNC")
427+
}
428+
429+
builder.P("ON")
428430
s.indexParts(builder, idx.Parts)
431+
432+
if hasAttrs && len(indexAttrs.CoverColumns) > 0 {
433+
builder.P("COVER")
434+
s.indexCoverColumns(builder, indexAttrs.CoverColumns)
435+
}
429436
}
430437

431438
// indexParts writes the index parts (columns) to the builder.

0 commit comments

Comments
 (0)