Browse Source

Added mssql support. (#3772)

pull/4126/head
dlob 8 years ago committed by 无闻
parent
commit
5179063e71
  1. 1
      models/migrations/migrations.go
  2. 20
      models/models.go
  3. 1
      modules/setting/setting.go
  4. 23
      public/js/gogs.js
  5. 6
      routers/install.go

1
models/migrations/migrations.go

@ -87,6 +87,7 @@ func Migrate(x *xorm.Engine) error {
} else if !has {
// If the version record does not exist we think
// it is a fresh installation and we can skip all migrations.
currentVersion.ID = 0
currentVersion.Version = int64(_MIN_DB_VER + len(migrations))
if _, err = x.InsertOne(currentVersion); err != nil {

20
models/models.go

@ -13,6 +13,7 @@ import (
"path"
"strings"
_ "github.com/denisenkom/go-mssqldb"
_ "github.com/go-sql-driver/mysql"
"github.com/go-xorm/core"
"github.com/go-xorm/xorm"
@ -85,6 +86,8 @@ func LoadConfigs() {
setting.UseMySQL = true
case "postgres":
setting.UsePostgreSQL = true
case "mssql":
setting.UseMSSQL = true
case "tidb":
setting.UseTiDB = true
}
@ -113,6 +116,20 @@ func parsePostgreSQLHostPort(info string) (string, string) {
return host, port
}
func parseMSSQLHostPort(info string) (string, string) {
host, port := "127.0.0.1", "1433"
if strings.Contains(info, ":") {
host = strings.Split(info, ":")[0]
port = strings.Split(info, ":")[1]
} else if strings.Contains(info, ",") {
host = strings.Split(info, ",")[0]
port = strings.TrimSpace(strings.Split(info, ",")[1])
} else if len(info) > 0 {
host = info
}
return host, port
}
func getEngine() (*xorm.Engine, error) {
connStr := ""
var Param string = "?"
@ -137,6 +154,9 @@ func getEngine() (*xorm.Engine, error) {
connStr = fmt.Sprintf("postgres://%s:%s@%s:%s/%s%ssslmode=%s",
url.QueryEscape(DbCfg.User), url.QueryEscape(DbCfg.Passwd), host, port, DbCfg.Name, Param, DbCfg.SSLMode)
}
case "mssql":
host, port := parseMSSQLHostPort(DbCfg.Host)
connStr = fmt.Sprintf("server=%s; port=%s; database=%s; user id=%s; password=%s;", host, port, DbCfg.Name, DbCfg.User, DbCfg.Passwd)
case "sqlite3":
if !EnableSQLite3 {
return nil, errors.New("This binary version does not build support for SQLite3.")

1
modules/setting/setting.go

@ -104,6 +104,7 @@ var (
UseSQLite3 bool
UseMySQL bool
UsePostgreSQL bool
UseMSSQL bool
UseTiDB bool
// Webhook settings

23
public/js/gogs.js

@ -228,22 +228,21 @@ function initInstall() {
return;
}
var mysqlDefault = '127.0.0.1:3306';
var postgresDefault = '127.0.0.1:5432';
var dbDefaults = {
"MySQL": "127.0.0.1:3306",
"PostgreSQL": "127.0.0.1:5432",
"MSSQL": "127.0.0.1, 1433"
};
$('#sqlite_settings').hide();
$('#sql_settings').show();
if (dbType === "PostgreSQL") {
$('#pgsql_settings').show();
if ($('#db_host').val() == mysqlDefault) {
$('#db_host').val(postgresDefault);
}
} else {
$('#pgsql_settings').hide();
if ($('#db_host').val() == postgresDefault) {
$('#db_host').val(mysqlDefault);
$('#pgsql_settings').toggle(dbType === "PostgreSQL");
$.each(dbDefaults, function(type, defaultHost) {
if ($('#db_host').val() == defaultHost) {
$('#db_host').val(dbDefaults[dbType]);
return false;
}
}
});
});
// TODO: better handling of exclusive relations.

6
routers/install.go

@ -102,7 +102,7 @@ func InstallInit(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("install.install")
ctx.Data["PageIsInstall"] = true
dbOpts := []string{"MySQL", "PostgreSQL"}
dbOpts := []string{"MySQL", "PostgreSQL", "MSSQL"}
if models.EnableSQLite3 {
dbOpts = append(dbOpts, "SQLite3")
}
@ -122,6 +122,8 @@ func Install(ctx *context.Context) {
switch models.DbCfg.Type {
case "postgres":
ctx.Data["CurDbOption"] = "PostgreSQL"
case "mssql":
ctx.Data["CurDbOption"] = "MSSQL"
case "sqlite3":
if models.EnableSQLite3 {
ctx.Data["CurDbOption"] = "SQLite3"
@ -191,7 +193,7 @@ func InstallPost(ctx *context.Context, form auth.InstallForm) {
// Pass basic check, now test configuration.
// Test database setting.
dbTypes := map[string]string{"MySQL": "mysql", "PostgreSQL": "postgres", "SQLite3": "sqlite3", "TiDB": "tidb"}
dbTypes := map[string]string{"MySQL": "mysql", "PostgreSQL": "postgres", "MSSQL": "mssql", "SQLite3": "sqlite3", "TiDB": "tidb"}
models.DbCfg.Type = dbTypes[form.DbType]
models.DbCfg.Host = form.DbHost
models.DbCfg.User = form.DbUser

Loading…
Cancel
Save