dump.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Copyright 2017 The Xorm Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package main
  5. import (
  6. "fmt"
  7. "os"
  8. "github.com/go-xorm/core"
  9. "github.com/go-xorm/xorm"
  10. )
  11. var CmdDump = &Command{
  12. UsageLine: "dump driverName datasourceName",
  13. Short: "dump database all table struct's and data to standard output",
  14. Long: `
  15. dump database for sqlite3, mysql, postgres.
  16. driverName Database driver name, now supported four: mysql mymysql sqlite3 postgres
  17. datasourceName Database connection uri, for detail infomation please visit driver's project page
  18. `,
  19. }
  20. func init() {
  21. CmdDump.Run = runDump
  22. CmdDump.Flags = map[string]bool{}
  23. }
  24. func runDump(cmd *Command, args []string) {
  25. if len(args) != 2 {
  26. fmt.Println("params error, please see xorm help dump")
  27. return
  28. }
  29. var err error
  30. engine, err = xorm.NewEngine(args[0], args[1])
  31. if err != nil {
  32. fmt.Println(err)
  33. return
  34. }
  35. engine.ShowSQL(false)
  36. engine.Logger().SetLevel(core.LOG_UNKNOWN)
  37. err = engine.Ping()
  38. if err != nil {
  39. fmt.Println(err)
  40. return
  41. }
  42. err = engine.DumpAll(os.Stdout)
  43. if err != nil {
  44. fmt.Println(err)
  45. return
  46. }
  47. }