mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-06-23 15:30:34 -04:00
Add schema handling via env var
This commit is contained in:
parent
b49eb3bffa
commit
e7c9eca524
@ -26,4 +26,7 @@ POSTGRES_PASSWORD=
|
|||||||
POSTGRES_DB=
|
POSTGRES_DB=
|
||||||
POSTGRES_SERVER=
|
POSTGRES_SERVER=
|
||||||
POSTGRES_PORT=5432
|
POSTGRES_PORT=5432
|
||||||
# (the schema "gocoder" will be used)
|
# Default is gocoder, you can specify "disabled" to use the default search_path of the user.
|
||||||
|
# If this is not "disabled", the schema will be created (if it does not exists) and
|
||||||
|
# the search_path of the user will be ignored (only the schema specified will be used).
|
||||||
|
POSTGRES_SCHEMA=gocoder
|
||||||
|
@ -24,21 +24,28 @@ type MetadataService struct {
|
|||||||
|
|
||||||
func NewMetadataService() (*MetadataService, error) {
|
func NewMetadataService() (*MetadataService, error) {
|
||||||
con := fmt.Sprintf(
|
con := fmt.Sprintf(
|
||||||
"postgresql://%v:%v@%v:%v/%v?application_name=gocoder&search_path=gocoder&sslmode=disable",
|
"postgresql://%v:%v@%v:%v/%v?application_name=gocoder&sslmode=disable",
|
||||||
url.QueryEscape(os.Getenv("POSTGRES_USER")),
|
url.QueryEscape(os.Getenv("POSTGRES_USER")),
|
||||||
url.QueryEscape(os.Getenv("POSTGRES_PASSWORD")),
|
url.QueryEscape(os.Getenv("POSTGRES_PASSWORD")),
|
||||||
url.QueryEscape(os.Getenv("POSTGRES_SERVER")),
|
url.QueryEscape(os.Getenv("POSTGRES_SERVER")),
|
||||||
url.QueryEscape(os.Getenv("POSTGRES_PORT")),
|
url.QueryEscape(os.Getenv("POSTGRES_PORT")),
|
||||||
url.QueryEscape(os.Getenv("POSTGRES_DB")),
|
url.QueryEscape(os.Getenv("POSTGRES_DB")),
|
||||||
)
|
)
|
||||||
|
schema := GetEnvOr("POSTGRES_SCHEMA", "gocoder")
|
||||||
|
if schema != "disabled" {
|
||||||
|
con = fmt.Sprintf("%s&search_path=%s", con, url.QueryEscape(schema))
|
||||||
|
}
|
||||||
db, err := sql.Open("postgres", con)
|
db, err := sql.Open("postgres", con)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
fmt.Printf("Could not connect to database, check your env variables!")
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = db.Exec("create schema if not exists gocoder")
|
if schema != "disabled" {
|
||||||
if err != nil {
|
_, err = db.Exec(fmt.Sprintf("create schema if not exists %s", schema))
|
||||||
return nil, err
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
driver, err := postgres.WithInstance(db, &postgres.Config{})
|
driver, err := postgres.WithInstance(db, &postgres.Config{})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user