diff --git a/dialect/entsql/annotation.go b/dialect/entsql/annotation.go index 598d781ec..30e9d12ed 100644 --- a/dialect/entsql/annotation.go +++ b/dialect/entsql/annotation.go @@ -218,8 +218,7 @@ type IndexAnnotation struct { // // index.Fields("c1", "c2", "c3"). // Annotation( - // entsql.DescColumn("c1"), - // entsql.DescColumn("c2"), + // entsql.DescColumns("c1", "c2"), // ) // // CREATE INDEX `table_c1_c2_c3` ON `table`(`c1` DESC, `c2` DESC, `c3`) diff --git a/dialect/sql/schema/atlas.go b/dialect/sql/schema/atlas.go index f57c02ae6..0f5f8ef26 100644 --- a/dialect/sql/schema/atlas.go +++ b/dialect/sql/schema/atlas.go @@ -468,6 +468,10 @@ func (m *Migrate) aIndexes(b atBuilder, t1 *Table, t2 *schema.Table) error { if err := b.atIndex(idx1, t2, idx2); err != nil { return err } + desc := descIndexes(idx1) + for _, p := range idx2.Parts { + p.Desc = desc[p.C.Name] + } t2.AddIndexes(idx2) } return nil @@ -493,3 +497,20 @@ func setAtChecks(t1 *Table, t2 *schema.Table) { } } } + +// descIndexes returns a map holding the DESC mapping if exist. +func descIndexes(idx *Index) map[string]bool { + descs := make(map[string]bool) + if idx.Annotation == nil { + return descs + } + // If DESC (without a column) was defined on the + // annotation, map it to the single column index. + if idx.Annotation.Desc && len(idx.Columns) == 1 { + descs[idx.Columns[0].Name] = idx.Annotation.Desc + } + for column, desc := range idx.Annotation.DescColumns { + descs[column] = desc + } + return descs +} diff --git a/entc/integration/migrate/entv2/migrate/schema.go b/entc/integration/migrate/entv2/migrate/schema.go index ecbae1aa1..deb91d074 100644 --- a/entc/integration/migrate/entv2/migrate/schema.go +++ b/entc/integration/migrate/entv2/migrate/schema.go @@ -162,6 +162,14 @@ var ( Unique: true, Columns: []*schema.Column{UsersColumns[7], UsersColumns[3]}, }, + { + Name: "user_age", + Unique: false, + Columns: []*schema.Column{UsersColumns[3]}, + Annotation: &entsql.IndexAnnotation{ + Desc: true, + }, + }, }, } // FriendsColumns holds the columns for the "friends" table. diff --git a/entc/integration/migrate/entv2/schema/user.go b/entc/integration/migrate/entv2/schema/user.go index 073010c2c..eb593c47f 100644 --- a/entc/integration/migrate/entv2/schema/user.go +++ b/entc/integration/migrate/entv2/schema/user.go @@ -124,6 +124,8 @@ func (User) Indexes() []ent.Index { // and defining a new one. index.Fields("phone", "age"). Unique(), + index.Fields("age"). + Annotations(entsql.Desc()), } } diff --git a/go.mod b/go.mod index fcee900d8..ddbf13eb5 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module entgo.io/ent go 1.17 require ( - ariga.io/atlas v0.3.2-0.20220120225051-c3fac7d636dd + ariga.io/atlas v0.3.3-0.20220131101532-28a925069338 github.com/DATA-DOG/go-sqlmock v1.5.0 github.com/go-openapi/inflect v0.19.0 github.com/go-sql-driver/mysql v1.6.0 diff --git a/go.sum b/go.sum index 43177b60b..4bfc0d10f 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,9 @@ ariga.io/atlas v0.3.2-0.20220120225051-c3fac7d636dd h1:YxnJl3ySvwQ3C7Rspa4CrQtwrftTZ0F8WJ36CvY7nWE= ariga.io/atlas v0.3.2-0.20220120225051-c3fac7d636dd/go.mod h1:XcLUpQX7Cq4qtagEHIleq3MJaBeeJ76BS8doc4gkOJk= +ariga.io/atlas v0.3.2 h1:rE0OMGK08+5fg2Q8ukFkKSjygJx7pyH7UsJveCQ62Rw= +ariga.io/atlas v0.3.2/go.mod h1:XcLUpQX7Cq4qtagEHIleq3MJaBeeJ76BS8doc4gkOJk= +ariga.io/atlas v0.3.3-0.20220131101532-28a925069338 h1:QBuKDC1U+Jm7UlFR199MOheUi447rnP8lfvsihgUn/s= +ariga.io/atlas v0.3.3-0.20220131101532-28a925069338/go.mod h1:XcLUpQX7Cq4qtagEHIleq3MJaBeeJ76BS8doc4gkOJk= cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= cloud.google.com/go v0.38.0/go.mod h1:990N+gfupTy94rShfmMCWGDn0LpTmnzTp2qbd1dvSRU=