From 5179063e71d7234250209389e6f69db1cd6f0caa Mon Sep 17 00:00:00 2001 From: dlob Date: Tue, 14 Feb 2017 02:50:00 +0100 Subject: [PATCH] Added mssql support. (#3772) --- models/migrations/migrations.go | 1 + models/models.go | 20 ++++++++++++++++++++ modules/setting/setting.go | 1 + public/js/gogs.js | 23 +++++++++++------------ routers/install.go | 6 ++++-- 5 files changed, 37 insertions(+), 14 deletions(-) diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go index 71a14960c..cf3785c6d 100644 --- a/models/migrations/migrations.go +++ b/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 { diff --git a/models/models.go b/models/models.go index 4a7385f15..7ebeac9b9 100644 --- a/models/models.go +++ b/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.") diff --git a/modules/setting/setting.go b/modules/setting/setting.go index a1c7b4272..5b8ab2116 100644 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -104,6 +104,7 @@ var ( UseSQLite3 bool UseMySQL bool UsePostgreSQL bool + UseMSSQL bool UseTiDB bool // Webhook settings diff --git a/public/js/gogs.js b/public/js/gogs.js index 6aadea59b..45b11fe7c 100644 --- a/public/js/gogs.js +++ b/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. diff --git a/routers/install.go b/routers/install.go index f46508323..dbdb2d6c0 100644 --- a/routers/install.go +++ b/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