Skip to content

Commit

Permalink
Resolve column position modification (#20)
Browse files Browse the repository at this point in the history
* Resolve column position modificication

* bug fix

* update version

* update CHANGELOG
  • Loading branch information
hatajoe authored Feb 10, 2017
1 parent a567b24 commit 4e10b3c
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 18 deletions.
21 changes: 21 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
## 0.4.2 (2017-02-10)

Minor feature released

### Added

- resolve column position modification

### Deprecated

- Nothing

### Removed

- Nothing

### Fixed

- Fix SEGV error


## 0.4.1 (2017-02-08)

Add migrate table collation
Expand Down
13 changes: 8 additions & 5 deletions builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,17 @@ func willAlterTableCharacterSet(old, new *mysql.Table) string {
alter := []string{}
if old.GetCharset() != new.GetCharset() {
alter = append(alter, new.ToConvertCharsetSQL())
old.TableCollation = new.TableCollation
}
old.TableCollation = new.TableCollation

return new.ToAlterSQL(alter)
}

func willAlterColumnCharacterSet(old, new *mysql.Table) []string {
if old == nil || new == nil {
return []string{}
}

newCols := new.Columns.GroupByColumnName()
oldCols := old.Columns.GroupByColumnName()
sqls := []string{}
Expand Down Expand Up @@ -134,18 +138,17 @@ func willModifyColumn(old, new *mysql.Table) []string {
oldCol := oldCols[colName]
oldTableSchema := oldCol.TableSchema
oldColumnKey := oldCol.ColumnKey
oldOrdinalPosition := oldCol.OrdinalPosition
oldPrivileges := oldCol.Privileges
oldCol.TableSchema = newCol.TableSchema
oldCol.ColumnKey = newCol.ColumnKey
oldCol.OrdinalPosition = newCol.OrdinalPosition
oldCol.Privileges = newCol.Privileges
if !reflect.DeepEqual(oldCol, newCol) {
sqls = append(sqls, newCol.ToModifySQL())
sql := newCol.ToModifySQL()
sql = fmt.Sprintf("%s %s", sql, newCol.AppendPos(new.Columns))
sqls = append(sqls, sql)
}
oldCol.TableSchema = oldTableSchema
oldCol.ColumnKey = oldColumnKey
oldCol.OrdinalPosition = oldOrdinalPosition
oldCol.Privileges = oldPrivileges
}
return sqls
Expand Down
2 changes: 1 addition & 1 deletion cmd/carpenter/version.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main

const Name string = "carpenter"
const Version string = "0.4.1"
const Version string = "0.4.2"
28 changes: 16 additions & 12 deletions dialect/mysql/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,21 +121,25 @@ func (m Columns) ToSQL() []string {
return sqls
}

func (m *Column) AppendPos(all Columns) string {
pos := "first"
if m.OrdinalPosition > 1 {
before := m.OrdinalPosition - 1
for _, v := range all {
if v.OrdinalPosition != before {
continue
}
pos = fmt.Sprintf("after %s", Quote(v.ColumnName))
break
}
}
return pos
}

func (m Columns) ToAddSQL(all Columns) []string {
sqls := make([]string, 0, len(m))
for _, col := range m {
pos := "first"
if col.OrdinalPosition > 1 {
before := col.OrdinalPosition - 1
for _, v := range all {
if v.OrdinalPosition != before {
continue
}
pos = fmt.Sprintf("after %s", Quote(v.ColumnName))
break
}
}
sqls = append(sqls, col.ToAddSQL(pos))
sqls = append(sqls, col.ToAddSQL(col.AppendPos(all)))
}
return sqls
}
Expand Down

0 comments on commit 4e10b3c

Please sign in to comment.