mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
dialect/sql/schema: support for USING method in create index builder (#1730)
* Support for USING 'method' in create index * Add USING 'method' in create index support for MySQL * Fix USING 'method' for MySQL to be a raw string
This commit is contained in:
@@ -526,6 +526,7 @@ type IndexBuilder struct {
|
||||
unique bool
|
||||
exists bool
|
||||
table string
|
||||
method string
|
||||
columns []string
|
||||
}
|
||||
|
||||
@@ -565,6 +566,12 @@ func (i *IndexBuilder) Table(table string) *IndexBuilder {
|
||||
return i
|
||||
}
|
||||
|
||||
// Using sets the method to create the index with.
|
||||
func (i *IndexBuilder) Using(method string) *IndexBuilder {
|
||||
i.method = method
|
||||
return i
|
||||
}
|
||||
|
||||
// Column appends a column to the column list for the index.
|
||||
func (i *IndexBuilder) Column(column string) *IndexBuilder {
|
||||
i.columns = append(i.columns, column)
|
||||
@@ -589,9 +596,27 @@ func (i *IndexBuilder) Query() (string, []interface{}) {
|
||||
}
|
||||
i.Ident(i.name)
|
||||
i.WriteString(" ON ")
|
||||
i.Ident(i.table).Nested(func(b *Builder) {
|
||||
b.IdentComma(i.columns...)
|
||||
})
|
||||
i.Ident(i.table)
|
||||
switch i.dialect {
|
||||
case dialect.Postgres:
|
||||
if i.method != "" {
|
||||
i.WriteString(" USING ").Ident(i.method)
|
||||
}
|
||||
i.Nested(func(b *Builder) {
|
||||
b.IdentComma(i.columns...)
|
||||
})
|
||||
case dialect.MySQL:
|
||||
i.Nested(func(b *Builder) {
|
||||
b.IdentComma(i.columns...)
|
||||
})
|
||||
if i.method != "" {
|
||||
i.WriteString(" USING " + i.method)
|
||||
}
|
||||
default:
|
||||
i.Nested(func(b *Builder) {
|
||||
b.IdentComma(i.columns...)
|
||||
})
|
||||
}
|
||||
return i.String(), nil
|
||||
}
|
||||
|
||||
|
||||
@@ -1320,6 +1320,24 @@ func TestBuilder(t *testing.T) {
|
||||
Column("name"),
|
||||
wantQuery: `CREATE INDEX IF NOT EXISTS "name_index" ON "users"("name")`,
|
||||
},
|
||||
{
|
||||
input: Dialect(dialect.Postgres).
|
||||
CreateIndex("name_index").
|
||||
IfNotExists().
|
||||
Table("users").
|
||||
Using("gin").
|
||||
Column("name"),
|
||||
wantQuery: `CREATE INDEX IF NOT EXISTS "name_index" ON "users" USING "gin"("name")`,
|
||||
},
|
||||
{
|
||||
input: Dialect(dialect.MySQL).
|
||||
CreateIndex("name_index").
|
||||
IfNotExists().
|
||||
Table("users").
|
||||
Using("HASH").
|
||||
Column("name"),
|
||||
wantQuery: "CREATE INDEX IF NOT EXISTS `name_index` ON `users`(`name`) USING HASH",
|
||||
},
|
||||
{
|
||||
input: CreateIndex("unique_name").Unique().Table("users").Columns("first", "last"),
|
||||
wantQuery: "CREATE UNIQUE INDEX `unique_name` ON `users`(`first`, `last`)",
|
||||
|
||||
Reference in New Issue
Block a user