postgres

package
v0.0.0-...-1a420d6 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2025 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CreateDBWithoutOwnerSQLTemplate = `CREATE DATABASE "%s"`
	AlterDBOwnerSQLTemplate         = `ALTER DATABASE "%s" OWNER TO "%s"`
)
View Source
const (
	CascadeKeyword                 = "CASCADE"
	RestrictKeyword                = "RESTRICT"
	CreateDBSQLTemplate            = `CREATE DATABASE "%s" WITH OWNER = "%s"`
	ChangeDBOwnerSQLTemplate       = `ALTER DATABASE "%s" OWNER TO "%s"`
	IsDatabaseExistSQLTemplate     = `SELECT 1 FROM pg_database WHERE datname='%s'`
	RenameDatabaseSQLTemplate      = `ALTER DATABASE "%s" RENAME TO "%s"`
	CreateSchemaSQLTemplate        = `CREATE SCHEMA IF NOT EXISTS "%s" AUTHORIZATION "%s"`
	CreateExtensionSQLTemplate     = `CREATE EXTENSION IF NOT EXISTS "%s"`
	DropDatabaseSQLTemplate        = `DROP DATABASE "%s"`
	DropExtensionSQLTemplate       = `DROP EXTENSION IF EXISTS "%s" %s`
	DropSchemaSQLTemplate          = `DROP SCHEMA IF EXISTS "%s" %s`
	GrantUsageSchemaSQLTemplate    = `GRANT USAGE ON SCHEMA "%s" TO "%s"`
	GrantAllTablesSQLTemplate      = `GRANT %s ON ALL TABLES IN SCHEMA "%s" TO "%s"`
	DefaultPrivsSchemaSQLTemplate  = `ALTER DEFAULT PRIVILEGES FOR ROLE "%s" IN SCHEMA "%s" GRANT %s ON TABLES TO "%s"`
	GetTablesFromSchemaSQLTemplate = `SELECT tablename,tableowner FROM pg_tables WHERE schemaname = '%s'`
	ChangeTableOwnerSQLTemplate    = `ALTER TABLE IF EXISTS "%s" OWNER TO "%s"`
	ChangeTypeOwnerSQLTemplate     = `ALTER TYPE "%s"."%s" OWNER TO "%s"`
	// Got and edited from : https://stackoverflow.com/questions/3660787/how-to-list-custom-types-using-postgres-information-schema
	GetTypesFromSchemaSQLTemplate = `` /* 404-byte string literal not displayed */

	DuplicateDatabaseErrorCode = "42P04"
)
View Source
const (
	CreatePublicationSQLTemplate                = `CREATE PUBLICATION "%s" %s %s`
	DropPublicationSQLTemplate                  = `DROP PUBLICATION "%s"`
	AlterPublicationRenameSQLTemplate           = `ALTER PUBLICATION "%s" RENAME TO "%s"`
	AlterPublicationGeneralOperationSQLTemplate = `ALTER PUBLICATION "%s" SET %s`
	GetPublicationSQLTemplate                   = `` /* 132-byte string literal not displayed */

	GetReplicationSlotSQLTemplate    = `SELECT slot_name,plugin,database FROM pg_replication_slots WHERE slot_name = '%s'`
	CreateReplicationSlotSQLTemplate = `SELECT pg_create_logical_replication_slot('%s', '%s')`
	DropReplicationSlotSQLTemplate   = `SELECT pg_drop_replication_slot('%s')`
)
View Source
const (
	CreateGroupRoleSQLTemplate             = `CREATE ROLE "%s"`
	CreateUserRoleSQLTemplate              = `CREATE ROLE "%s" WITH LOGIN PASSWORD '%s' %s`
	GrantRoleSQLTemplate                   = `GRANT "%s" TO "%s"`
	GrantRoleWithAdminOptionSQLTemplate    = `GRANT "%s" TO "%s" WITH ADMIN OPTION`
	AlterUserSetRoleSQLTemplate            = `ALTER USER "%s" SET ROLE "%s"`
	AlterUserSetRoleOnDatabaseSQLTemplate  = `ALTER ROLE "%s" IN DATABASE "%s" SET ROLE "%s"`
	RevokeUserSetRoleOnDatabaseSQLTemplate = `ALTER ROLE "%s" IN DATABASE "%s" RESET role`
	RevokeRoleSQLTemplate                  = `REVOKE "%s" FROM "%s"`
	UpdatePasswordSQLTemplate              = `ALTER ROLE "%s" WITH PASSWORD '%s'` // #nosec
	DropRoleSQLTemplate                    = `DROP ROLE "%s"`
	DropOwnedBySQLTemplate                 = `DROP OWNED BY "%s"`
	ReassignObjectsSQLTemplate             = `REASSIGN OWNED BY "%s" TO "%s"`
	IsRoleExistSQLTemplate                 = `SELECT 1 FROM pg_roles WHERE rolname='%s'`
	RenameRoleSQLTemplate                  = `ALTER ROLE "%s" RENAME TO "%s"`
	AlterRoleWithOptionSQLTemplate         = `ALTER ROLE "%s" WITH %s`
	// Source: https://dba.stackexchange.com/questions/136858/postgresql-display-role-members
	GetRoleMembershipSQLTemplate = `` /* 187-byte string literal not displayed */
	GetRoleAttributesSQLTemplate = `select rolconnlimit, rolreplication, rolbypassrls FROM pg_roles WHERE rolname = '%s'`
	// DO NOT TOUCH THIS
	// Cannot filter on compute value so... cf line before.
	GetRoleSettingsSQLTemplate = `` //nolint:lll//Because
	/* 370-byte string literal not displayed */
	DoesRoleHaveActiveSessionSQLTemplate = `SELECT 1 from pg_stat_activity WHERE usename = '%s' group by usename`
	DuplicateRoleErrorCode               = "42710"
	RoleNotFoundErrorCode                = "42704"
	InvalidGrantOperationErrorCode       = "0LP01"
)
View Source
const MaxIdentifierLength = 63
View Source
const MinUserSplit = 1

