Help us shape the future of Go! Take the 2026 Go Developer Survey

dbx

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package dbx holds what the state components (internal/state) and the data module (internal/data) both do with MySQL/MariaDB and PostgreSQL servers: connecting, quoting names, setting up a session, telling MariaDB from MySQL, reading SHOW CREATE, filtering PostgreSQL's system schemas, and checking that a database is on a host DriftGate may rewrite. Reading a DSN stays in internal/dbdsn, which keeps every error free of the DSN.

Where the two callers differ on purpose, the difference is a parameter here, with the reason at the parameter.

Index

Constants

View Source
const (
	FlavorMySQL   = "mysql"
	FlavorMariaDB = "mariadb"
)

Servers a MySQL DSN may reach.

Variables

This section is empty.

Functions

func AllowHost

func AllowHost(ctx context.Context, lists [][]string, host, hint string) error

AllowHost allows a local socket ("") and a host that one of the allow lists (config.Config.AllowHostLists) allows. Without any list only the local system is allowed, not the private ranges an HTTP target may use: a database on a private address may well be production, and both a state restore and a data target rewrite it. hint names the setting that turns the check off for the caller.

func AllowHosts

func AllowHosts(ctx context.Context, driver, dsn string, lists [][]string, hint string) error

AllowHosts checks every host the driver may connect to with the DSN (several hosts, fallbacks, PostgreSQL's host= parameter) with AllowHost.

func Flavor

func Flavor(ctx context.Context, q Querier) (string, error)

Flavor reads which server a MySQL connection reached: FlavorMySQL or FlavorMariaDB.

func IsMariaDB

func IsMariaDB(version string) bool

IsMariaDB tells MariaDB from MySQL by the text of VERSION().

func MySQLCharset

func MySQLCharset(ctx context.Context, q Querier, db string) (charset, collation string, err error)

MySQLCharset reads the default character set and collation of a database; sql.ErrNoRows when it does not exist.

func MySQLDatabaseExists

func MySQLDatabaseExists(ctx context.Context, q Querier, db string) (bool, error)

MySQLDatabaseExists tells whether the server has the database.

func OpenMySQL

func OpenMySQL(dsn, db string, dial DialFunc) (*sql.DB, error)

OpenMySQL connects to a MySQL or MariaDB server with the configuration dbdsn.MySQLConfig makes of the DSN, and db as the default database ("" for none). That configuration interpolates parameters on the client, so DriftGate's own queries never prepare a statement on the server: the state components' write check leaves Com_stmt_execute to the application. A dial function other than nil makes every connection (AllowDial).

func OpenPostgres

func OpenPostgres(dsn, db string, dial DialFunc) (*sql.DB, error)

OpenPostgres connects to a PostgreSQL server with the URL PostgresDSN makes. A dial function other than nil makes every connection, and gets the host by name: pgx's own lookup is turned off, so the name is resolved once, by the dial function, which checks the address it connects to.

func PostgresDSN

func PostgresDSN(dsn, db string) (string, error)

PostgresDSN is the connection string for the pgx driver: the postgres:// URL with the database db ("" keeps the URL's own, default postgres).

func PostgresNotExtension

func PostgresNotExtension(oid, class string) string

PostgresNotExtension is an SQL condition that the object oid of the catalog class (pg_class, pg_proc…) does not belong to an extension.

Only the data module uses it: it writes CREATE EXTENSION, which makes the extension's objects again, so copying them too would fail. The state components fingerprint extension objects with the rest: their rows can change (PostGIS's spatial_ref_sys), and a restore copies the whole database anyway.

func PostgresUserSchema

func PostgresUserSchema(col string) string

PostgresUserSchema is an SQL condition that the schema named in col is one of the database itself: not pg_catalog, information_schema or another schema of the system (pg_toast, pg_temp_N, pg_toast_temp_N; PostgreSQL keeps the prefix pg_ for them, and the underscore is escaped, so pgxtemp is a user schema). The state components and the data module both use it.

func QuoteMySQL

func QuoteMySQL(name string) string

