Skip to content

Commit 892c78e

Browse files
authored
avoid duplicate comment opts (#64)
* avoid duplicate comment opts * correct compare 2 list elements
1 parent 94b12b7 commit 892c78e

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

element/column.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ func (c Column) pkDefinition(isPrev bool) (string, bool) {
186186
strSql := " " + c.typeDefinition(isPrev)
187187

188188
isPrimaryKey := false
189+
hasComment := false
189190
for _, opt := range attr.Options {
190191
if opt.Tp == ast.ColumnOptionPrimaryKey {
191192
isPrimaryKey = true
@@ -248,7 +249,14 @@ func (c Column) pkDefinition(isPrev bool) (string, bool) {
248249
if opt.Tp == ast.ColumnOptionDefaultValue {
249250
optStr = strings.ReplaceAll(optStr, "DEFAULT _UTF8MB4", "DEFAULT ")
250251
}
251-
strSql += " " + optStr
252+
253+
if !hasComment {
254+
strSql += " " + optStr
255+
}
256+
257+
if opt.Tp == ast.ColumnOptionComment {
258+
hasComment = true
259+
}
252260
}
253261

254262
return strSql, isPrimaryKey

element/table.go

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -262,18 +262,40 @@ func (t Table) getIndexForeignKey(fkName string) int {
262262
}
263263

264264
func hasChangedMysqlOptions(new, old []*ast.ColumnOption) bool {
265-
if len(new) != len(old) {
265+
optNew := make([]*ast.ColumnOption, 0, len(new))
266+
hasComment := false
267+
for _, v := range new {
268+
if !hasComment {
269+
optNew = append(optNew, v)
270+
}
271+
if v.Tp == ast.ColumnOptionComment {
272+
hasComment = true
273+
}
274+
}
275+
276+
optOld := make([]*ast.ColumnOption, 0, len(old))
277+
hasComment = false
278+
for _, v := range old {
279+
if !hasComment {
280+
optOld = append(optOld, v)
281+
}
282+
if v.Tp == ast.ColumnOptionComment {
283+
hasComment = true
284+
}
285+
}
286+
287+
if len(optNew) != len(optOld) {
266288
return true
267289
}
268290

269291
mNew := map[ast.ColumnOptionType]int{}
270-
for i := range old {
271-
mNew[old[i].Tp] += 1
292+
for i := range optNew {
293+
mNew[optNew[i].Tp] += 1
272294
}
273295

274296
mOld := map[ast.ColumnOptionType]int{}
275-
for i := range old {
276-
mOld[old[i].Tp] += 1
297+
for i := range optOld {
298+
mOld[optOld[i].Tp] += 1
277299
}
278300

279301
for k, v := range mOld {

0 commit comments

Comments
 (0)