pg

package
v0.0.0-...-0490640 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 16, 2019 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DatabaseExistsQuery           = `SELECT EXISTS(SELECT 1 FROM pg_database WHERE datname = $1)`
	ConnectedUserIsSuperuserQuery = `SELECT rolsuper FROM pg_roles WHERE rolname = current_user`
)

Queries that support positional arguments and can thus be used without constructors.

Variables

View Source
var A2SuperuserCerts = TLSCertPaths{
	Cert:     "/hab/svc/automate-postgresql/config/server.crt",
	Key:      "/hab/svc/automate-postgresql/config/server.key",
	RootCert: "/hab/svc/automate-postgresql/config/root.crt",
}
View Source
var CurrentDBProvider = DefaultDBProvider
View Source
var ErrNoExport = errors.New("No export of the requested database exists")
View Source
var PGDumpCmd = []string{"hab", "pkg", "exec", "chef/automate-postgresql", "pg_dump"}

PGDumpCmd is the command we will run for pg_dump. This self-test harness replaces this with a stub.

View Source
var PGRestoreCmd = []string{"hab", "pkg", "exec", "chef/automate-postgresql", "pg_restore"}

PGRestoreCmd is the command we will run for pg_restore.

View Source
var PSQLCmd = []string{"hab", "pkg", "exec", "chef/automate-postgresql", "psql"}

PSQLCmd is the command we will run for psql.

Functions

func AlterDatabaseOwner

func AlterDatabaseOwner(dbname string, owner string) string

func CreateDatabaseQuery

func CreateDatabaseQuery(dbname string) string

func CreateDatabaseWithOwnerQuery

func CreateDatabaseWithOwnerQuery(dbname string, owner string) string

func CreateExtensionQuery

func CreateExtensionQuery(extName string) string

func CreateRoleQuery

func CreateRoleQuery(name string) string

func DropDatabaseQuery

func DropDatabaseQuery(dbname string) string

func GrantAllQuery

func GrantAllQuery(dbName string, roleName string) string

func QuoteIdentifier

func QuoteIdentifier(value string) string

Quote a postgresql identifier. Identifiers are quoted with double quotes in postgresql.

The postgresql function that does this

https://github.com/postgres/postgres/blob/master/src/backend/utils/adt/ruleutils.c#L10550

keeps identifiers unquoted if they only contain safe characters. Here, we just always quote since we use this in places where quoted identifiers are always allowed.

func QuoteLiteral

func QuoteLiteral(value string) string

Taken from: https://github.com/lib/pq/pull/718 which seems close enough to

https://github.com/postgres/postgres/blob/322548a8abe225f2cfd6a48e07b99e2711d28ef7/src/backend/utils/adt/quote.c#L46-L70

to be trustworthy.

QuoteLiteral quotes a string literal to be used as part of an SQL statement. It's useful with SQL statements that don't support parametrization. For example:

quoted := pq.QuoteLiteral("secret") err := db.Exec(fmt.Sprintf("CREATE USER foo PASSWORD %s", quoted))

Any single quotes and backslashes in value will be escaped. If value contains at least one backslash, "E" prefix will be prepended.

func RemoveRolePasswordQuery

func RemoveRolePasswordQuery(name string) string

func RenameDatabaseQuery

func RenameDatabaseQuery(old string, new string) string

Types

type A1ConnInfo

type A1ConnInfo struct {
	User string
	Pass string
	Host string
	Port uint64
	// contains filtered or unexported fields
}

A1ConnInfo represents the connection information for an Automate 1 PostgreSQL database. It assumes connections should be made without TLS and that password authentication can be used.

To avoid leaking passwords via the environment, we use the PGPASSFILE feature of libpq. Users should call InitPgPassfile() before using the options returned by PsqlCmdOptions. Users are responsible for calling CleanupPgPassfile() when they are done.

func (*A1ConnInfo) CleanupPgPassfile

func (c *A1ConnInfo) CleanupPgPassfile() error

func (*A1ConnInfo) ConnURI

func (c *A1ConnInfo) ConnURI(dbname string) string

