dialect/sql/schema: add IndexBuilder.IfNotExists and use for Create() case. (#1666)

Append-only mode creates tables with the `IF NOT EXISTS` annotation.
Make indices case symmetric with this.
This commit is contained in:
bshihr
2021-06-29 23:09:48 -07:00
committed by GitHub
parent 7ffdce4cef
commit 4066255641
5 changed files with 25 additions and 7 deletions

View File

@@ -524,6 +524,7 @@ type IndexBuilder struct {
Builder
name string
unique bool
exists bool
table string
columns []string
}
@@ -546,6 +547,12 @@ func CreateIndex(name string) *IndexBuilder {
return &IndexBuilder{name: name}
}
// IfNotExists appends the `IF NOT EXISTS` clause to the `CREATE INDEX` statement.
func (i *IndexBuilder) IfNotExists() *IndexBuilder {
i.exists = true
return i
}
// Unique sets the index to be a unique index.
func (i *IndexBuilder) Unique() *IndexBuilder {
i.unique = true
@@ -577,6 +584,9 @@ func (i *IndexBuilder) Query() (string, []interface{}) {
i.WriteString("UNIQUE ")
}
i.WriteString("INDEX ")
if i.exists {
i.WriteString("IF NOT EXISTS ")
}
i.Ident(i.name)
i.WriteString(" ON ")
i.Ident(i.table).Nested(func(b *Builder) {