|
7 | 7 | "sort" |
8 | 8 | "strings" |
9 | 9 |
|
| 10 | + ptypes "github.com/auxten/postgresql-parser/pkg/sql/types" |
10 | 11 | "github.com/pingcap/parser/ast" |
| 12 | + "github.com/pingcap/parser/types" |
11 | 13 | "github.com/sunary/sqlize/utils" |
12 | 14 | ) |
13 | 15 |
|
@@ -252,16 +254,49 @@ func (t Table) getIndexForeignKey(fkName string) int { |
252 | 254 | return -1 |
253 | 255 | } |
254 | 256 |
|
| 257 | +func hasChangedMysqlOptions(new, old []*ast.ColumnOption) bool { |
| 258 | + if len(new) != len(old) { |
| 259 | + return true |
| 260 | + } |
| 261 | + |
| 262 | + mNew := map[ast.ColumnOptionType]int{} |
| 263 | + for i := range old { |
| 264 | + mNew[old[i].Tp] += 1 |
| 265 | + } |
| 266 | + |
| 267 | + mOld := map[ast.ColumnOptionType]int{} |
| 268 | + for i := range old { |
| 269 | + mOld[old[i].Tp] += 1 |
| 270 | + } |
| 271 | + |
| 272 | + for k, v := range mOld { |
| 273 | + if mNew[k] != v { |
| 274 | + return true |
| 275 | + } |
| 276 | + } |
| 277 | + |
| 278 | + return false |
| 279 | +} |
| 280 | + |
| 281 | +func hasChangedMysqlType(new, old *types.FieldType) bool { |
| 282 | + return new != nil && new.String() != old.String() |
| 283 | +} |
| 284 | + |
| 285 | +func hasChangePostgresType(new, old *ptypes.T) bool { |
| 286 | + return new != nil && new.SQLString() != old.SQLString() |
| 287 | +} |
| 288 | + |
255 | 289 | // Diff differ between 2 migrations |
256 | 290 | func (t *Table) Diff(old Table) { |
257 | 291 | for i := range t.Columns { |
258 | 292 | if j := old.getIndexColumn(t.Columns[i].Name); t.Columns[i].Action == MigrateAddAction && |
259 | 293 | j >= 0 && old.Columns[j].Action != MigrateNoAction { |
260 | | - if (t.Columns[i].MysqlType != nil && t.Columns[i].MysqlType.String() == old.Columns[j].MysqlType.String()) || |
261 | | - (t.Columns[i].PgType != nil && t.Columns[i].PgType.SQLString() == old.Columns[j].PgType.SQLString()) { |
262 | | - t.Columns[i].Action = MigrateNoAction |
263 | | - } else { |
| 294 | + if hasChangedMysqlOptions(t.Columns[i].Options, old.Columns[j].Options) || |
| 295 | + hasChangedMysqlType(t.Columns[i].MysqlType, old.Columns[j].MysqlType) || |
| 296 | + hasChangePostgresType(t.Columns[i].PgType, old.Columns[j].PgType) { |
264 | 297 | t.Columns[i].Action = MigrateModifyAction |
| 298 | + } else { |
| 299 | + t.Columns[i].Action = MigrateNoAction |
265 | 300 | } |
266 | 301 | } |
267 | 302 | } |
|
0 commit comments