2019-02-13 16:06:34 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
// Package pgutil contains utilities for postgres
|
|
|
|
package pgutil
|
|
|
|
|
|
|
|
import (
|
2020-01-13 13:18:48 +00:00
|
|
|
"context"
|
2019-02-13 16:06:34 +00:00
|
|
|
"database/sql"
|
|
|
|
"encoding/hex"
|
2019-04-02 17:52:25 +01:00
|
|
|
"math/rand"
|
2019-02-13 16:06:34 +00:00
|
|
|
"net/url"
|
|
|
|
"strings"
|
2019-12-04 03:36:21 +00:00
|
|
|
|
|
|
|
"github.com/lib/pq"
|
2019-02-13 16:06:34 +00:00
|
|
|
)
|
|
|
|
|
2019-04-02 17:52:25 +01:00
|
|
|
// CreateRandomTestingSchemaName creates a random schema name string.
|
|
|
|
func CreateRandomTestingSchemaName(n int) string {
|
2019-02-13 16:06:34 +00:00
|
|
|
data := make([]byte, n)
|
2019-04-02 17:52:25 +01:00
|
|
|
|
|
|
|
// math/rand.Read() always returns a nil error so there's no need to handle the error.
|
2019-02-13 16:06:34 +00:00
|
|
|
_, _ = rand.Read(data)
|
|
|
|
return hex.EncodeToString(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConnstrWithSchema adds schema to a connection string
|
|
|
|
func ConnstrWithSchema(connstr, schema string) string {
|
2019-12-04 03:36:21 +00:00
|
|
|
if strings.Contains(connstr, "?") {
|
|
|
|
connstr += "&options="
|
|
|
|
} else {
|
|
|
|
connstr += "?options="
|
|
|
|
}
|
|
|
|
return connstr + url.QueryEscape("--search_path="+pq.QuoteIdentifier(schema))
|
2019-02-13 16:06:34 +00:00
|
|
|
}
|
|
|
|
|
2019-05-14 16:13:18 +01:00
|
|
|
// ParseSchemaFromConnstr returns the name of the schema parsed from the
|
|
|
|
// connection string if one is provided
|
|
|
|
func ParseSchemaFromConnstr(connstr string) (string, error) {
|
|
|
|
url, err := url.Parse(connstr)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
queryValues := url.Query()
|
2019-12-04 03:36:21 +00:00
|
|
|
// this is the Proper™ way to encode search_path in a pq connection string
|
|
|
|
options := queryValues["options"]
|
|
|
|
for _, option := range options {
|
|
|
|
if strings.HasPrefix(option, "--search_path=") {
|
|
|
|
return UnquoteIdentifier(option[len("--search_path="):]), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// this is another way we've used before; supported brokenly as a kludge in github.com/lib/pq
|
2019-05-14 16:13:18 +01:00
|
|
|
schema := queryValues["search_path"]
|
|
|
|
if len(schema) > 0 {
|
2019-12-04 03:36:21 +00:00
|
|
|
return UnquoteIdentifier(schema[0]), nil
|
2019-05-14 16:13:18 +01:00
|
|
|
}
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2019-02-13 16:06:34 +00:00
|
|
|
// QuoteSchema quotes schema name for
|
|
|
|
func QuoteSchema(schema string) string {
|
2019-12-04 03:36:21 +00:00
|
|
|
return pq.QuoteIdentifier(schema)
|
2019-02-13 16:06:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Execer is for executing sql
|
|
|
|
type Execer interface {
|
|
|
|
Exec(query string, args ...interface{}) (sql.Result, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateSchema creates a schema if it doesn't exist.
|
2020-01-13 13:18:48 +00:00
|
|
|
func CreateSchema(ctx context.Context, db Execer, schema string) (err error) {
|
2019-11-06 12:12:20 +00:00
|
|
|
for try := 0; try < 5; try++ {
|
|
|
|
_, err = db.Exec(`create schema if not exists ` + QuoteSchema(schema) + `;`)
|
|
|
|
|
|
|
|
// Postgres `CREATE SCHEMA IF NOT EXISTS` may return "duplicate key value violates unique constraint".
|
|
|
|
// In that case, we will retry rather than doing anything more complicated.
|
|
|
|
//
|
|
|
|
// See more in: https://stackoverflow.com/a/29908840/192220
|
|
|
|
if IsConstraintError(err) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-02-13 16:06:34 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// DropSchema drops the named schema
|
2020-01-13 13:18:48 +00:00
|
|
|
func DropSchema(ctx context.Context, db Execer, schema string) error {
|
2019-02-13 16:06:34 +00:00
|
|
|
_, err := db.Exec(`drop schema ` + QuoteSchema(schema) + ` cascade;`)
|
|
|
|
return err
|
|
|
|
}
|