Variables

View Source
var (
	DefaultAttributeConnectionLimit = -1
	DefaultAttributeReplication     = false
	DefaultAttributeBypassRLS       = false
)

Functions

func CloseAllSavedPoolsForName

func CloseAllSavedPoolsForName(name string) error

func CloseDatabaseSavedPoolsForName

func CloseDatabaseSavedPoolsForName(name, database string) error

func TemplatePostgresqlURL

func TemplatePostgresqlURL(host, user, password, database string, port int) string

func TemplatePostgresqlURLWithArgs

func TemplatePostgresqlURLWithArgs(host, user, password, uriArgs, database string, port int) string

Types

type CreatePublicationBuilder

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

func NewCreatePublicationBuilder

func NewCreatePublicationBuilder() *CreatePublicationBuilder

func (*CreatePublicationBuilder) AddTable

func (b *CreatePublicationBuilder) AddTable(name string, columns *[]string, additionalWhere *string) *CreatePublicationBuilder

func (*CreatePublicationBuilder) Build

func (b *CreatePublicationBuilder) Build()

func (*CreatePublicationBuilder) SetForAllTables

func (b *CreatePublicationBuilder) SetForAllTables() *CreatePublicationBuilder

func (*CreatePublicationBuilder) SetName

func (*CreatePublicationBuilder) SetTablesInSchema

func (b *CreatePublicationBuilder) SetTablesInSchema(schemaList []string) *CreatePublicationBuilder

func (*CreatePublicationBuilder) SetWith

func (b *CreatePublicationBuilder) SetWith(publish string, publishViaPartitionRoot *bool) *CreatePublicationBuilder

type PG

