dialect/sql/schema: initial work for incremental migration (#428)

This is a WIP PR and should be ignored this moment.
It's based on PR #221 created by Erik Hollensbe (He should
get his credit for his work before we land this).
This commit is contained in:
Ariel Mashraki
2020-04-12 19:12:33 +03:00
committed by GitHub
parent 8effe6dfeb
commit 2208b243db
11 changed files with 421 additions and 52 deletions

View File

@@ -382,3 +382,21 @@ func (d *Postgres) renameIndex(t *Table, old, new *Index) sql.Querier {
func (d *Postgres) tableSchema() sql.Querier {
return sql.Raw("(CURRENT_SCHEMA())")
}
// alterColumns returns the queries for applying the columns change-set.
func (d *Postgres) alterColumns(table string, add, modify, drop []*Column) sql.Queries {
b := sql.Dialect(dialect.Postgres).AlterTable(table)
for _, c := range add {
b.AddColumn(d.addColumn(c))
}
for _, c := range modify {
b.ModifyColumns(d.alterColumn(c)...)
}
for _, c := range drop {
b.DropColumn(sql.Dialect(dialect.Postgres).Column(c.Name))
}
if len(b.Queries) == 0 {
return nil
}
return sql.Queries{b}
}