mirror of
https://github.com/ent/ent.git
synced 2026-05-22 09:31:45 +03:00
dialect/sql: add drop/add-index for older version of mysql (#315)
Add support for MySQL 5.6.35
This commit is contained in:
@@ -265,6 +265,28 @@ func (t *TableAlter) RenameIndex(curr, new string) *TableAlter {
|
||||
return t
|
||||
}
|
||||
|
||||
// DropIndex appends the `DROP INDEX` clause to the given `ALTER TABLE` statement.
|
||||
func (t *TableAlter) DropIndex(name string) *TableAlter {
|
||||
t.Queries = append(t.Queries, Raw(fmt.Sprintf("DROP INDEX %s", t.Quote(name))))
|
||||
return t
|
||||
}
|
||||
|
||||
// AddIndex appends the `ADD INDEX` clause to the given `ALTER TABLE` statement.
|
||||
func (t *TableAlter) AddIndex(idx *IndexBuilder) *TableAlter {
|
||||
b := &Builder{dialect: t.dialect}
|
||||
b.WriteString("ADD ")
|
||||
if idx.unique {
|
||||
b.WriteString("UNIQUE ")
|
||||
}
|
||||
b.WriteString("INDEX ")
|
||||
b.Ident(idx.name)
|
||||
b.Nested(func(b *Builder) {
|
||||
b.IdentComma(idx.columns...)
|
||||
})
|
||||
t.Queries = append(t.Queries, b)
|
||||
return t
|
||||
}
|
||||
|
||||
// AddForeignKey adds a foreign key constraint to the `ALTER TABLE` statement.
|
||||
func (t *TableAlter) AddForeignKey(fk *ForeignKeyBuilder) *TableAlter {
|
||||
t.Queries = append(t.Queries, &Wrapper{"ADD CONSTRAINT %s", fk})
|
||||
|
||||
@@ -200,6 +200,13 @@ func TestBuilder(t *testing.T) {
|
||||
input: AlterTable("users").RenameIndex("old", "new"),
|
||||
wantQuery: "ALTER TABLE `users` RENAME INDEX `old` TO `new`",
|
||||
},
|
||||
{
|
||||
input: AlterTable("users").
|
||||
DropIndex("old").
|
||||
AddIndex(CreateIndex("new1").Columns("c1", "c2")).
|
||||
AddIndex(CreateIndex("new2").Columns("c1", "c2").Unique()),
|
||||
wantQuery: "ALTER TABLE `users` DROP INDEX `old`, ADD INDEX `new1`(`c1`, `c2`), ADD UNIQUE INDEX `new2`(`c1`, `c2`)",
|
||||
},
|
||||
{
|
||||
input: Dialect(dialect.Postgres).AlterIndex("old").
|
||||
Rename("new"),
|
||||
|
||||
Reference in New Issue
Block a user