From c6d05fa553db4fb5b06759811c539ee3c97e25ff Mon Sep 17 00:00:00 2001 From: lijunshi Date: Sun, 28 Apr 2024 16:12:17 +0000 Subject: [PATCH] fix: Sync2 will remove AUTO_INCREMENT unexpectly (#2444) (#2445) fix #2444 Co-authored-by: Lunny Xiao Reviewed-on: https://gitea.com/xorm/xorm/pulls/2445 Reviewed-by: Lunny Xiao Co-authored-by: lijunshi Co-committed-by: lijunshi --- README.md | 2 +- dialects/mysql.go | 3 +++ tests/schema_test.go | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 5e6418df..450d649e 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/dialects/mysql.go b/dialects/mysql.go index d11c728b..424807d4 100644 --- a/dialects/mysql.go +++ b/dialects/mysql.go @@ -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) } diff --git a/tests/schema_test.go b/tests/schema_test.go index 3657950e..9c43d982 100644 --- a/tests/schema_test.go +++ b/tests/schema_test.go @@ -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) + + } + +} \ No newline at end of file