diff --git a/dialects/dialect.go b/dialects/dialect.go index dc96f73a..18b781a1 100644 --- a/dialects/dialect.go +++ b/dialects/dialect.go @@ -163,7 +163,7 @@ func (db *Base) DropIndexSQL(tableName string, index *schemas.Index) string { func (db *Base) ModifyColumnSQL(tableName string, col *schemas.Column) string { s, _ := ColumnString(db.dialect, col, false) - return fmt.Sprintf("alter table %s MODIFY COLUMN %s", tableName, s) + return fmt.Sprintf("ALTER TABLE %s MODIFY COLUMN %s", tableName, s) } func (b *Base) ForUpdateSQL(query string) string { diff --git a/dialects/mssql.go b/dialects/mssql.go index 8e76e538..5340455d 100644 --- a/dialects/mssql.go +++ b/dialects/mssql.go @@ -368,6 +368,11 @@ func (db *mssql) DropTableSQL(tableName string) (string, bool) { "DROP TABLE \"%s\"", tableName, tableName), true } +func (db *mssql) ModifyColumnSQL(tableName string, col *schemas.Column) string { + s, _ := ColumnString(db.dialect, col, false) + return fmt.Sprintf("ALTER TABLE %s ALTER COLUMN %s", tableName, s) +} + func (db *mssql) IndexCheckSQL(tableName, idxName string) (string, []interface{}) { args := []interface{}{idxName} sql := "select name from sysindexes where id=object_id('" + tableName + "') and name=?" diff --git a/integrations/session_schema_test.go b/integrations/session_schema_test.go index 3f8f810b..72779e11 100644 --- a/integrations/session_schema_test.go +++ b/integrations/session_schema_test.go @@ -10,6 +10,7 @@ import ( "time" "github.com/stretchr/testify/assert" + "xorm.io/xorm/schemas" ) func TestStoreEngine(t *testing.T) { @@ -396,3 +397,26 @@ func TestSync2_Default(t *testing.T) { assertSync(t, new(TestSync2Default)) assert.NoError(t, testEngine.Sync2(new(TestSync2Default))) } + +func TestModifyColum(t *testing.T) { + type TestModifyColumn struct { + Id int64 + UserId int64 `xorm:"default(1)"` + IsMember bool `xorm:"default(true)"` + Name string `xorm:"char(10)"` + } + + assert.NoError(t, PrepareEngine()) + assertSync(t, new(TestModifyColumn)) + + alterSQL := testEngine.Dialect().ModifyColumnSQL("test_modify_column", &schemas.Column{ + Name: "name", + SQLType: schemas.SQLType{ + Name: "VARCHAR", + }, + Length: 16, + Nullable: false, + }) + _, err := testEngine.Exec(alterSQL) + assert.NoError(t, err) +}