dbutil

package
v2.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DatabaseName

func DatabaseName(u *url.URL) string

DatabaseName returns the database name from a URL

func MustClose

func MustClose(c io.Closer)

MustClose ensures a stream is closed

func MustUnescapePath

func MustUnescapePath(s string) string

MustUnescapePath unescapes a URL path, and panics if it fails. It is used during in cases where we are parsing a generated path.

func QueryColumn

func QueryColumn(db Transaction, query string, args ...interface{}) ([]string, error)

QueryColumn runs a SQL statement and returns a slice of strings it is assumed that the statement returns only one column e.g. schema_migrations table

func QueryValue

func QueryValue(db Transaction, query string, args ...interface{}) (string, error)

QueryValue runs a SQL statement and returns a single string it is assumed that the statement returns only one row and one column sql NULL is returned as empty string

func RunCommand

func RunCommand(name string, args ...string) ([]byte, error)

RunCommand runs a command and returns the stdout if successful

func StripPsqlMetaCommands

func StripPsqlMetaCommands(data []byte) ([]byte, error)

StripPsqlMetaCommands removes psql meta-commands (backslash commands) from SQL. PostgreSQL 15.14+/16.10+/17.6+ pg_dump adds \restrict and \unrestrict commands to schema dumps as a security measure (CVE-2025-8714). These are psql client commands that cannot be executed directly against the PostgreSQL server.

Note: This is a naive line-based implementation that does not parse SQL properly. It will incorrectly strip lines inside multi-line string literals if they start with a backslash, e.g.: INSERT INTO t VALUES ('line1\n\line2'); In practice this is not an issue because:

  • This function is only used to process pg_dump schema output
  • pg_dump generates DDL statements which rarely contain multi-line string literals
  • pg_dump uses proper escaping (E'...' or $$...$$) for complex strings

func TrimLeadingSQLComments

func TrimLeadingSQLComments(data []byte) ([]byte, error)

TrimLeadingSQLComments removes sql comments and blank lines from the beginning of text generally when performing sql dumps these contain host-specific information such as client/server version numbers

Types

type Transaction

type Transaction interface {
	Exec(query string, args ...interface{}) (sql.Result, error)
	Query(query string, args ...interface{}) (*sql.Rows, error)
	QueryRow(query string, args ...interface{}) *sql.Row
}

Transaction can represent a database or open transaction

Jump to

Keyboard shortcuts

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