mirror of
https://github.com/ent/ent.git
synced 2026-05-22 09:31:45 +03:00
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:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user