From 470807151d0a443bcdd49a13d3ec108e1eb65bd2 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Wed, 5 Jan 2022 09:37:18 +0800 Subject: [PATCH] fix cross db dumping of bools (#2089) When dumping booleans these need to be converted from the original DB representation to the new db representation. In the case of most DBs this is simply to 0 or 1 but for postgres these have to be false or true. Signed-off-by: Andrew Thornton Reviewed-on: https://gitea.com/xorm/xorm/pulls/2089 Reviewed-by: Lunny Xiao Co-authored-by: Andrew Thornton Co-committed-by: Andrew Thornton --- engine.go | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/engine.go b/engine.go index 709cc384..1257de20 100644 --- a/engine.go +++ b/engine.go @@ -12,6 +12,7 @@ import ( "os" "reflect" "runtime" + "strconv" "strings" "time" @@ -438,16 +439,14 @@ func (engine *Engine) DumpTables(tables []*schemas.Table, w io.Writer, tp ...sch return engine.dumpTables(context.Background(), tables, w, tp...) } -func formatBool(s string, dstDialect dialects.Dialect) string { - if dstDialect.URI().DBType == schemas.MSSQL { - switch s { - case "true": +func formatBool(s bool, dstDialect dialects.Dialect) string { + if dstDialect.URI().DBType != schemas.POSTGRES { + if s { return "1" - case "false": - return "0" } + return "0" } - return s + return strconv.FormatBool(s) } // dumpTables dump database all table structs and data to w with specify db type @@ -581,8 +580,13 @@ func (engine *Engine) dumpTables(ctx context.Context, tables []*schemas.Table, w return err } } else { - if stp.IsBool() || (dstDialect.URI().DBType == schemas.MSSQL && strings.EqualFold(stp.Name, schemas.Bit)) { - if _, err = io.WriteString(w, formatBool(s.String, dstDialect)); err != nil { + if table.Columns()[i].SQLType.IsBool() || stp.IsBool() || (dstDialect.URI().DBType == schemas.MSSQL && strings.EqualFold(stp.Name, schemas.Bit)) { + val, err := strconv.ParseBool(s.String) + if err != nil { + return err + } + + if _, err = io.WriteString(w, formatBool(val, dstDialect)); err != nil { return err } } else if stp.IsNumeric() {