Skip to content

Commit 25b5ffb

Browse files
btrepplunny
authored andcommitted
Enables mssql support (#383)
* 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 <beautrepp@gmail.com> * Vendors in denisenkom/go-mssqldb Includes golang.org/x/crypto/md4 as this is required by go-msssqldb Signed-off-by: Beau Trepp <beautrepp@gmail.com>
1 parent f2ff0ee commit 25b5ffb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+69268
-14
lines changed

Diff for: models/migrations/migrations.go

+1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ func Migrate(x *xorm.Engine) error {
9393
} else if !has {
9494
// If the version record does not exist we think
9595
// it is a fresh installation and we can skip all migrations.
96+
currentVersion.ID = 0
9697
currentVersion.Version = int64(minDBVersion + len(migrations))
9798

9899
if _, err = x.InsertOne(currentVersion); err != nil {

Diff for: models/models.go

+22
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ import (
2121
// Needed for the Postgresql driver
2222
_ "github.com/lib/pq"
2323

24+
// Needed for the MSSSQL driver
25+
_ "github.com/denisenkom/go-mssqldb"
26+
2427
"code.gitea.io/gitea/models/migrations"
2528
"code.gitea.io/gitea/modules/setting"
2629
)
@@ -97,6 +100,8 @@ func LoadConfigs() {
97100
setting.UsePostgreSQL = true
98101
case "tidb":
99102
setting.UseTiDB = true
103+
case "mssql":
104+
setting.UseMSSQL = true
100105
}
101106
DbCfg.Host = sec.Key("HOST").String()
102107
DbCfg.Name = sec.Key("NAME").String()
@@ -123,6 +128,20 @@ func parsePostgreSQLHostPort(info string) (string, string) {
123128
return host, port
124129
}
125130

131+
func parseMSSQLHostPort(info string) (string, string) {
132+
host, port := "127.0.0.1", "1433"
133+
if strings.Contains(info, ":") {
134+
host = strings.Split(info, ":")[0]
135+
port = strings.Split(info, ":")[1]
136+
} else if strings.Contains(info, ",") {
137+
host = strings.Split(info, ",")[0]
138+
port = strings.TrimSpace(strings.Split(info, ",")[1])
139+
} else if len(info) > 0 {
140+
host = info
141+
}
142+
return host, port
143+
}
144+
126145
func getEngine() (*xorm.Engine, error) {
127146
connStr := ""
128147
var Param = "?"
@@ -147,6 +166,9 @@ func getEngine() (*xorm.Engine, error) {
147166
connStr = fmt.Sprintf("postgres://%s:%s@%s:%s/%s%ssslmode=%s",
148167
url.QueryEscape(DbCfg.User), url.QueryEscape(DbCfg.Passwd), host, port, DbCfg.Name, Param, DbCfg.SSLMode)
149168
}
169+
case "mssql":
170+
host, port := parseMSSQLHostPort(DbCfg.Host)
171+
connStr = fmt.Sprintf("server=%s; port=%s; database=%s; user id=%s; password=%s;", host, port, DbCfg.Name, DbCfg.User, DbCfg.Passwd)
150172
case "sqlite3":
151173
if !EnableSQLite3 {
152174
return nil, errors.New("this binary version does not build support for SQLite3")

Diff for: modules/setting/setting.go

+1
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ var (
100100
// Database settings
101101
UseSQLite3 bool
102102
UseMySQL bool
103+
UseMSSQL bool
103104
UsePostgreSQL bool
104105
UseTiDB bool
105106

Diff for: public/js/index.js

+12-12
Original file line numberDiff line numberDiff line change
@@ -228,22 +228,22 @@ function initInstall() {
228228
return;
229229
}
230230

231-
var mysqlDefault = '127.0.0.1:3306';
232-
var postgresDefault = '127.0.0.1:5432';
231+
var dbDefaults = {
232+
"MySQL": "127.0.0.1:3306",
233+
"PostgreSQL": "127.0.0.1:5432",
234+
"MSSQL": "127.0.0.1:1433"
235+
};
233236

234237
$('#sqlite_settings').hide();
235238
$('#sql_settings').show();
236-
if (dbType === "PostgreSQL") {
237-
$('#pgsql_settings').show();
238-
if ($('#db_host').val() == mysqlDefault) {
239-
$('#db_host').val(postgresDefault);
240-
}
241-
} else {
242-
$('#pgsql_settings').hide();
243-
if ($('#db_host').val() == postgresDefault) {
244-
$('#db_host').val(mysqlDefault);
239+
240+
$('#pgsql_settings').toggle(dbType === "PostgreSQL");
241+
$.each(dbDefaults, function(type, defaultHost) {
242+
if ($('#db_host').val() == defaultHost) {
243+
$('#db_host').val(dbDefaults[dbType]);
244+
return false;
245245
}
246-
}
246+
});
247247
});
248248

249249
// TODO: better handling of exclusive relations.

Diff for: routers/install.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func InstallInit(ctx *context.Context) {
4040
ctx.Data["Title"] = ctx.Tr("install.install")
4141
ctx.Data["PageIsInstall"] = true
4242

43-
dbOpts := []string{"MySQL", "PostgreSQL"}
43+
dbOpts := []string{"MySQL", "PostgreSQL","MSSQL"}
4444
if models.EnableSQLite3 {
4545
dbOpts = append(dbOpts, "SQLite3")
4646
}
@@ -64,6 +64,8 @@ func Install(ctx *context.Context) {
6464
switch models.DbCfg.Type {
6565
case "postgres":
6666
ctx.Data["CurDbOption"] = "PostgreSQL"
67+
case "mssql":
68+
ctx.Data["CurDbOption"] = "MSSQL"
6769
case "sqlite3":
6870
if models.EnableSQLite3 {
6971
ctx.Data["CurDbOption"] = "SQLite3"
@@ -139,7 +141,7 @@ func InstallPost(ctx *context.Context, form auth.InstallForm) {
139141

140142
// Pass basic check, now test configuration.
141143
// Test database setting.
142-
dbTypes := map[string]string{"MySQL": "mysql", "PostgreSQL": "postgres", "SQLite3": "sqlite3", "TiDB": "tidb"}
144+
dbTypes := map[string]string{"MySQL": "mysql", "PostgreSQL": "postgres", "MSSQL": "mssql", "SQLite3": "sqlite3", "TiDB": "tidb"}
143145
models.DbCfg.Type = dbTypes[form.DbType]
144146
models.DbCfg.Host = form.DbHost
145147
models.DbCfg.User = form.DbUser

Diff for: vendor/github.com/denisenkom/go-mssqldb/LICENSE.txt

+27
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: vendor/github.com/denisenkom/go-mssqldb/README.md

+95
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)