Skip to content

Commit

Permalink
generate modify column
Browse files Browse the repository at this point in the history
  • Loading branch information
sunary committed Jul 19, 2021
1 parent b504fe6 commit fd31d2f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 34 deletions.
56 changes: 32 additions & 24 deletions element/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,29 +54,7 @@ func (c Column) migrationUp(tbName, after string, ident int) []string {
strSql += strings.Repeat(" ", ident-len(c.Name))
}

if !sql.IsPostgres && c.Typ != nil {
strSql += " " + c.Typ.String()
} else if sql.IsPostgres && c.PgTyp != nil {
strSql += " " + sql.FamilyName(int32(c.PgTyp.Family))
}

for _, opt := range c.Options {
b := bytes.NewBufferString("")
var ctx *format.RestoreCtx
if sql.IsLower {
ctx = format.NewRestoreCtx(LowerRestoreFlag, b)
} else {
ctx = format.NewRestoreCtx(UppercaseRestoreFlag, b)
}

if sql.IsPostgres && opt.Tp == ast.ColumnOptionDefaultValue {
strSql += " " + b.String()
continue
}

_ = opt.Restore(ctx)
strSql += " " + b.String()
}
strSql += c.definition()

if ident < 0 {
if after != "" {
Expand All @@ -92,7 +70,7 @@ func (c Column) migrationUp(tbName, after string, ident int) []string {
return []string{fmt.Sprintf(sql.AlterTableDropColumnStm(), utils.EscapeSqlName(sql.IsPostgres, tbName), utils.EscapeSqlName(sql.IsPostgres, c.Name))}

case MigrateModifyAction:
return nil
return []string{fmt.Sprintf(sql.AlterTableModifyColumnStm(), utils.EscapeSqlName(sql.IsPostgres, tbName), utils.EscapeSqlName(sql.IsPostgres, c.Name)+c.definition())}

case MigrateRenameAction:
return []string{fmt.Sprintf(sql.AlterTableRenameColumnStm(), utils.EscapeSqlName(sql.IsPostgres, tbName), utils.EscapeSqlName(sql.IsPostgres, c.OldName), utils.EscapeSqlName(sql.IsPostgres, c.Name))}
Expand All @@ -114,6 +92,7 @@ func (c Column) migrationDown(tbName, after string) []string {
c.Action = MigrateAddAction

case MigrateModifyAction:
return nil

case MigrateRenameAction:
c.Name, c.OldName = c.OldName, c.Name
Expand All @@ -124,3 +103,32 @@ func (c Column) migrationDown(tbName, after string) []string {

return c.migrationUp(tbName, after, -1)
}

func (c Column) definition() string {
strSql := ""
if !sql.IsPostgres && c.Typ != nil {
strSql += " " + c.Typ.String()
} else if sql.IsPostgres && c.PgTyp != nil {
strSql += " " + sql.FamilyName(int32(c.PgTyp.Family))
}

for _, opt := range c.Options {
b := bytes.NewBufferString("")
var ctx *format.RestoreCtx
if sql.IsLower {
ctx = format.NewRestoreCtx(LowerRestoreFlag, b)
} else {
ctx = format.NewRestoreCtx(UppercaseRestoreFlag, b)
}

if sql.IsPostgres && opt.Tp == ast.ColumnOptionDefaultValue {
strSql += " " + b.String()
continue
}

_ = opt.Restore(ctx)
strSql += " " + b.String()
}

return strSql
}
14 changes: 8 additions & 6 deletions element/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@ func (t *Table) AddColumn(col Column) {
default:
t.Columns[id].Options = append(t.Columns[id].Options, col.Options...)

size := len(t.Columns[id].Options)
for i := range t.Columns[id].Options[:size-1] {
if t.Columns[id].Options[i].Tp == ast.ColumnOptionPrimaryKey {
t.Columns[id].Options[i], t.Columns[id].Options[size-1] = t.Columns[id].Options[size-1], t.Columns[id].Options[i]
break
if size := len(t.Columns[id].Options); size > 0 {
for i := range t.Columns[id].Options[:size-1] {
if t.Columns[id].Options[i].Tp == ast.ColumnOptionPrimaryKey {
t.Columns[id].Options[i], t.Columns[id].Options[size-1] = t.Columns[id].Options[size-1], t.Columns[id].Options[i]
break
}
}
}

Expand Down Expand Up @@ -194,7 +195,8 @@ func (t Table) getIndexIndex(idxName string) int {
func (t *Table) Diff(old Table) {
for i := range t.Columns {
if j := old.getIndexColumn(t.Columns[i].Name); t.Columns[i].Action == MigrateAddAction && j >= 0 {
if t.Columns[i].Typ == old.Columns[j].Typ {
if (t.Columns[i].Typ != nil && t.Columns[i].Typ.String() == old.Columns[j].Typ.String()) ||
(t.Columns[i].PgTyp != nil && t.Columns[i].PgTyp == old.Columns[j].PgTyp) {
t.Columns[i].Action = MigrateNoAction
} else {
t.Columns[i] = old.Columns[j]
Expand Down
10 changes: 6 additions & 4 deletions sql-parser/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,13 @@ func (p *Parser) Enter(in ast.Node) (ast.Node, bool) {

case ast.AlterTableModifyColumn:
if len(alter.Specs[i].NewColumns) > 0 {
col := element.Column{
Node: element.Node{Name: alter.Specs[i].OldColumnName.Name.O, Action: element.MigrateModifyAction},
Typ: alter.Specs[i].NewColumns[0].Tp,
for j := range alter.Specs[i].NewColumns {
col := element.Column{
Node: element.Node{Name: alter.Specs[i].NewColumns[j].Name.Name.O, Action: element.MigrateModifyAction},
Typ: alter.Specs[i].NewColumns[j].Tp,
}
p.Migration.AddColumn(alter.Table.Name.O, col)
}
p.Migration.AddColumn(alter.Table.Name.O, col)
}

case ast.AlterTableRenameColumn:
Expand Down

0 comments on commit fd31d2f

Please sign in to comment.