Skip to content

Commit

Permalink
Enables mssql support (#383)
Browse files Browse the repository at this point in the history
* Enables mssql support

Port of dlobs work in gogs.
Enables options in index.js
Enables MSSQL as a database option in go.
Sets ID to 0 on initial migration. Required for
MSSQL insert statements.

Signed-off-by: Beau Trepp <[email protected]>

* Vendors in denisenkom/go-mssqldb

Includes golang.org/x/crypto/md4
as this is required by go-msssqldb

Signed-off-by: Beau Trepp <[email protected]>
  • Loading branch information
btrepp authored and lunny committed Dec 24, 2016
1 parent f2ff0ee commit 25b5ffb
Show file tree
Hide file tree
Showing 44 changed files with 69,268 additions and 14 deletions.
1 change: 1 addition & 0 deletions models/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,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(minDBVersion + len(migrations))

if _, err = x.InsertOne(currentVersion); err != nil {
Expand Down
22 changes: 22 additions & 0 deletions models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import (
// Needed for the Postgresql driver
_ "github.com/lib/pq"

// Needed for the MSSSQL driver
_ "github.com/denisenkom/go-mssqldb"

"code.gitea.io/gitea/models/migrations"
"code.gitea.io/gitea/modules/setting"
)
Expand Down Expand Up @@ -97,6 +100,8 @@ func LoadConfigs() {
setting.UsePostgreSQL = true
case "tidb":
setting.UseTiDB = true
case "mssql":
setting.UseMSSQL = true
}
DbCfg.Host = sec.Key("HOST").String()
DbCfg.Name = sec.Key("NAME").String()
Expand All @@ -123,6 +128,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 = "?"
Expand All @@ -147,6 +166,9 @@ func getEngine() (*xorm.Engine, error) {
connStr = fmt.Sprintf("postgres:https://%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")
Expand Down
1 change: 1 addition & 0 deletions modules/setting/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ var (
// Database settings
UseSQLite3 bool
UseMySQL bool
UseMSSQL bool
UsePostgreSQL bool
UseTiDB bool

Expand Down
24 changes: 12 additions & 12 deletions public/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,22 +228,22 @@ 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.
Expand Down
6 changes: 4 additions & 2 deletions routers/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,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")
}
Expand All @@ -64,6 +64,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"
Expand Down Expand Up @@ -139,7 +141,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
Expand Down
27 changes: 27 additions & 0 deletions vendor/github.com/denisenkom/go-mssqldb/LICENSE.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

95 changes: 95 additions & 0 deletions vendor/github.com/denisenkom/go-mssqldb/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 25b5ffb

Please sign in to comment.