(writer http.ResponseWriter, request *http.Request)
| 104 | } |
| 105 | |
| 106 | func (this *InstallController) openDbConnection(writer http.ResponseWriter, request *http.Request) *gorm.DB { |
| 107 | dbType := request.FormValue("dbType") |
| 108 | mysqlPortStr := request.FormValue("mysqlPort") |
| 109 | mysqlHost := request.FormValue("mysqlHost") |
| 110 | mysqlSchema := request.FormValue("mysqlSchema") |
| 111 | mysqlUsername := request.FormValue("mysqlUsername") |
| 112 | mysqlPassword := request.FormValue("mysqlPassword") |
| 113 | mysqlCharset := request.FormValue("mysqlCharset") |
| 114 | |
| 115 | var mysqlPort int |
| 116 | if mysqlPortStr != "" { |
| 117 | tmp, err := strconv.Atoi(mysqlPortStr) |
| 118 | this.PanicError(err) |
| 119 | mysqlPort = tmp |
| 120 | } |
| 121 | |
| 122 | //log config |
| 123 | dbLogger := logger.New( |
| 124 | log.New(os.Stdout, "\r\n", log.LstdFlags), // io writer |
| 125 | logger.Config{ |
| 126 | SlowThreshold: time.Second, // slow SQL 1s |
| 127 | LogLevel: logger.Silent, // log level. open when debug. |
| 128 | IgnoreRecordNotFoundError: true, // ignore ErrRecordNotFound |
| 129 | Colorful: false, // colorful print |
| 130 | }, |
| 131 | ) |
| 132 | //table name strategy |
| 133 | namingStrategy := core.CONFIG.NamingStrategy() |
| 134 | |
| 135 | if dbType == "sqlite" { |
| 136 | |
| 137 | var err error = nil |
| 138 | sqliteFolder := core.CONFIG.SqliteFolder() + "/tank.sqlite" |
| 139 | this.logger.Info("Connect Sqlite %s", sqliteFolder) |
| 140 | db, err := gorm.Open(sqlite.Open(sqliteFolder), &gorm.Config{Logger: dbLogger, NamingStrategy: namingStrategy}) |
| 141 | |
| 142 | if err != nil { |
| 143 | core.LOGGER.Panic("failed to connect sqlite database") |
| 144 | } |
| 145 | |
| 146 | //sqlite lock issue. https://gist.github.com/mrnugget/0eda3b2b53a70fa4a894 |
| 147 | phyDb, err := db.DB() |
| 148 | phyDb.SetMaxOpenConns(1) |
| 149 | |
| 150 | return db |
| 151 | |
| 152 | } else { |
| 153 | mysqlUrl := util.GetMysqlUrl(mysqlPort, mysqlHost, mysqlSchema, mysqlUsername, mysqlPassword, mysqlCharset) |
| 154 | |
| 155 | this.logger.Info("Connect MySQL %s", mysqlUrl) |
| 156 | |
| 157 | var err error = nil |
| 158 | db, err := gorm.Open(mysql.Open(mysqlUrl), &gorm.Config{Logger: dbLogger, NamingStrategy: namingStrategy}) |
| 159 | this.PanicError(err) |
| 160 | |
| 161 | return db |
| 162 | } |
| 163 |
no test coverage detected