mirror of https://github.com/gogs/gogs.git
database: add PostgreSQL custom schema support (#6695)
Co-authored-by: Homura37 <git@gvip.xyz> Co-authored-by: Homura <16538800+Homura37@users.noreply.github.com> Co-authored-by: Joe Chen <jc@unknwon.io>pull/6709/head
parent
14481533b8
commit
a9be4de5a5
|
@ -9,6 +9,7 @@ All notable changes to Gogs are documented in this file.
|
||||||
- An unlisted option is added when create or migrate a repository. Unlisted repositories are public but not being listed for users without direct access in the UI. [#5733](https://github.com/gogs/gogs/issues/5733)
|
- An unlisted option is added when create or migrate a repository. Unlisted repositories are public but not being listed for users without direct access in the UI. [#5733](https://github.com/gogs/gogs/issues/5733)
|
||||||
- New configuration option `[git.timeout] DIFF` for customizing operation timeout of `git diff`. [#6315](https://github.com/gogs/gogs/issues/6315)
|
- New configuration option `[git.timeout] DIFF` for customizing operation timeout of `git diff`. [#6315](https://github.com/gogs/gogs/issues/6315)
|
||||||
- New configuration option `[server] SSH_SERVER_MACS` for setting list of accepted MACs for connections to builtin SSH server. [#6434](https://github.com/gogs/gogs/issues/6434)
|
- New configuration option `[server] SSH_SERVER_MACS` for setting list of accepted MACs for connections to builtin SSH server. [#6434](https://github.com/gogs/gogs/issues/6434)
|
||||||
|
- Support specifying custom schema for PostgreSQL. [#6695](https://github.com/gogs/gogs/pull/6695)
|
||||||
- New languages support: Mongolian. [#6510](https://github.com/gogs/gogs/pull/6510)
|
- New languages support: Mongolian. [#6510](https://github.com/gogs/gogs/pull/6510)
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
|
|
@ -144,6 +144,8 @@ HOST = 127.0.0.1:5432
|
||||||
NAME = gogs
|
NAME = gogs
|
||||||
USER = gogs
|
USER = gogs
|
||||||
PASSWORD =
|
PASSWORD =
|
||||||
|
; For "postgres" only
|
||||||
|
SCHEMA = public
|
||||||
; For "postgres" only, either "disable", "require" or "verify-full".
|
; For "postgres" only, either "disable", "require" or "verify-full".
|
||||||
SSL_MODE = disable
|
SSL_MODE = disable
|
||||||
; For "sqlite3" only, make sure to use absolute path.
|
; For "sqlite3" only, make sure to use absolute path.
|
||||||
|
|
|
@ -58,6 +58,7 @@ host = Host
|
||||||
user = User
|
user = User
|
||||||
password = Password
|
password = Password
|
||||||
db_name = Database Name
|
db_name = Database Name
|
||||||
|
db_schema = Schema
|
||||||
db_helper = Please use INNODB engine with utf8_general_ci charset for MySQL.
|
db_helper = Please use INNODB engine with utf8_general_ci charset for MySQL.
|
||||||
ssl_mode = SSL Mode
|
ssl_mode = SSL Mode
|
||||||
path = Path
|
path = Path
|
||||||
|
@ -1229,6 +1230,8 @@ config.db_config = Database configuration
|
||||||
config.db.type = Type
|
config.db.type = Type
|
||||||
config.db.host = Host
|
config.db.host = Host
|
||||||
config.db.name = Name
|
config.db.name = Name
|
||||||
|
config.db.schema = Schema
|
||||||
|
config.db.schema_helper = (for "postgres" only)
|
||||||
config.db.user = User
|
config.db.user = User
|
||||||
config.db.ssl_mode = SSL mode
|
config.db.ssl_mode = SSL mode
|
||||||
config.db.ssl_mode_helper = (for "postgres" only)
|
config.db.ssl_mode_helper = (for "postgres" only)
|
||||||
|
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
@ -368,6 +368,7 @@ type DatabaseOpts struct {
|
||||||
Type string
|
Type string
|
||||||
Host string
|
Host string
|
||||||
Name string
|
Name string
|
||||||
|
Schema string
|
||||||
User string
|
User string
|
||||||
Password string
|
Password string
|
||||||
SSLMode string `ini:"SSL_MODE"`
|
SSLMode string `ini:"SSL_MODE"`
|
||||||
|
|
|
@ -60,6 +60,7 @@ MAX_FILES=5
|
||||||
TYPE=sqlite
|
TYPE=sqlite
|
||||||
HOST=127.0.0.1:5432
|
HOST=127.0.0.1:5432
|
||||||
NAME=gogs
|
NAME=gogs
|
||||||
|
SCHEMA=public
|
||||||
USER=gogs
|
USER=gogs
|
||||||
PASSWORD=12345678
|
PASSWORD=12345678
|
||||||
SSL_MODE=disable
|
SSL_MODE=disable
|
||||||
|
|
|
@ -6,7 +6,6 @@ package db
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/url"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
@ -74,13 +73,8 @@ func parseDSN(opts conf.DatabaseOpts) (dsn string, err error) {
|
||||||
|
|
||||||
case "postgres":
|
case "postgres":
|
||||||
host, port := parsePostgreSQLHostPort(opts.Host)
|
host, port := parsePostgreSQLHostPort(opts.Host)
|
||||||
if host[0] == '/' { // looks like a unix socket
|
dsn = fmt.Sprintf("user='%s' password='%s' host='%s' port='%s' dbname='%s' sslmode='%s' search_path='%s'",
|
||||||
dsn = fmt.Sprintf("postgres://%s:%s@:%s/%s%ssslmode=%s&host=%s",
|
opts.User, opts.Password, host, port, opts.Name, opts.SSLMode, opts.Schema)
|
||||||
url.QueryEscape(opts.User), url.QueryEscape(opts.Password), port, opts.Name, concate, opts.SSLMode, host)
|
|
||||||
} else {
|
|
||||||
dsn = fmt.Sprintf("postgres://%s:%s@%s:%s/%s%ssslmode=%s",
|
|
||||||
url.QueryEscape(opts.User), url.QueryEscape(opts.Password), host, port, opts.Name, concate, opts.SSLMode)
|
|
||||||
}
|
|
||||||
|
|
||||||
case "mssql":
|
case "mssql":
|
||||||
host, port := parseMSSQLHostPort(opts.Host)
|
host, port := parseMSSQLHostPort(opts.Host)
|
||||||
|
|
|
@ -96,11 +96,12 @@ func Test_parseDSN(t *testing.T) {
|
||||||
Type: "postgres",
|
Type: "postgres",
|
||||||
Host: "/tmp/pg.sock",
|
Host: "/tmp/pg.sock",
|
||||||
Name: "gogs",
|
Name: "gogs",
|
||||||
|
Schema: "test",
|
||||||
User: "gogs@local",
|
User: "gogs@local",
|
||||||
Password: "pa$$word",
|
Password: "pa$$word",
|
||||||
SSLMode: "disable",
|
SSLMode: "disable",
|
||||||
},
|
},
|
||||||
expDSN: "postgres://gogs%40local:pa%24%24word@:5432/gogs?sslmode=disable&host=/tmp/pg.sock",
|
expDSN: "user='gogs@local' password='pa$$word' host='/tmp/pg.sock' port='5432' dbname='gogs' sslmode='disable' search_path='test'",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "postgres: tcp",
|
name: "postgres: tcp",
|
||||||
|
@ -108,11 +109,12 @@ func Test_parseDSN(t *testing.T) {
|
||||||
Type: "postgres",
|
Type: "postgres",
|
||||||
Host: "127.0.0.1",
|
Host: "127.0.0.1",
|
||||||
Name: "gogs",
|
Name: "gogs",
|
||||||
|
Schema: "test",
|
||||||
User: "gogs@local",
|
User: "gogs@local",
|
||||||
Password: "pa$$word",
|
Password: "pa$$word",
|
||||||
SSLMode: "disable",
|
SSLMode: "disable",
|
||||||
},
|
},
|
||||||
expDSN: "postgres://gogs%40local:pa%24%24word@127.0.0.1:5432/gogs?sslmode=disable",
|
expDSN: "user='gogs@local' password='pa$$word' host='127.0.0.1' port='5432' dbname='gogs' sslmode='disable' search_path='test'",
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
|
|
|
@ -7,7 +7,6 @@ package db
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/url"
|
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
@ -90,13 +89,8 @@ func getEngine() (*xorm.Engine, error) {
|
||||||
case "postgres":
|
case "postgres":
|
||||||
conf.UsePostgreSQL = true
|
conf.UsePostgreSQL = true
|
||||||
host, port := parsePostgreSQLHostPort(conf.Database.Host)
|
host, port := parsePostgreSQLHostPort(conf.Database.Host)
|
||||||
if host[0] == '/' { // looks like a unix socket
|
connStr = fmt.Sprintf("user='%s' password='%s' host='%s' port='%s' dbname='%s' sslmode='%s' search_path='%s'",
|
||||||
connStr = fmt.Sprintf("postgres://%s:%s@:%s/%s%ssslmode=%s&host=%s",
|
conf.Database.User, conf.Database.Password, host, port, conf.Database.Name, conf.Database.SSLMode, conf.Database.Schema)
|
||||||
url.QueryEscape(conf.Database.User), url.QueryEscape(conf.Database.Password), port, conf.Database.Name, Param, conf.Database.SSLMode, host)
|
|
||||||
} else {
|
|
||||||
connStr = fmt.Sprintf("postgres://%s:%s@%s:%s/%s%ssslmode=%s",
|
|
||||||
url.QueryEscape(conf.Database.User), url.QueryEscape(conf.Database.Password), host, port, conf.Database.Name, Param, conf.Database.SSLMode)
|
|
||||||
}
|
|
||||||
driver = "pgx"
|
driver = "pgx"
|
||||||
|
|
||||||
case "mssql":
|
case "mssql":
|
||||||
|
@ -123,6 +117,10 @@ func NewTestEngine() error {
|
||||||
return fmt.Errorf("connect to database: %v", err)
|
return fmt.Errorf("connect to database: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if conf.UsePostgreSQL {
|
||||||
|
x.SetSchema(conf.Database.Schema)
|
||||||
|
}
|
||||||
|
|
||||||
x.SetMapper(core.GonicMapper{})
|
x.SetMapper(core.GonicMapper{})
|
||||||
return x.StoreEngine("InnoDB").Sync2(legacyTables...)
|
return x.StoreEngine("InnoDB").Sync2(legacyTables...)
|
||||||
}
|
}
|
||||||
|
@ -134,6 +132,10 @@ func SetEngine() (*gorm.DB, error) {
|
||||||
return nil, fmt.Errorf("connect to database: %v", err)
|
return nil, fmt.Errorf("connect to database: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if conf.UsePostgreSQL {
|
||||||
|
x.SetSchema(conf.Database.Schema)
|
||||||
|
}
|
||||||
|
|
||||||
x.SetMapper(core.GonicMapper{})
|
x.SetMapper(core.GonicMapper{})
|
||||||
|
|
||||||
var logPath string
|
var logPath string
|
||||||
|
|
|
@ -17,6 +17,7 @@ type Install struct {
|
||||||
DbUser string
|
DbUser string
|
||||||
DbPasswd string
|
DbPasswd string
|
||||||
DbName string
|
DbName string
|
||||||
|
DbSchema string
|
||||||
SSLMode string
|
SSLMode string
|
||||||
DbPath string
|
DbPath string
|
||||||
|
|
||||||
|
|
|
@ -132,6 +132,7 @@ func Install(c *context.Context) {
|
||||||
f.DbHost = conf.Database.Host
|
f.DbHost = conf.Database.Host
|
||||||
f.DbUser = conf.Database.User
|
f.DbUser = conf.Database.User
|
||||||
f.DbName = conf.Database.Name
|
f.DbName = conf.Database.Name
|
||||||
|
f.DbSchema = conf.Database.Schema
|
||||||
f.DbPath = conf.Database.Path
|
f.DbPath = conf.Database.Path
|
||||||
|
|
||||||
c.Data["CurDbOption"] = "PostgreSQL"
|
c.Data["CurDbOption"] = "PostgreSQL"
|
||||||
|
@ -216,6 +217,7 @@ func InstallPost(c *context.Context, f form.Install) {
|
||||||
conf.Database.User = f.DbUser
|
conf.Database.User = f.DbUser
|
||||||
conf.Database.Password = f.DbPasswd
|
conf.Database.Password = f.DbPasswd
|
||||||
conf.Database.Name = f.DbName
|
conf.Database.Name = f.DbName
|
||||||
|
conf.Database.Schema = f.DbSchema
|
||||||
conf.Database.SSLMode = f.SSLMode
|
conf.Database.SSLMode = f.SSLMode
|
||||||
conf.Database.Path = f.DbPath
|
conf.Database.Path = f.DbPath
|
||||||
|
|
||||||
|
@ -311,6 +313,7 @@ func InstallPost(c *context.Context, f form.Install) {
|
||||||
cfg.Section("database").Key("TYPE").SetValue(conf.Database.Type)
|
cfg.Section("database").Key("TYPE").SetValue(conf.Database.Type)
|
||||||
cfg.Section("database").Key("HOST").SetValue(conf.Database.Host)
|
cfg.Section("database").Key("HOST").SetValue(conf.Database.Host)
|
||||||
cfg.Section("database").Key("NAME").SetValue(conf.Database.Name)
|
cfg.Section("database").Key("NAME").SetValue(conf.Database.Name)
|
||||||
|
cfg.Section("database").Key("SCHEMA").SetValue(conf.Database.Schema)
|
||||||
cfg.Section("database").Key("USER").SetValue(conf.Database.User)
|
cfg.Section("database").Key("USER").SetValue(conf.Database.User)
|
||||||
cfg.Section("database").Key("PASSWORD").SetValue(conf.Database.Password)
|
cfg.Section("database").Key("PASSWORD").SetValue(conf.Database.Password)
|
||||||
cfg.Section("database").Key("SSL_MODE").SetValue(conf.Database.SSLMode)
|
cfg.Section("database").Key("SSL_MODE").SetValue(conf.Database.SSLMode)
|
||||||
|
|
|
@ -174,6 +174,8 @@
|
||||||
<dd>{{.Database.Host}}</dd>
|
<dd>{{.Database.Host}}</dd>
|
||||||
<dt>{{.i18n.Tr "admin.config.db.name"}}</dt>
|
<dt>{{.i18n.Tr "admin.config.db.name"}}</dt>
|
||||||
<dd>{{.Database.Name}}</dd>
|
<dd>{{.Database.Name}}</dd>
|
||||||
|
<dt>{{.i18n.Tr "admin.config.db.schema"}}</dt>
|
||||||
|
<dd>{{.Database.Schema}} {{.i18n.Tr "admin.config.db.schema_helper"}}</dd>
|
||||||
<dt>{{.i18n.Tr "admin.config.db.user"}}</dt>
|
<dt>{{.i18n.Tr "admin.config.db.user"}}</dt>
|
||||||
<dd>{{.Database.User}}</dd>
|
<dd>{{.Database.User}}</dd>
|
||||||
<dt>{{.i18n.Tr "admin.config.db.ssl_mode"}}</dt>
|
<dt>{{.i18n.Tr "admin.config.db.ssl_mode"}}</dt>
|
||||||
|
|
|
@ -49,6 +49,10 @@
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div id="pgsql_settings" class="{{if not (eq .CurDbOption "PostgreSQL")}}hide{{end}}">
|
<div id="pgsql_settings" class="{{if not (eq .CurDbOption "PostgreSQL")}}hide{{end}}">
|
||||||
|
<div class="inline required field">
|
||||||
|
<label for="db_schema">{{.i18n.Tr "install.db_schema"}}</label>
|
||||||
|
<input id="db_schema" name="db_schema" value="{{.db_schema}}">
|
||||||
|
</div>
|
||||||
<div class="inline required field">
|
<div class="inline required field">
|
||||||
<label>{{.i18n.Tr "install.ssl_mode"}}</label>
|
<label>{{.i18n.Tr "install.ssl_mode"}}</label>
|
||||||
<div class="ui selection database type dropdown">
|
<div class="ui selection database type dropdown">
|
||||||
|
|
Loading…
Reference in New Issue