type PG interface {
	CreateDB(ctx context.Context, dbname, username string) error
	ChangeDBOwner(ctx context.Context, dbname, owner string) error
	IsDatabaseExist(ctx context.Context, dbname string) (bool, error)
	RenameDatabase(ctx context.Context, oldname, newname string) error
	CreateSchema(ctx context.Context, db, role, schema string) error
	CreateExtension(ctx context.Context, db, extension string) error
	CreateGroupRole(ctx context.Context, role string) error
	CreateUserRole(ctx context.Context, role, password string, attributes *RoleAttributes) (string, error)
	AlterRoleAttributes(ctx context.Context, role string, attributes *RoleAttributes) error
	GetRoleAttributes(ctx context.Context, role string) (*RoleAttributes, error)
	IsRoleExist(ctx context.Context, role string) (bool, error)
	RenameRole(ctx context.Context, oldname, newname string) error
	UpdatePassword(ctx context.Context, role, password string) error
	GrantRole(ctx context.Context, role, grantee string, withAdminOption bool) error
	SetSchemaPrivileges(ctx context.Context, db, creator, role, schema, privs string) error
	RevokeRole(ctx context.Context, role, userRole string) error
	AlterDefaultLoginRole(ctx context.Context, role, setRole string) error
	AlterDefaultLoginRoleOnDatabase(ctx context.Context, role, setRole, database string) error
	RevokeUserSetRoleOnDatabase(ctx context.Context, role, database string) error
	DoesRoleHaveActiveSession(ctx context.Context, role string) (bool, error)
	DropDatabase(ctx context.Context, db string) error
	DropRoleAndDropAndChangeOwnedBy(ctx context.Context, role, newOwner, database string) error
	ChangeAndDropOwnedBy(ctx context.Context, role, newOwner, database string) error
	GetSetRoleOnDatabasesRoleSettings(ctx context.Context, role string) ([]*SetRoleOnDatabaseRoleSetting, error)
	DropRole(ctx context.Context, role string) error
	DropSchema(ctx context.Context, database, schema string, cascade bool) error
	DropExtension(ctx context.Context, database, extension string, cascade bool) error
	GetRoleMembership(ctx context.Context, role string) ([]string, error)
	GetTablesInSchema(ctx context.Context, db, schema string) ([]*TableOwnership, error)
	ChangeTableOwner(ctx context.Context, db, table, owner string) error
	GetTypesInSchema(ctx context.Context, db, schema string) ([]*TypeOwnership, error)
	ChangeTypeOwnerInSchema(ctx context.Context, db, schema, typeName, owner string) error
	DropPublication(ctx context.Context, dbname, name string) error
	RenamePublication(ctx context.Context, dbname, oldname, newname string) error
	GetPublication(ctx context.Context, dbname, name string) (*PublicationResult, error)
	CreatePublication(ctx context.Context, dbname string, builder *CreatePublicationBuilder) error
	UpdatePublication(ctx context.Context, dbname, publicationName string, builder *UpdatePublicationBuilder) error
	DropReplicationSlot(ctx context.Context, name string) error
	CreateReplicationSlot(ctx context.Context, dbname, name, plugin string) error
	GetReplicationSlot(ctx context.Context, name string) (*ReplicationSlotResult, error)
	GetUser() string
	GetHost() string
	GetPort() int
	GetDefaultDatabase() string
	GetArgs() string
	Ping(ctx context.Context) error
}

func NewPG

func NewPG(
	name,
	host,
	user,
	password,
	args,
	defaultDatabase string,
	port int,
	cloudType v1alpha1.ProviderType,
	logger logr.Logger,
) PG

type PublicationResult

type PublicationResult struct {
	AllTables          bool
	Insert             bool
	Update             bool
	Delete             bool
	Truncate           bool
	PublicationViaRoot bool
}

type PublicationTableDetail

type PublicationTableDetail struct {
	SchemaName      string
	TableName       string
	AdditionalWhere *string
	Columns         []string
}

type ReplicationSlotResult

type ReplicationSlotResult struct {
	SlotName string
	Plugin   string
	Database string
}

type RoleAttributes

type RoleAttributes struct {
	ConnectionLimit *int
	Replication     *bool
	BypassRLS       *bool
}

type SetRoleOnDatabaseRoleSetting

type SetRoleOnDatabaseRoleSetting struct {
	Role     string
	Database string
}

type TableOwnership

type TableOwnership struct {
	TableName string
	Owner     string
}

type TypeOwnership

type TypeOwnership struct {
	TypeName string
	Owner    string
}

type UpdatePublicationBuilder

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

func NewUpdatePublicationBuilder

func NewUpdatePublicationBuilder() *UpdatePublicationBuilder

func (*UpdatePublicationBuilder) AddSetTable

func (b *UpdatePublicationBuilder) AddSetTable(name string, columns *[]string, additionalWhere *string) *UpdatePublicationBuilder

func (*UpdatePublicationBuilder) Build

func (b *UpdatePublicationBuilder) Build()

func (*UpdatePublicationBuilder) RenameTo

func (*UpdatePublicationBuilder) SetTablesInSchema

func (b *UpdatePublicationBuilder) SetTablesInSchema(schemaList []string) *UpdatePublicationBuilder

func (*UpdatePublicationBuilder) SetWith

func (b *UpdatePublicationBuilder) SetWith(publish string, publishViaPartitionRoot *bool) *UpdatePublicationBuilder

Jump to

Keyboard shortcuts

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