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:
Tony Griezmann
2021-07-19 08:21:35 -04:00
committed by GitHub
parent 0f16f49fff
commit 201c7301aa
2 changed files with 46 additions and 3 deletions

View File

@@ -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
}

View File

@@ -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`)",