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 { } else if !has {
// If the version record does not exist we think // If the version record does not exist we think
// it is a fresh installation and we can skip all migrations. // it is a fresh installation and we can skip all migrations.
currentVersion.ID = 0
currentVersion.Version = int64(_MIN_DB_VER + len(migrations)) currentVersion.Version = int64(_MIN_DB_VER + len(migrations))
if _, err = x.InsertOne(currentVersion); err != nil { if _, err = x.InsertOne(currentVersion); err != nil {

20
models/models.go

@ -13,6 +13,7 @@ import (
"path" "path"
"strings" "strings"
_ "github.com/denisenkom/go-mssqldb"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
"github.com/go-xorm/core" "github.com/go-xorm/core"
"github.com/go-xorm/xorm" "github.com/go-xorm/xorm"
@ -85,6 +86,8 @@ func LoadConfigs() {
setting.UseMySQL = true setting.UseMySQL = true
case "postgres": case "postgres":
setting.UsePostgreSQL = true setting.UsePostgreSQL = true
case "mssql":
setting.UseMSSQL = true
case "tidb": case "tidb":
setting.UseTiDB = true setting.UseTiDB = true
} }
@ -113,6 +116,20 @@ func parsePostgreSQLHostPort(info string) (string, string) {
return host, port 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) { func getEngine() (*xorm.Engine, error) {
connStr := "" connStr := ""
var Param string = "?" var Param string = "?"
@ -137,6 +154,9 @@ func getEngine() (*xorm.Engine, error) {
connStr = fmt.Sprintf("postgres://%s:%s@%s:%s/%s%ssslmode=%s", 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) 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": case "sqlite3":
if !EnableSQLite3 { if !EnableSQLite3 {
return nil, errors.New("This binary version does not build support for SQLite3.") 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 UseSQLite3 bool
UseMySQL bool UseMySQL bool
UsePostgreSQL bool UsePostgreSQL bool
UseMSSQL bool
UseTiDB bool UseTiDB bool
// Webhook settings // Webhook settings

23
public/js/gogs.js

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

6
routers/install.go

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

Loading…
Cancel
Save