(args []string)
| 82 | } |
| 83 | |
| 84 | func (c *dbinitCmd) RunCommand(args []string) error { |
| 85 | if c.dbName == "" { |
| 86 | return cmdmain.UsageError("--dbname flag required") |
| 87 | } |
| 88 | |
| 89 | if c.dbType != "mysql" && c.dbType != "postgres" && c.dbType != "mongo" { |
| 90 | if c.dbType == "sqlite" { |
| 91 | if !WithSQLite { |
| 92 | return ErrNoSQLite |
| 93 | } |
| 94 | } else { |
| 95 | return cmdmain.UsageError(fmt.Sprintf("--dbtype flag: got %v, want %v", c.dbType, `"mysql" or "postgres" or "sqlite", or "mongo"`)) |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | var rootdb *sql.DB |
| 100 | var err error |
| 101 | switch c.dbType { |
| 102 | case "postgres": |
| 103 | conninfo := fmt.Sprintf("user=%s dbname=%s host=%s password=%s sslmode=%s", c.user, "postgres", c.host, c.password, c.sslMode) |
| 104 | rootdb, err = sql.Open("postgres", conninfo) |
| 105 | case "mysql": |
| 106 | // need to use an empty dbname to query tables |
| 107 | rootdb, err = sql.Open("mysql", c.mysqlDSN("")) |
| 108 | case "sqlite": |
| 109 | rootdb, err = sql.Open("sqlite", c.dbName) |
| 110 | } |
| 111 | if err != nil { |
| 112 | exitf("Error connecting to the root %s database: %v", c.dbType, err) |
| 113 | } |
| 114 | defer rootdb.Close() |
| 115 | |
| 116 | // Validate the DSN to avoid confusion here |
| 117 | err = rootdb.Ping() |
| 118 | if err != nil { |
| 119 | exitf("Error connecting to the root %s database: %v", c.dbType, err) |
| 120 | } |
| 121 | |
| 122 | dbname := c.dbName |
| 123 | exists := c.dbExists(rootdb) |
| 124 | if exists { |
| 125 | if c.keep { |
| 126 | return nil |
| 127 | } |
| 128 | if !c.wipe { |
| 129 | return cmdmain.UsageError(fmt.Sprintf("Database %q already exists, but --wipe not given. Stopping.", dbname)) |
| 130 | } |
| 131 | if c.dbType == "mongo" { |
| 132 | return c.wipeMongo() |
| 133 | } |
| 134 | if c.dbType != "sqlite" { |
| 135 | do(rootdb, "DROP DATABASE "+dbname) |
| 136 | } |
| 137 | } |
| 138 | switch c.dbType { |
| 139 | case "sqlite": |
| 140 | _, err := os.Create(dbname) |
| 141 | if err != nil { |
nothing calls this directly
no test coverage detected