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
- func AllowHost(ctx context.Context, lists [][]string, host, hint string) error
- func AllowHosts(ctx context.Context, driver, dsn string, lists [][]string, hint string) error
- func Flavor(ctx context.Context, q Querier) (string, error)
- func IsMariaDB(version string) bool
- func MySQLCharset(ctx context.Context, q Querier, db string) (charset, collation string, err error)
- func MySQLDatabaseExists(ctx context.Context, q Querier, db string) (bool, error)
- func OpenMySQL(dsn, db string, dial DialFunc) (*sql.DB, error)
- func OpenPostgres(dsn, db string, dial DialFunc) (*sql.DB, error)
- func PostgresDSN(dsn, db string) (string, error)
- func PostgresNotExtension(oid, class string) string
- func PostgresUserSchema(col string) string
- func QuoteMySQL(name string) string
- func QuotePostgres(name string) string
- func ShowCreate(ctx context.Context, q Querier, kind, db, name string, missingOK bool) (map[string]string, error)
- func ShowCreateSQL(ctx context.Context, q Querier, kind, db, name string, missingOK bool) (string, error)
- type DialFunc
- type MySQLObject
- type MySQLSession
- type MySQLTrigger
- type Querier
Constants ¶
const ( FlavorMySQL = "mysql" FlavorMariaDB = "mariadb" )
Servers a MySQL DSN may reach.
Variables ¶
This section is empty.
Functions ¶
func AllowHost ¶
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 ¶
AllowHosts checks every host the driver may connect to with the DSN (several hosts, fallbacks, PostgreSQL's host= parameter) with AllowHost.
func MySQLCharset ¶
MySQLCharset reads the default character set and collation of a database; sql.ErrNoRows when it does not exist.
func MySQLDatabaseExists ¶
MySQLDatabaseExists tells whether the server has the database.
func OpenMySQL ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
QuoteMySQL quotes an identifier for MySQL and MariaDB.
func QuotePostgres ¶
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.
Types ¶
type DialFunc ¶
DialFunc connects to a database server: network is tcp or unix, addr host:port or the socket's path.
func AllowDial ¶
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 ¶
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) 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 ¶
MySQLTrigger is a trigger and its place among the triggers of its table.
func MySQLTriggers ¶
MySQLTriggers lists the triggers of a database in firing order per table, the order they are created again in.