dialect/sql/schema: alter column for postgres

Summary: Pull Request resolved: https://github.com/facebookincubator/ent/pull/117

Reviewed By: alexsn

Differential Revision: D18083914

fbshipit-source-id: a5f6993cfe9a260a84c0d4ab868e3e797b3a5776
This commit is contained in:
Ariel Mashraki
2019-10-23 05:37:42 -07:00
committed by Facebook Github Bot
parent a0c7ee77dc
commit c414cd9a82
8 changed files with 83 additions and 17 deletions

View File

@@ -230,7 +230,7 @@ func (d *Postgres) tBuilder(t *Table) *sql.TableBuilder {
b := sql.Dialect(dialect.Postgres).
CreateTable(t.Name).IfNotExists()
for _, c := range t.Columns {
b.Column(d.cBuilder(c))
b.Column(d.addColumn(c))
}
for _, pk := range t.PrimaryKey {
b.PrimaryKey(pk.Name)
@@ -274,8 +274,8 @@ func (d *Postgres) cType(c *Column) (t string) {
return t
}
// cBuilder returns the ColumnBuilder for the given column.
func (d *Postgres) cBuilder(c *Column) *sql.ColumnBuilder {
// addColumn returns the ColumnBuilder for adding the given column to a table.
func (d *Postgres) addColumn(c *Column) *sql.ColumnBuilder {
b := sql.Dialect(dialect.Postgres).
Column(c.Name).Type(d.cType(c)).Attr(c.Attr)
c.unique(b)
@@ -286,3 +286,15 @@ func (d *Postgres) cBuilder(c *Column) *sql.ColumnBuilder {
c.defaultValue(b)
return b
}
// alterColumn returns list of ColumnBuilder for applying in order to alter a column.
func (d *Postgres) alterColumn(c *Column) (ops []*sql.ColumnBuilder) {
b := sql.Dialect(dialect.Postgres)
ops = append(ops, b.Column(c.Name).Type(d.cType(c)))
if c.Nullable {
ops = append(ops, b.Column(c.Name).Attr("DROP NOT NULL"))
} else {
ops = append(ops, b.Column(c.Name).Attr("SET NOT NULL"))
}
return ops
}