Skip to content

Commit a6f961f

Browse files
wxiaoguangzeripathlunny
authored
Refactor install page (db type) (#17919)
* Refactor install page (db type) * set correct default DB HOST for different DB TYPE * remove legacy TiDB from documents * unify the usage of DB TYPE, in code we only use "mysql". "MySQL" is only shown to users for friendly name. * Gitea can use TiDB via MySQL protocol Co-authored-by: zeripath <[email protected]> Co-authored-by: Lunny Xiao <[email protected]>
1 parent b30870e commit a6f961f

File tree

14 files changed

+75
-80
lines changed

14 files changed

+75
-80
lines changed

docs/content/doc/advanced/config-cheat-sheet.zh-cn.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,14 @@ menu:
8484

8585
## Database (`database`)
8686

87-
- `DB_TYPE`: 数据库类型,可选 `mysql`, `postgres`, `mssql`, `tidb``sqlite3`
87+
- `DB_TYPE`: 数据库类型,可选 `mysql`, `postgres`, `mssql``sqlite3`
8888
- `HOST`: 数据库服务器地址和端口。
8989
- `NAME`: 数据库名称。
9090
- `USER`: 数据库用户名。
9191
- `PASSWD`: 数据库用户密码。
9292
- `SSL_MODE`: MySQL 或 PostgreSQL数据库是否启用SSL模式。
9393
- `CHARSET`: **utf8mb4**: 仅当数据库为 MySQL 时有效, 可以为 "utf8" 或 "utf8mb4"。注意:如果使用 "utf8mb4",你的 MySQL InnoDB 版本必须在 5.6 以上。
94-
- `PATH`: Tidb 或者 SQLite3 数据文件存放路径。
94+
- `PATH`: SQLite3 数据文件存放路径。
9595
- `LOG_SQL`: **true**: 显示生成的SQL,默认为真。
9696
- `MAX_IDLE_CONNS` **0**: 最大空闲数据库连接
9797
- `CONN_MAX_LIFETIME` **3s**: 数据库连接最大存活时间

docs/content/page/index.en-us.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ Windows, on architectures like amd64, i386, ARM, PowerPC, and others.
7373
- PostgreSQL (>=10)
7474
- SQLite3
7575
- MSSQL (>=2008R2 SP3)
76-
- TiDB (experimental, not recommended)
76+
- TiDB (MySQL protocol)
7777
- Configuration file
7878
- [app.ini](https://github.com/go-gitea/gitea/blob/master/custom/conf/app.example.ini)
7979
- Admin panel

docs/content/page/index.fr-fr.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ Le but de ce projet est de fournir de la manière la plus simple, la plus rapide
6868
- PostgreSQL
6969
- SQLite3
7070
- MSSQL
71-
- [TiDB](https://github.com/pingcap/tidb) (expérimental)
71+
- [TiDB](https://github.com/pingcap/tidb) (MySQL protocol)
7272
- Fichier de configuration
7373
- Voir [ici](https://github.com/go-gitea/gitea/blob/master/custom/conf/app.example.ini)
7474
- Panel d'administration

docs/content/page/index.zh-cn.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Gitea的首要目标是创建一个极易安装,运行非常快速,安装和
3232
- 支持自定义源的 Gravatar 和 Federated Avatar
3333
- 支持邮件服务
3434
- 支持后台管理面板
35-
- 支持 MySQL、PostgreSQL、SQLite3, MSSQL 和 TiDB(实验性支持) 数据库
35+
- 支持 MySQL、PostgreSQL、SQLite3MSSQL 和 TiDB(MySQL) 数据库
3636
- 支持多语言本地化(21 种语言)
3737

3838
## 系统要求

docs/content/page/index.zh-tw.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ Gitea 是從 [Gogs](http://gogs.io) Fork 出來的,請閱讀部落格文章 [G
6969
- PostgreSQL
7070
- SQLite3
7171
- MSSQL
72-
- TiDB(實驗中, 不建議使用
72+
- TiDB(MySQL 協議
7373
- 設定檔
7474
- [app.ini](https://github.com/go-gitea/gitea/blob/master/custom/conf/app.example.ini)
7575
- 管理員面板

models/engine_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ func TestDumpDatabase(t *testing.T) {
2828
}
2929
assert.NoError(t, db.GetEngine(db.DefaultContext).Sync2(new(Version)))
3030

31-
for _, dbName := range setting.SupportedDatabases {
32-
dbType := setting.GetDBTypeByName(dbName)
31+
for _, dbType := range setting.SupportedDatabaseTypes {
3332
assert.NoError(t, db.DumpDatabase(filepath.Join(dir, dbType+".sql"), dbType))
3433
}
3534
}

modules/setting/database.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ import (
1616
)
1717

1818
var (
19-
// SupportedDatabases includes all supported databases type
20-
SupportedDatabases = []string{"MySQL", "PostgreSQL", "MSSQL"}
21-
dbTypes = map[string]string{"MySQL": "mysql", "PostgreSQL": "postgres", "MSSQL": "mssql", "SQLite3": "sqlite3"}
19+
// SupportedDatabaseTypes includes all XORM supported databases type, sqlite3 maybe added by `database_sqlite3.go`
20+
SupportedDatabaseTypes = []string{"mysql", "postgres", "mssql"}
21+
// DatabaseTypeNames contains the friendly names for all database types
22+
DatabaseTypeNames = map[string]string{"mysql": "MySQL", "postgres": "PostgreSQL", "mssql": "MSSQL", "sqlite3": "SQLite3"}
2223

2324
// EnableSQLite3 use SQLite3, set by build flag
2425
EnableSQLite3 bool
@@ -52,11 +53,6 @@ var (
5253
}
5354
)
5455

55-
// GetDBTypeByName returns the database type as it defined on XORM according the given name
56-
func GetDBTypeByName(name string) string {
57-
return dbTypes[name]
58-
}
59-
6056
// InitDBConfig loads the database settings
6157
func InitDBConfig() {
6258
sec := Cfg.Section("database")

modules/setting/database_sqlite.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ import (
1313

1414
func init() {
1515
EnableSQLite3 = true
16-
SupportedDatabases = append(SupportedDatabases, "SQLite3")
16+
SupportedDatabaseTypes = append(SupportedDatabaseTypes, "sqlite3")
1717
}

options/locale/locale_en-US.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ license_desc = Go get <a target="_blank" rel="noopener noreferrer" href="https:/
124124
install = Installation
125125
title = Initial Configuration
126126
docker_helper = If you run Gitea inside Docker, please read the <a target="_blank" rel="noopener noreferrer" href="%s">documentation</a> before changing any settings.
127-
requite_db_desc = Gitea requires MySQL, PostgreSQL, MSSQL or SQLite3.
127+
require_db_desc = Gitea requires MySQL, PostgreSQL, MSSQL, SQLite3 or TiDB (MySQL protocol).
128128
db_title = Database Settings
129129
db_type = Database Type
130130
host = Host

routers/install/install.go

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@ const (
4242
tplPostInstall base.TplName = "post-install"
4343
)
4444

45+
var supportedDbTypeNames []map[string]string // use a slice to keep order
46+
func getDbTypeNames() []map[string]string {
47+
if supportedDbTypeNames == nil {
48+
for _, t := range setting.SupportedDatabaseTypes {
49+
supportedDbTypeNames = append(supportedDbTypeNames, map[string]string{"type": t, "name": setting.DatabaseTypeNames[t]})
50+
}
51+
}
52+
return supportedDbTypeNames
53+
}
54+
4555
// Init prepare for rendering installation page
4656
func Init(next http.Handler) http.Handler {
4757
var rnd = templates.HTMLRenderer()
@@ -63,7 +73,7 @@ func Init(next http.Handler) http.Handler {
6373
Data: map[string]interface{}{
6474
"Title": locale.Tr("install.install"),
6575
"PageIsInstall": true,
66-
"DbOptions": setting.SupportedDatabases,
76+
"DbTypeNames": getDbTypeNames(),
6777
"i18n": locale,
6878
"Language": locale.Language(),
6979
"Lang": locale.Language(),
@@ -100,19 +110,18 @@ func Install(ctx *context.Context) {
100110
form.DbSchema = setting.Database.Schema
101111
form.Charset = setting.Database.Charset
102112

103-
var curDBOption = "MySQL"
104-
switch setting.Database.Type {
105-
case "postgres":
106-
curDBOption = "PostgreSQL"
107-
case "mssql":
108-
curDBOption = "MSSQL"
109-
case "sqlite3":
110-
if setting.EnableSQLite3 {
111-
curDBOption = "SQLite3"
113+
curDBType := setting.Database.Type
114+
var isCurDBTypeSupported bool
115+
for _, dbType := range setting.SupportedDatabaseTypes {
116+
if dbType == curDBType {
117+
isCurDBTypeSupported = true
118+
break
112119
}
113120
}
114-
115-
ctx.Data["CurDbOption"] = curDBOption
121+
if !isCurDBTypeSupported {
122+
curDBType = "mysql"
123+
}
124+
ctx.Data["CurDbType"] = curDBType
116125

117126
// Application general settings
118127
form.AppName = setting.AppName
@@ -237,7 +246,7 @@ func SubmitInstall(ctx *context.Context) {
237246
form.AppURL += "/"
238247
}
239248

240-
ctx.Data["CurDbOption"] = form.DbType
249+
ctx.Data["CurDbType"] = form.DbType
241250

242251
if ctx.HasError() {
243252
if ctx.HasValue("Err_SMTPUser") {
@@ -261,7 +270,7 @@ func SubmitInstall(ctx *context.Context) {
261270
// ---- Basic checks are passed, now test configuration.
262271

263272
// Test database setting.
264-
setting.Database.Type = setting.GetDBTypeByName(form.DbType)
273+
setting.Database.Type = form.DbType
265274
setting.Database.Host = form.DbHost
266275
setting.Database.User = form.DbUser
267276
setting.Database.Passwd = form.DbPasswd

templates/admin/config.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@
119119
<dl class="dl-horizontal admin-dl-horizontal">
120120
<dt>{{.i18n.Tr "admin.config.db_type"}}</dt>
121121
<dd>{{.DbCfg.Type}}</dd>
122-
{{if not (or (eq .DbCfg.Type "sqlite3") (eq .DbCfg.Type "tidb"))}}
122+
{{if not (eq .DbCfg.Type "sqlite3")}}
123123
<dt>{{.i18n.Tr "admin.config.db_host"}}</dt>
124124
<dd>{{if .DbCfg.Host}}{{.DbCfg.Host}}{{else}}-{{end}}</dd>
125125
<dt>{{.i18n.Tr "admin.config.db_name"}}</dt>
@@ -133,7 +133,7 @@
133133
<dt>{{.i18n.Tr "admin.config.db_ssl_mode"}}</dt>
134134
<dd>{{if .DbCfg.SSLMode}}{{.DbCfg.SSLMode}}{{else}}-{{end}}</dd>
135135
{{end}}
136-
{{if or (eq .DbCfg.Type "sqlite3") (eq .DbCfg.Type "tidb")}}
136+
{{if eq .DbCfg.Type "sqlite3"}}
137137
<dt>{{.i18n.Tr "admin.config.db_path"}}</dt>
138138
<dd>{{if .DbCfg.Path}}{{.DbCfg.Path}}{{else}}-{{end}}</dd>
139139
{{end}}

templates/install.tmpl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,22 @@
1313
<form class="ui form" action="{{AppSubUrl}}/" method="post">
1414
<!-- Database Settings -->
1515
<h4 class="ui dividing header">{{.i18n.Tr "install.db_title"}}</h4>
16-
<p>{{.i18n.Tr "install.requite_db_desc"}}</p>
16+
<p>{{.i18n.Tr "install.require_db_desc"}}</p>
1717
<div class="inline required field {{if .Err_DbType}}error{{end}}">
1818
<label>{{.i18n.Tr "install.db_type"}}</label>
1919
<div class="ui selection database type dropdown">
20-
<input type="hidden" id="db_type" name="db_type" value="{{.CurDbOption}}">
21-
<div class="text">{{.CurDbOption}}</div>
20+
<input type="hidden" id="db_type" name="db_type" value="{{.CurDbType}}">
21+
<div class="text">{{.CurDbType}}</div>
2222
{{svg "octicon-triangle-down" 14 "dropdown icon"}}
2323
<div class="menu">
24-
{{range .DbOptions}}
25-
<div class="item" data-value="{{.}}">{{.}}</div>
24+
{{range .DbTypeNames}}
25+
<div class="item" data-value="{{.type}}">{{.name}}</div>
2626
{{end}}
2727
</div>
2828
</div>
2929
</div>
3030

31-
<div id="sql_settings" class="{{if or (eq .CurDbOption "SQLite3")}}hide{{end}}">
31+
<div class="hide" data-db-setting-for="common-host">
3232
<div class="inline required field {{if .Err_DbSetting}}error{{end}}">
3333
<label for="db_host">{{.i18n.Tr "install.host"}}</label>
3434
<input id="db_host" name="db_host" value="{{.db_host}}">
@@ -48,7 +48,7 @@
4848
</div>
4949
</div>
5050

51-
<div id="pgsql_settings" class="{{if not (eq .CurDbOption "PostgreSQL")}}hide{{end}}">
51+
<div class="hide" data-db-setting-for="postgres">
5252
<div class="inline required field">
5353
<label>{{.i18n.Tr "install.ssl_mode"}}</label>
5454
<div class="ui selection database type dropdown">
@@ -69,7 +69,7 @@
6969
</div>
7070
</div>
7171

72-
<div id="mysql_settings" class="{{if not (eq .CurDbOption "MySQL")}}hide{{end}}">
72+
<div class="hide" data-db-setting-for="mysql">
7373
<div class="inline required field">
7474
<label>{{.i18n.Tr "install.charset"}}</label>
7575
<div class="ui selection database type dropdown">
@@ -83,7 +83,7 @@
8383
</div>
8484
</div>
8585

86-
<div id="sqlite_settings" class="{{if not (or (eq .CurDbOption "SQLite3") (eq .CurDbOption "TiDB"))}}hide{{end}}">
86+
<div class="hide" data-db-setting-for="sqlite3">
8787
<div class="inline required field {{if or .Err_DbPath .Err_DbSetting}}error{{end}}">
8888
<label for="db_path">{{.i18n.Tr "install.path"}}</label>
8989
<input id="db_path" name="db_path" value="{{.db_path}}">

web_src/js/features/install.js

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,41 @@
11
export function initInstall() {
2-
if ($('.install').length === 0) {
2+
if ($('.page-content.install').length === 0) {
33
return;
44
}
55

6-
if ($('#db_host').val() === '') {
7-
$('#db_host').val('127.0.0.1:3306');
8-
$('#db_user').val('gitea');
9-
$('#db_name').val('gitea');
10-
}
6+
const defaultDbUser = 'gitea';
7+
const defaultDbName = 'gitea';
8+
9+
const defaultDbHosts = {
10+
mysql: '127.0.0.1:3306',
11+
postgres: '127.0.0.1:5432',
12+
mssql: '127.0.0.1:1433'
13+
};
14+
15+
const $dbHost = $('#db_host');
16+
const $dbUser = $('#db_user');
17+
const $dbName = $('#db_name');
1118

1219
// Database type change detection.
1320
$('#db_type').on('change', function () {
14-
const sqliteDefault = 'data/gitea.db';
15-
const tidbDefault = 'data/gitea_tidb';
16-
1721
const dbType = $(this).val();
18-
if (dbType === 'SQLite3') {
19-
$('#sql_settings').hide();
20-
$('#pgsql_settings').hide();
21-
$('#mysql_settings').hide();
22-
$('#sqlite_settings').show();
22+
$('div[data-db-setting-for]').hide();
23+
$(`div[data-db-setting-for=${dbType}]`).show();
2324

24-
if (dbType === 'SQLite3' && $('#db_path').val() === tidbDefault) {
25-
$('#db_path').val(sqliteDefault);
25+
if (dbType !== 'sqlite3') {
26+
// for most remote database servers
27+
$(`div[data-db-setting-for=common-host]`).show();
28+
const lastDbHost = $dbHost.val();
29+
const isDbHostDefault = !lastDbHost || Object.values(defaultDbHosts).includes(lastDbHost);
30+
if (isDbHostDefault) {
31+
$dbHost.val(defaultDbHosts[dbType] ?? '');
2632
}
27-
return;
28-
}
29-
30-
const dbDefaults = {
31-
MySQL: '127.0.0.1:3306',
32-
PostgreSQL: '127.0.0.1:5432',
33-
MSSQL: '127.0.0.1:1433'
34-
};
35-
36-
$('#sqlite_settings').hide();
37-
$('#sql_settings').show();
38-
39-
$('#pgsql_settings').toggle(dbType === 'PostgreSQL');
40-
$('#mysql_settings').toggle(dbType === 'MySQL');
41-
$.each(dbDefaults, (_type, defaultHost) => {
42-
if ($('#db_host').val() === defaultHost) {
43-
$('#db_host').val(dbDefaults[dbType]);
44-
return false;
33+
if (!$dbUser.val() && !$dbName.val()) {
34+
$dbUser.val(defaultDbUser);
35+
$dbName.val(defaultDbName);
4536
}
46-
});
47-
});
37+
} // else: for SQLite3, the default path is always prepared by backend code (setting)
38+
}).trigger('change');
4839

4940
// TODO: better handling of exclusive relations.
5041
$('#offline-mode input').on('change', function () {

web_src/less/_install.less

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.install {
1+
.page-content.install {
22
padding-top: 45px;
33

44
form {

0 commit comments

Comments
 (0)