Fix bug when modify column on mssql #1849

Merged
lunny merged 1 commits from lunny/fix_mssql_modify_column into master 2021-01-05 02:37:53 +00:00
3 changed files with 30 additions and 1 deletions

View File

@ -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 {

View File

@ -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=?"

View File

@ -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)
}