dialect/sql: add drop/add-index for older version of mysql (#315)

Add support for MySQL 5.6.35
This commit is contained in:
Ariel Mashraki
2020-01-23 20:25:48 +02:00
committed by GitHub
parent 3ee6223e64
commit cb5b9f6f9e
2 changed files with 29 additions and 0 deletions

View File

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

View File

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