Commit 12f3c8f9 by Marcus Efraimsson

fix only add column if not exists for mysql

parent a451c201
...@@ -36,3 +36,13 @@ type IfIndexNotExistsCondition struct { ...@@ -36,3 +36,13 @@ type IfIndexNotExistsCondition struct {
func (c *IfIndexNotExistsCondition) Sql(dialect Dialect) (string, []interface{}) { func (c *IfIndexNotExistsCondition) Sql(dialect Dialect) (string, []interface{}) {
return dialect.IndexCheckSql(c.TableName, c.IndexName) return dialect.IndexCheckSql(c.TableName, c.IndexName)
} }
type IfColumnNotExistsCondition struct {
NotExistsMigrationCondition
TableName string
ColumnName string
}
func (c *IfColumnNotExistsCondition) Sql(dialect Dialect) (string, []interface{}) {
return dialect.ColumnCheckSql(c.TableName, c.ColumnName)
}
...@@ -33,6 +33,7 @@ type Dialect interface { ...@@ -33,6 +33,7 @@ type Dialect interface {
UpdateTableSql(tableName string, columns []*Column) string UpdateTableSql(tableName string, columns []*Column) string
IndexCheckSql(tableName, indexName string) (string, []interface{}) IndexCheckSql(tableName, indexName string) (string, []interface{})
ColumnCheckSql(tableName, columnName string) (string, []interface{})
ColString(*Column) string ColString(*Column) string
ColStringNoPk(*Column) string ColStringNoPk(*Column) string
...@@ -183,6 +184,10 @@ func (db *BaseDialect) RenameTable(oldName string, newName string) string { ...@@ -183,6 +184,10 @@ func (db *BaseDialect) RenameTable(oldName string, newName string) string {
return fmt.Sprintf("ALTER TABLE %s RENAME TO %s", quote(oldName), quote(newName)) return fmt.Sprintf("ALTER TABLE %s RENAME TO %s", quote(oldName), quote(newName))
} }
func (db *BaseDialect) ColumnCheckSql(tableName, columnName string) (string, []interface{}) {
return "", nil
}
func (db *BaseDialect) DropIndexSql(tableName string, index *Index) string { func (db *BaseDialect) DropIndexSql(tableName string, index *Index) string {
quote := db.dialect.Quote quote := db.dialect.Quote
name := index.XName(tableName) name := index.XName(tableName)
......
...@@ -85,7 +85,9 @@ type AddColumnMigration struct { ...@@ -85,7 +85,9 @@ type AddColumnMigration struct {
} }
func NewAddColumnMigration(table Table, col *Column) *AddColumnMigration { func NewAddColumnMigration(table Table, col *Column) *AddColumnMigration {
return &AddColumnMigration{tableName: table.Name, column: col} m := &AddColumnMigration{tableName: table.Name, column: col}
m.Condition = &IfColumnNotExistsCondition{TableName: table.Name, ColumnName: col.Name}
return m
} }
func (m *AddColumnMigration) Table(tableName string) *AddColumnMigration { func (m *AddColumnMigration) Table(tableName string) *AddColumnMigration {
......
...@@ -121,6 +121,8 @@ func (mg *Migrator) exec(m Migration, sess *xorm.Session) error { ...@@ -121,6 +121,8 @@ func (mg *Migrator) exec(m Migration, sess *xorm.Session) error {
condition := m.GetCondition() condition := m.GetCondition()
if condition != nil { if condition != nil {
sql, args := condition.Sql(mg.Dialect) sql, args := condition.Sql(mg.Dialect)
if sql != "" {
mg.Logger.Debug("Executing migration condition sql", "id", m.Id(), "sql", sql, "args", args) mg.Logger.Debug("Executing migration condition sql", "id", m.Id(), "sql", sql, "args", args)
results, err := sess.SQL(sql, args...).Query() results, err := sess.SQL(sql, args...).Query()
if err != nil { if err != nil {
...@@ -133,6 +135,7 @@ func (mg *Migrator) exec(m Migration, sess *xorm.Session) error { ...@@ -133,6 +135,7 @@ func (mg *Migrator) exec(m Migration, sess *xorm.Session) error {
return nil return nil
} }
} }
}
var err error var err error
if codeMigration, ok := m.(CodeMigration); ok { if codeMigration, ok := m.(CodeMigration); ok {
......
...@@ -108,6 +108,12 @@ func (db *Mysql) IndexCheckSql(tableName, indexName string) (string, []interface ...@@ -108,6 +108,12 @@ func (db *Mysql) IndexCheckSql(tableName, indexName string) (string, []interface
return sql, args return sql, args
} }
func (db *Mysql) ColumnCheckSql(tableName, columnName string) (string, []interface{}) {
args := []interface{}{tableName, columnName}
sql := "SELECT 1 FROM " + db.Quote("INFORMATION_SCHEMA") + "." + db.Quote("COLUMNS") + " WHERE " + db.Quote("TABLE_SCHEMA") + " = DATABASE() AND " + db.Quote("TABLE_NAME") + "=? AND " + db.Quote("COLUMN_NAME") + "=?"
return sql, args
}
func (db *Mysql) CleanDB() error { func (db *Mysql) CleanDB() error {
tables, _ := db.engine.DBMetas() tables, _ := db.engine.DBMetas()
sess := db.engine.NewSession() sess := db.engine.NewSession()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment