pg

package
v0.0.0-...-bde254b Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2024 License: Apache-2.0 Imports: 20 Imported by: 7

Documentation

Overview

Code generated by mockery v1.0.0. DO NOT EDIT.

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.

View Source
const Pg13Client = "core/postgresql13-client"

Variables

View Source
var (
	// DefaultPGDumpCmd is the command we will run for
	// pg_dump.This package-level var is provided so a
	// stub-command can be injected by the tests.
	DefaultPGDumpCmd = []string{"hab", "pkg", "exec", Pg13Client, "pg_dump"}

	// PGRestoreCmd is the command we will run for
	// pg_restore. This package-level var is provided so a
	// stub-command can be injected by the tests.
	DefaultPGRestoreCmd = []string{"hab", "pkg", "exec", Pg13Client, "pg_restore"}

	// PSQLCmd is the command we will run for psql. This
	// package-level var is provided so a stub-command can be
	// injected by the tests.
	DefaultPSQLCmd = []string{"hab", "pkg", "exec", Pg13Client, "psql"}
)
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")

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 IsIncompatibleTOCLine

func IsIncompatibleTOCLine(line string) bool

isIncompatibleTOCLine takes in a line from TOC of a pg backup and decides if it should be excluded from the restore attempt. Some items in a backup cannot be restored in (e.g.) AWS RDS, where the superuser is not all that "super."

To gather the data you will likely need to modify this function, you first need to make a database dump in the custom format. The command for that is of the form `pg_dump DB_URI --format c > database_dump.fc`. To view the TOC of that file, use a command like `pg_restore --list database_dump.fc`

If using the habitat dev studio, you can access the postgresql CLI tools via `hab pkg exec core/postgresql-client COMMAND`

func PGDumpCmd

func PGDumpCmd() []string

func PGRestoreCmd

func PGRestoreCmd() []string

func PSQLCmd

func PSQLCmd() []string

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
	Password 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) InsecureConnURI

func (c *A2ConnInfo) InsecureConnURI(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
	TempDir           string

	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 PlatformConnInfo

type PlatformConnInfo struct {
	// contains filtered or unexported fields
}

NOTE(ssd) 2019-08-19: This is a bit of duplication with code in platform/config to ensure that you can depend on the platform config without pulling in the command class.

func SuperuserConnInfoFromPlatformConfig

func SuperuserConnInfoFromPlatformConfig(c *platform_config.Config) (*PlatformConnInfo, error)

func (*PlatformConnInfo) ConnURI

func (p *PlatformConnInfo) ConnURI(dbname string) string

func (*PlatformConnInfo) PsqlCmdOptions

func (p *PlatformConnInfo) PsqlCmdOptions() []command.Opt

func (*PlatformConnInfo) String

func (p *PlatformConnInfo) String() string

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