|
|
|
@ -228,7 +228,7 @@ func (session *Session) Sync2(beans ...interface{}) error { |
|
|
|
|
defer session.Close() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
tables, err := engine.DBMetas() |
|
|
|
|
tables, err := engine.dialect.GetTables() |
|
|
|
|
if err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
@ -239,26 +239,29 @@ func (session *Session) Sync2(beans ...interface{}) error { |
|
|
|
|
session.resetStatement() |
|
|
|
|
}() |
|
|
|
|
|
|
|
|
|
var structTables []*core.Table |
|
|
|
|
|
|
|
|
|
for _, bean := range beans { |
|
|
|
|
v := rValue(bean) |
|
|
|
|
table, err := engine.mapType(v) |
|
|
|
|
if err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
structTables = append(structTables, table) |
|
|
|
|
tbName := engine.TableName(bean) |
|
|
|
|
tbNameWithSchema := engine.TableName(tbName, true) |
|
|
|
|
var tbName string |
|
|
|
|
if len(session.statement.AltTableName) > 0 { |
|
|
|
|
tbName = session.statement.AltTableName |
|
|
|
|
} else { |
|
|
|
|
tbName = engine.TableName(bean) |
|
|
|
|
} |
|
|
|
|
tbNameWithSchema := engine.tbNameWithSchema(tbName) |
|
|
|
|
|
|
|
|
|
var oriTable *core.Table |
|
|
|
|
for _, tb := range tables { |
|
|
|
|
if strings.EqualFold(tb.Name, tbName) { |
|
|
|
|
if strings.EqualFold(engine.tbNameWithSchema(tb.Name), engine.tbNameWithSchema(tbName)) { |
|
|
|
|
oriTable = tb |
|
|
|
|
break |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// this is a new table
|
|
|
|
|
if oriTable == nil { |
|
|
|
|
err = session.StoreEngine(session.statement.StoreEngine).createTable(bean) |
|
|
|
|
if err != nil { |
|
|
|
@ -274,7 +277,15 @@ func (session *Session) Sync2(beans ...interface{}) error { |
|
|
|
|
if err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
continue |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// this will modify an old table
|
|
|
|
|
if err = engine.loadTableInfo(oriTable); err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// check columns
|
|
|
|
|
for _, col := range table.Columns() { |
|
|
|
|
var oriCol *core.Column |
|
|
|
|
for _, col2 := range oriTable.Columns() { |
|
|
|
@ -284,7 +295,17 @@ func (session *Session) Sync2(beans ...interface{}) error { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if oriCol != nil { |
|
|
|
|
// column is not exist on table
|
|
|
|
|
if oriCol == nil { |
|
|
|
|
session.statement.RefTable = table |
|
|
|
|
session.statement.tableName = tbNameWithSchema |
|
|
|
|
if err = session.addColumn(col.Name); err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
continue |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
err = nil |
|
|
|
|
expectedType := engine.dialect.SqlType(col) |
|
|
|
|
curType := engine.dialect.SqlType(oriCol) |
|
|
|
|
if expectedType != curType { |
|
|
|
@ -323,19 +344,21 @@ func (session *Session) Sync2(beans ...interface{}) error { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if col.Default != oriCol.Default { |
|
|
|
|
if (col.SQLType.Name == core.Bool || col.SQLType.Name == core.Boolean) && |
|
|
|
|
((strings.EqualFold(col.Default, "true") && oriCol.Default == "1") || |
|
|
|
|
(strings.EqualFold(col.Default, "false") && oriCol.Default == "0")) { |
|
|
|
|
} else { |
|
|
|
|
engine.logger.Warnf("Table %s Column %s db default is %s, struct default is %s", |
|
|
|
|
tbName, col.Name, oriCol.Default, col.Default) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
if col.Nullable != oriCol.Nullable { |
|
|
|
|
engine.logger.Warnf("Table %s Column %s db nullable is %v, struct nullable is %v", |
|
|
|
|
tbName, col.Name, oriCol.Nullable, col.Nullable) |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
session.statement.RefTable = table |
|
|
|
|
session.statement.tableName = tbNameWithSchema |
|
|
|
|
err = session.addColumn(col.Name) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
@ -394,28 +417,14 @@ func (session *Session) Sync2(beans ...interface{}) error { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for _, table := range tables { |
|
|
|
|
var oriTable *core.Table |
|
|
|
|
for _, structTable := range structTables { |
|
|
|
|
if strings.EqualFold(table.Name, session.tbNameNoSchema(structTable)) { |
|
|
|
|
oriTable = structTable |
|
|
|
|
break |
|
|
|
|
// check all the columns which removed from struct fields but left on database tables.
|
|
|
|
|
for _, colName := range oriTable.ColumnsSeq() { |
|
|
|
|
if table.GetColumn(colName) == nil { |
|
|
|
|
engine.logger.Warnf("Table %s has column %s but struct has not related field", engine.TableName(oriTable.Name, true), colName) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if oriTable == nil { |
|
|
|
|
//engine.LogWarnf("Table %s has no struct to mapping it", table.Name)
|
|
|
|
|
continue |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for _, colName := range table.ColumnsSeq() { |
|
|
|
|
if oriTable.GetColumn(colName) == nil { |
|
|
|
|
engine.logger.Warnf("Table %s has column %s but struct has not related field", engine.TableName(table.Name, true), colName) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return nil |
|
|
|
|
} |
|
|
|
|