fix: Sync2 will remove AUTO_INCREMENT unexpectly (#2444) (#2445)

fix #2444

Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Reviewed-on: https://gitea.com/xorm/xorm/pulls/2445
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
Co-authored-by: lijunshi <lijunshi2015@163.com>
Co-committed-by: lijunshi <lijunshi2015@163.com>
This commit is contained in:
lijunshi 2024-04-28 16:12:17 +00:00 committed by Lunny Xiao
parent 3bc2ea24f6
commit c6d05fa553
3 changed files with 44 additions and 1 deletions

View File

@ -465,7 +465,7 @@ res, err := engine.Transaction(func(session *xorm.Session) (interface{}, error)
## Contributing
If you want to pull request, please see [CONTRIBUTING](https://gitea.com/xorm/xorm/src/branch/master/CONTRIBUTING.md). And you can also go to [Xorm on discourse](https://xorm.discourse.group) to discuss.
If you want to pull request, please see [CONTRIBUTING](CONTRIBUTING.md). And you can also go to [Xorm on discourse](https://xorm.discourse.group) to discuss.
## Credits

View File

@ -402,6 +402,9 @@ func (db *mysql) AddColumnSQL(tableName string, col *schemas.Column) string {
// ModifyColumnSQL returns a SQL to modify SQL
func (db *mysql) ModifyColumnSQL(tableName string, col *schemas.Column) string {
s, _ := ColumnString(db.dialect, col, false, true)
if col.IsAutoIncrement {
s += " " + db.AutoIncrStr()
}
if col.Comment != "" {
s += fmt.Sprintf(" COMMENT '%s'", col.Comment)
}

View File

@ -751,3 +751,43 @@ func getKeysFromMap(m map[string]*schemas.Index) []string {
}
return ss
}
type SyncTestUser struct {
Id int64 `xorm:"pk autoincr 'id' comment('primary key 1')"`
Name string `xorm:"'name' notnull comment('nickname')" json:"name"`
}
func (m *SyncTestUser) TableName() string {
return "sync_test_user"
}
type SyncTestUser2 struct {
Id int64 `xorm:"pk autoincr 'id' comment('primary key 2')"`
Name string `xorm:"'name' notnull comment('nickname')" json:"name"`
}
func (m *SyncTestUser2) TableName() string {
return "sync_test_user"
}
func TestSync2_3(t *testing.T) {
if testEngine.Dialect().URI().DBType == schemas.MYSQL {
assert.NoError(t, PrepareEngine())
assertSync(t, new(SyncTestUser))
assert.NoError(t, testEngine.Sync2(new(SyncTestUser2)))
tables, err := testEngine.DBMetas()
assert.NoError(t, err)
tableInfo, err := testEngine.TableInfo(new(SyncTestUser2))
assert.EqualValues(t, tables[0].GetColumn("id").IsAutoIncrement, tableInfo.GetColumn("id").IsAutoIncrement)
assert.EqualValues(t, tables[0].GetColumn("id").Name, tableInfo.GetColumn("id").Name)
assert.EqualValues(t, tables[0].GetColumn("id").SQLType.Name, tableInfo.GetColumn("id").SQLType.Name)
assert.EqualValues(t, tables[0].GetColumn("id").Nullable, tableInfo.GetColumn("id").Nullable)
assert.EqualValues(t, tables[0].GetColumn("id").Comment, tableInfo.GetColumn("id").Comment)
}
}