func (*A1ConnInfo) InitPgPassfile

func (c *A1ConnInfo) InitPgPassfile() (string, error)

InitPgPassfile creates a temporary pgpass file. The format of the file is defined by:

https://www.postgresql.org/docs/9.6/static/libpq-pgpass.html

The filename is returned if the file has been successfully written and synced to disk. Otherwise, an error is returned. The caller is responsible for cleaning up the file after use.

func (*A1ConnInfo) PsqlCmdOptions

func (c *A1ConnInfo) PsqlCmdOptions() []command.Opt

func (*A1ConnInfo) String

func (c *A1ConnInfo) String() string

type A2ConnInfo

type A2ConnInfo struct {
	User  string
	Host  string
	Port  uint64
	Certs TLSCertPaths
}

A2ConnInfo represents the connection information for an Automate 2 PostgreSQL database. It assumes connections can be made via TLS and authentication happens via client certificates.

func (*A2ConnInfo) ConnURI

func (c *A2ConnInfo) ConnURI(dbname string) string

func (*A2ConnInfo) PsqlCmdOptions

func (c *A2ConnInfo) PsqlCmdOptions() []command.Opt

func (*A2ConnInfo) String

func (c *A2ConnInfo) String() string

type ConnInfo

type ConnInfo interface {
	ConnInfoURI
	// PsqlCmdOptions returns an array of command.Opts that set
	// the environment variables required for psql (or other tools
	// that use libpq environment variables) to connect to the
	// given database. See
	//     https://www.postgresql.org/docs/9.6/static/libpq-envars.html
	// for details on the various variables.
	PsqlCmdOptions() []command.Opt
}

ConnInfo provides connection information for PostgreSQL databases

type ConnInfoURI

type ConnInfoURI interface {
	// ConnURI returns a string suitable for passing to the
	// database/sql libraries or other tools that accept libpq
	// connection URIs. See
	//     https://www.postgresql.org/docs/9.6/static/libpq-connect.html#LIBPQ-CONNSTRING
	// for details on the format.
	ConnURI(string) string
}

type DB

type DB interface {
	CreateDatabase(string) error
	CreateDatabaseWithOwner(string, string) error
	AlterDatabaseOwner(string, string) error
	CreateRole(string) error
	RemovePassword(string) error
	GrantAll(string, string) error
	DropDatabase(string) error
	ConnectedUserIsSuperuser() (bool, error)
	RenameDatabase(string, string) error
	DatabaseExists(string) (bool, error)
	CreateExtension(string) error

	Ping() error
	Close() error
	ExecStatement(string, ...interface{}) error
	StringQuery(string, ...interface{}) (string, error)
	BoolQuery(string, ...interface{}) (bool, error)
	BigintQuery(string, ...interface{}) (int64, error)
}

DB is a high-level abstraction over PostgreSQL database interactions. This interface allows us to mock database interactions with Automate 1 in self-test mode.

func Connect

func Connect(info ConnInfoURI, dbname string) (DB, error)

type DBProvider

type DBProvider interface {
	Connect(ConnInfoURI, string) (DB, error)
}

A DBProvider allows you to connect to a DB

var DefaultDBProvider DBProvider = &sqlDBProvider{}

type DatabaseExporter

type DatabaseExporter struct {
	DataDir           string
	Name              string
	User              string
	IncludedTables    []string
	ExcludedTables    []string
	ConnInfo          ConnInfo
	CmdExecutor       command.Executor
	Timeout           time.Duration
	DisableRoleCreate bool
	UseCustomFormat   bool

	Stdout io.Writer
	Stdin  io.Reader
}

DatabaseExporter knows how to export and import a database. See Export and Import for further details.

func (DatabaseExporter) Exists

func (db DatabaseExporter) Exists() (bool, error)

Exists returns true if the database already exists in the PostgreSQL instance identified by the ConnInfo.

func (DatabaseExporter) Export

func (db DatabaseExporter) Export() error

