Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enables mssql support #383

Merged
merged 2 commits into from
Dec 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -101,6 +101,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 @@ -138,7 +140,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