QuoteMySQL quotes an identifier for MySQL and MariaDB.

func QuotePostgres

func QuotePostgres(name string) string

QuotePostgres quotes an identifier for PostgreSQL.

func ShowCreate

func ShowCreate(ctx context.Context, q Querier, kind, db, name string, missingOK bool) (map[string]string, error)

ShowCreate runs SHOW CREATE kind (TABLE, VIEW, SEQUENCE, TRIGGER…) on db.name and returns its columns by name.

missingOK decides what an object that does not exist is (errors 1146 and 1360, or no row): with it, nil and no error; without it, an error. The state components pass true: they compare the working and the pristine database, and an object one of them lacks is a difference they restore, not a failure. The data module passes false: it reads objects it has just listed, and one that is gone means the source changed under the read, which must not end in a copy without it.

func ShowCreateSQL

func ShowCreateSQL(ctx context.Context, q Querier, kind, db, name string, missingOK bool) (string, error)

ShowCreateSQL returns the CREATE statement of a table, view or sequence; "" for one that does not exist with missingOK (see ShowCreate).

Types

type DialFunc

type DialFunc func(ctx context.Context, network, addr string) (net.Conn, error)

DialFunc connects to a database server: network is tcp or unix, addr host:port or the socket's path.

func AllowDial

func AllowDial(lists [][]string, hint string) (DialFunc, error)

AllowDial returns a dial function that connects only to a host one of the allow lists (config.Config.AllowHostLists) allows, as AllowHost decides: a local socket, a host named in a list, or a name whose address is in an allowed range. The address is checked when the connection is made, and the connection goes to the checked address, so a name that resolves elsewhere after AllowHost (DNS rebinding) is not reached. hint names the setting that turns the check off.

type MySQLObject

type MySQLObject struct{ Name, Type string }

MySQLObject is a table, view or sequence: TABLE_TYPE is BASE TABLE, VIEW, SEQUENCE (MariaDB) or SYSTEM VERSIONED (MariaDB).

func MySQLObjects

func MySQLObjects(ctx context.Context, q Querier, db string) ([]MySQLObject, error)

MySQLObjects lists the tables, views and sequences of a database by name.

type MySQLSession

type MySQLSession struct {
	// UTF8UTC sets utf8mb4 and UTC for TIMESTAMP columns, so values read
	// the same on every server. The data module sets it; the state
	// components do not, as they copy rows inside one server and hash the
	// text the server prints in its own session settings.
	UTF8UTC bool
	// SQLMode is the session's sql_mode. The data module wants a value
	// that does not fit its column to be an error; the state components
	// want only NO_AUTO_VALUE_ON_ZERO, so old values (zero dates) and a 0
	// in an AUTO_INCREMENT column are copied back as they were.
	SQLMode string
	// FreshStats turns MySQL 8's cache of table statistics off for the
	// session (information_schema_stats_expiry = 0), so UPDATE_TIME and
	// sizes are current; MariaDB does not know the variable and is left as
	// it is. Only the state components read those statistics.
	FreshStats bool
}

MySQLSession sets up a MySQL or MariaDB connection for copying data as it is: no checks of references and uniqueness while rows are loaded (they were valid where they came from).

func (MySQLSession) Apply

func (s MySQLSession) Apply(ctx context.Context, c *sql.Conn) error

Apply runs the session's statements on c.

func (MySQLSession) Statements

func (s MySQLSession) Statements() []string

Statements are the SET statements of the session, also the head of a dump that loads into a server the same way (FreshStats is not among them).

type MySQLTrigger

type MySQLTrigger struct {
	Name, Table, Timing, Event string
	Order                      int
}

MySQLTrigger is a trigger and its place among the triggers of its table.

func MySQLTriggers

func MySQLTriggers(ctx context.Context, q Querier, db string) ([]MySQLTrigger, error)

MySQLTriggers lists the triggers of a database in firing order per table, the order they are created again in.

type Querier

type Querier interface {
	QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
	QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
}

Querier is a *sql.DB, *sql.Conn or *sql.Tx.

Jump to

Keyboard shortcuts

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