Export uses pg_dump to create a SQL (plain text) export of the database in the given DataDir. If no User is present on the DatabaseExporter the export will be stripped of ownership information. Any tables or views list in ExcludedTables will be excluded from the backup. If IncludedTables has been specified only tables listed in IncludedTables will be exported.

See https://www.postgresql.org/docs/9.6/static/app-pgdump.html for all of the details.

func (DatabaseExporter) Import

func (db DatabaseExporter) Import(exitOnError bool) error

Import imports a SQL (plain text) export of the database from a previously created export in the DataDir. ErrNoExport is returned if no SQL file for the database can be found in the DataDir. If the DatabaseExporter has a User set, the user will be created before the import. The database is dropped before import. The exitOnError parameter controls whether the SQL import will exit on the first error or continue.

type MockDB

type MockDB struct {
	mock.Mock
}

MockDB is an autogenerated mock type for the DB type

func (*MockDB) AlterDatabaseOwner

func (_m *MockDB) AlterDatabaseOwner(_a0 string, _a1 string) error

AlterDatabaseOwner provides a mock function with given fields: _a0, _a1

func (*MockDB) BigintQuery

func (_m *MockDB) BigintQuery(_a0 string, _a1 ...interface{}) (int64, error)

BigintQuery provides a mock function with given fields: _a0, _a1

func (*MockDB) BoolQuery

func (_m *MockDB) BoolQuery(_a0 string, _a1 ...interface{}) (bool, error)

BoolQuery provides a mock function with given fields: _a0, _a1

func (*MockDB) Close

func (_m *MockDB) Close() error

Close provides a mock function with given fields:

func (*MockDB) ConnectedUserIsSuperuser

func (_m *MockDB) ConnectedUserIsSuperuser() (bool, error)

ConnectedUserIsSuperuser provides a mock function with given fields:

func (*MockDB) CreateDatabase

func (_m *MockDB) CreateDatabase(_a0 string) error

CreateDatabase provides a mock function with given fields: _a0

func (*MockDB) CreateDatabaseWithOwner

func (_m *MockDB) CreateDatabaseWithOwner(_a0 string, _a1 string) error

CreateDatabaseWithOwner provides a mock function with given fields: _a0, _a1

func (*MockDB) CreateExtension

func (_m *MockDB) CreateExtension(_a0 string) error

CreateExtension provides a mock function with given fields: _a0

func (*MockDB) CreateRole

func (_m *MockDB) CreateRole(_a0 string) error

CreateRole provides a mock function with given fields: _a0

func (*MockDB) DatabaseExists

func (_m *MockDB) DatabaseExists(_a0 string) (bool, error)

DatabaseExists provides a mock function with given fields: _a0

func (*MockDB) DropDatabase

func (_m *MockDB) DropDatabase(_a0 string) error

DropDatabase provides a mock function with given fields: _a0

func (*MockDB) ExecStatement

func (_m *MockDB) ExecStatement(_a0 string, _a1 ...interface{}) error

ExecStatement provides a mock function with given fields: _a0, _a1

func (*MockDB) GrantAll

func (_m *MockDB) GrantAll(_a0 string, _a1 string) error

GrantAll provides a mock function with given fields: _a0, _a1

func (*MockDB) Ping

func (_m *MockDB) Ping() error

Ping provides a mock function with given fields:

func (*MockDB) RemovePassword

func (_m *MockDB) RemovePassword(_a0 string) error

RemovePassword provides a mock function with given fields: _a0

func (*MockDB) RenameDatabase

func (_m *MockDB) RenameDatabase(_a0 string, _a1 string) error

RenameDatabase provides a mock function with given fields: _a0, _a1

func (*MockDB) StringQuery

func (_m *MockDB) StringQuery(_a0 string, _a1 ...interface{}) (string, error)

StringQuery provides a mock function with given fields: _a0, _a1

type TLSCertPaths

type TLSCertPaths struct {
	Cert     string
	Key      string
	RootCert string
}

A certDir is a directory containing TLS certificates that can be used for TLS postgresql connections.

func TLSCertPathsFromDir

func TLSCertPathsFromDir(dir string) TLSCertPaths

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL