From 89a726ce5da26da893dd3c2d8475e1d66677fd9c Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Fri, 9 Feb 2018 22:58:34 +0800 Subject: [PATCH] Move ModifyColumn implemention to Dialect --- dialect.go | 2 ++ dialect_common.go | 5 +++++ dialect_mysql.go | 5 +++++ dialects/mssql/mssql.go | 5 +++++ migration_test.go | 5 +---- scope.go | 2 +- 6 files changed, 19 insertions(+), 5 deletions(-) diff --git a/dialect.go b/dialect.go index 90b1723f..fe8e2f62 100644 --- a/dialect.go +++ b/dialect.go @@ -33,6 +33,8 @@ type Dialect interface { HasTable(tableName string) bool // HasColumn check has column or not HasColumn(tableName string, columnName string) bool + // ModifyColumn modify column's type + ModifyColumn(tableName string, columnName string, typ string) error // LimitAndOffsetSQL return generated SQL with Limit and Offset, as mssql has special case LimitAndOffsetSQL(limit, offset interface{}) string diff --git a/dialect_common.go b/dialect_common.go index 30f035a5..06d0bd07 100644 --- a/dialect_common.go +++ b/dialect_common.go @@ -120,6 +120,11 @@ func (s commonDialect) HasColumn(tableName string, columnName string) bool { return count > 0 } +func (s commonDialect) ModifyColumn(tableName string, columnName string, typ string) error { + _, err := s.db.Exec(fmt.Sprintf("ALTER TABLE %v ALTER COLUMN %v TYPE %v", tableName, columnName, typ)) + return err +} + func (s commonDialect) CurrentDatabase() (name string) { s.db.QueryRow("SELECT DATABASE()").Scan(&name) return diff --git a/dialect_mysql.go b/dialect_mysql.go index f4858e10..b9887a5c 100644 --- a/dialect_mysql.go +++ b/dialect_mysql.go @@ -127,6 +127,11 @@ func (s mysql) RemoveIndex(tableName string, indexName string) error { return err } +func (s mysql) ModifyColumn(tableName string, columnName string, typ string) error { + _, err := s.db.Exec(fmt.Sprintf("ALTER TABLE %v MODIFY COLUMN %v %v", tableName, columnName, typ)) + return err +} + func (s mysql) LimitAndOffsetSQL(limit, offset interface{}) (sql string) { if limit != nil { if parsedLimit, err := strconv.ParseInt(fmt.Sprint(limit), 0, 0); err == nil && parsedLimit >= 0 { diff --git a/dialects/mssql/mssql.go b/dialects/mssql/mssql.go index a4f8e87c..10a779de 100644 --- a/dialects/mssql/mssql.go +++ b/dialects/mssql/mssql.go @@ -140,6 +140,11 @@ func (s mssql) HasColumn(tableName string, columnName string) bool { return count > 0 } +func (s mssql) ModifyColumn(tableName string, columnName string, typ string) error { + _, err := s.db.Exec(fmt.Sprintf("ALTER TABLE %v ALTER COLUMN %v %v", tableName, columnName, typ)) + return err +} + func (s mssql) CurrentDatabase() (name string) { s.db.QueryRow("SELECT DB_NAME() AS [Current Database]").Scan(&name) return diff --git a/migration_test.go b/migration_test.go index 3f3a5c8f..6b4470a6 100644 --- a/migration_test.go +++ b/migration_test.go @@ -435,10 +435,7 @@ func TestMultipleIndexes(t *testing.T) { } func TestModifyColumnType(t *testing.T) { - dialect := os.Getenv("GORM_DIALECT") - if dialect != "postgres" && - dialect != "mysql" && - dialect != "mssql" { + if dialect := os.Getenv("GORM_DIALECT"); dialect != "postgres" && dialect != "mysql" && dialect != "mssql" { t.Skip("Skipping this because only postgres, mysql and mssql support altering a column type") } diff --git a/scope.go b/scope.go index 0ef087bc..a10cb3a2 100644 --- a/scope.go +++ b/scope.go @@ -1139,7 +1139,7 @@ func (scope *Scope) dropTable() *Scope { } func (scope *Scope) modifyColumn(column string, typ string) { - scope.Raw(fmt.Sprintf("ALTER TABLE %v ALTER COLUMN %v TYPE %v", scope.QuotedTableName(), scope.Quote(column), typ)).Exec() + scope.db.AddError(scope.Dialect().ModifyColumn(scope.QuotedTableName(), scope.Quote(column), typ)) } func (scope *Scope) dropColumn(column string) {