Documentation
¶
Overview ¶
Package dbx runs classified SQL against a conn and turns the result into plain JSON values. Without write it uses a read-only transaction, so the database itself refuses changes the classifier may have missed.
Index ¶
- Constants
- func Activity(ctx context.Context, db *sql.DB, kind string, all bool, timeout time.Duration) ([]map[string]any, error)
- func ClassifyMongo(cmd bson.D) driver.Verdict
- func Dump(ctx context.Context, db *sql.DB, kind string, o DumpOptions, w io.Writer) error
- func Info(ctx context.Context, db *sql.DB, kind string, timeout time.Duration) (map[string]any, error)
- func Locks(ctx context.Context, db *sql.DB, kind string, timeout time.Duration) ([]map[string]any, error)
- func MongoActivity(ctx context.Context, m *Mongo, all bool) ([]map[string]any, error)
- func MongoExplain(ctx context.Context, m *Mongo, db string, cmd bson.D, analyze bool) (any, error)
- func MongoInfo(ctx context.Context, m *Mongo) (map[string]any, error)
- func MongoRules() []driver.Rule
- func MongoVersion(ctx context.Context, m *Mongo) string
- func NotSupported(kind, what string) error
- func Open(ctx context.Context, rawURL string, t driver.Target, password string, ...) (*sql.DB, error)
- func ParseBytes(s string) (int64, error)
- func ParseMongoCommand(text string) (bson.D, string, error)
- func PostgresTargets(rawURL string) []string
- func Restore(ctx context.Context, db *sql.DB, kind string, r io.Reader) error
- func SQLitePath(t driver.Target) string
- func Sizes(ctx context.Context, db *sql.DB, kind string, limit int, timeout time.Duration) (map[string]any, error)
- func UserChange(ctx context.Context, db *sql.DB, kind, action string, u UserSpec, dry bool, ...) (string, error)
- type Column
- type Constraint
- type DialFunc
- type DumpOptions
- type Index
- type Mongo
- type MongoCollection
- type MongoField
- type MongoResult
- type Options
- type Result
- type StatementResult
- type Table
- type TableColumn
- type UnsupportedError
- type UserSpec
Constants ¶
const BQDefaultMaxBytes int64 = 10 << 30
BQDefaultMaxBytes is the most a query may read without --max-bytes: 10 GiB, a few cents at on-demand prices.
Variables ¶
This section is empty.
Functions ¶
func Activity ¶
func Activity(ctx context.Context, db *sql.DB, kind string, all bool, timeout time.Duration) ([]map[string]any, error)
Activity lists sessions: by default only those running something.
func ClassifyMongo ¶ added in v0.7.0
ClassifyMongo rates a command document.
func Dump ¶
Dump writes a plain SQL backup of the database, made by komando itself (no pg_dump or mysqldump). It reads inside one consistent snapshot. Objects it cannot write faithfully stop it with their names, so a dump never silently misses something.
func Info ¶
func Info(ctx context.Context, db *sql.DB, kind string, timeout time.Duration) (map[string]any, error)
Info is the server at a glance: version, uptime, size, connections, and whether it is a primary or a read-only replica.
func Locks ¶
func Locks(ctx context.Context, db *sql.DB, kind string, timeout time.Duration) ([]map[string]any, error)
Locks lists sessions that wait for a lock, with the session holding it.
func MongoActivity ¶ added in v0.14.0
MongoActivity is what the server is doing now (currentOp), one row per operation in the shape db activity shows for SQL: pid (opid), user, database, running_seconds, state (the operation), query (the command). all includes idle connections.
func MongoExplain ¶ added in v0.14.0
MongoExplain is the plan MongoDB makes for cmd (find, aggregate, count, distinct ...) in database db: queryPlanner, or with analyze executionStats, which runs the read and reports what it did. The caller lets only read commands through.
func MongoRules ¶ added in v0.7.0
MongoRules lists the rules ClassifyMongo can report.
func MongoVersion ¶ added in v0.7.0
MongoVersion is the server version, for `komando test`.
func NotSupported ¶ added in v0.12.0
NotSupported is the stop for a db command that kind has no use for: SQLite, a file with no server, has no sessions and no users.
func Open ¶
func Open(ctx context.Context, rawURL string, t driver.Target, password string, dial DialFunc, readOnly bool, appName string) (*sql.DB, error)
Open connects to t. dial is nil for a direct connection. readOnly opens file databases read-only as well.
func ParseBytes ¶ added in v0.9.0
ParseBytes reads a size such as 10GB, 500MiB, 1TB or 1048576 (bytes); the units are powers of 1024, as BigQuery bills. 0 is no cap.
func ParseMongoCommand ¶ added in v0.7.0
ParseMongoCommand reads a command document written as JSON (MongoDB Extended JSON: {"$oid": ...}, {"$date": ...} work). A "$db" key names the database for this command and is taken out.
func PostgresTargets ¶ added in v0.14.1
PostgresTargets are the host:port pairs pgx logs in at for rawURL, its fallbacks included, read as Open reads it.
func Restore ¶
Restore runs a plain SQL backup (komando's own, pg_dump's or mysqldump's) without psql or mysql. PostgreSQL restores in one transaction and stops at the first error, like psql --single-transaction -v ON_ERROR_STOP=1; COPY ... FROM stdin blocks are streamed. MySQL runs the statements in order and understands DELIMITER. The file must have passed driver.CheckRestore.
func SQLitePath ¶ added in v0.12.0
SQLitePath is the file a sqlite conn opens, with ~/ read as the home folder (as `komando test` does).
func Sizes ¶
func Sizes(ctx context.Context, db *sql.DB, kind string, limit int, timeout time.Duration) (map[string]any, error)
Sizes lists databases, the biggest tables and the biggest indexes.
func UserChange ¶ added in v0.3.0
func UserChange(ctx context.Context, db *sql.DB, kind, action string, u UserSpec, dry bool, cmd func(verb string) string) (string, error)
UserChange creates the user or sets its password and returns the statement as it may be shown. With dry, nothing runs. cmd gives the words after komando of user VERB, for the hints.
Types ¶
type Constraint ¶
type DumpOptions ¶
DumpOptions narrow a built-in dump.
type Mongo ¶ added in v0.7.0
Mongo is an open MongoDB conn and the database its commands use.
func OpenMongo ¶ added in v0.7.0
func OpenMongo(ctx context.Context, rawURL string, t driver.Target, password string, dial DialFunc, appName string, timeout time.Duration) (*Mongo, error)
OpenMongo connects to a MongoDB conn: the URL as the driver reads it (hosts, replicaSet, authSource, tls), the password from the vault, every member through dial when there is one, and ?ca= for its certificate.
type MongoCollection ¶ added in v0.7.0
type MongoCollection struct {
Name string `json:"name"`
Type string `json:"type,omitempty"` // view, timeseries
Documents int64 `json:"documents"` // an estimate, from the collection's metadata
Indexes []string `json:"indexes,omitempty"`
Fields []MongoField `json:"fields,omitempty"`
Sampled int `json:"sampled,omitempty"`
}
MongoCollection is one collection in `db schema`: its size and indexes, and with a name given, the fields of a sample of its documents.
func MongoSchema ¶ added in v0.7.0
MongoSchema lists the collections, or describes the named ones from a sample of up to 100 documents each.
type MongoField ¶ added in v0.7.0
type MongoField struct {
Path string `json:"path"`
Types []string `json:"types"`
Seen int `json:"seen"`
}
MongoField is a field path seen in sampled documents, with its types and how many of them had it.
type MongoResult ¶ added in v0.7.0
type MongoResult struct {
Documents []any `json:"documents,omitempty"`
Count int `json:"count"`
Truncated string `json:"truncated,omitempty"` // "documents" (--limit) or "bytes" (--max-output)
Reply any `json:"reply,omitempty"`
}
MongoResult is what one command gave: documents for the commands that return a cursor (find, aggregate, listCollections, ...), else the reply.
type Options ¶
type Options struct {
Write bool
DryRun bool
Limit int // max rows per statement
MaxBytes int // rough cap on the JSON size of all rows
Timeout time.Duration
Args []any
// MaxBilled is the most a BigQuery query may read (--max-bytes): 0 is
// BQDefaultMaxBytes, below 0 no cap.
MaxBilled int64
}
Options control one Run.
type Result ¶
type Result struct {
Results []StatementResult `json:"results"`
Committed bool `json:"committed"`
RolledBack bool `json:"rolled_back,omitempty"`
}
Result is the outcome of a Run.
func Run ¶
func Run(ctx context.Context, db *sql.DB, kind string, plan driver.SQLPlan, o Options) (*Result, error)
Run executes plan on db in one transaction. Without Write the transaction is read-only and always rolled back; with DryRun it is rolled back too. Statements that cannot run in a transaction must be alone in the plan; in a dry run they are reported but not executed.
type StatementResult ¶
type StatementResult struct {
SQL string `json:"sql"`
Keyword string `json:"keyword"`
Risk string `json:"risk"`
Columns []Column `json:"columns,omitempty"`
Rows [][]any `json:"-"`
RowCount int `json:"-"`
Truncated string `json:"truncated,omitempty"` // which cap cut the rows: "rows" (--limit) or "bytes" (--max-output)
RowsAffected *int64 `json:"rows_affected,omitempty"`
Skipped string `json:"skipped,omitempty"` // why it did not run (dry run of a non-rollbackable statement)
// BytesProcessed is what a BigQuery statement read, which BigQuery
// bills by (in a dry run or db explain: what it would read)
BytesProcessed *int64 `json:"bytes_processed,omitempty"`
}
StatementResult is the outcome of one statement. A statement that returns rows has columns, rows and row_count (rows may be empty); one that changes rows has rows_affected; other statements (DDL, SET) have neither.
func (StatementResult) MarshalJSON ¶
func (r StatementResult) MarshalJSON() ([]byte, error)
type Table ¶
type Table struct {
Schema string `json:"schema,omitempty"`
Name string `json:"name"`
Type string `json:"type"`
Engine string `json:"engine,omitempty"` // ClickHouse: MergeTree, ReplicatedMergeTree, View, ...
RowEstimate *int64 `json:"row_estimate,omitempty"`
Columns []TableColumn `json:"columns,omitempty"`
Indexes []Index `json:"indexes,omitempty"`
// Constraints are foreign keys, checks and unique constraints.
Constraints []Constraint `json:"constraints,omitempty"`
}
Table describes one table or view. Columns and Indexes are filled only when the table was asked for by name.
type TableColumn ¶
type TableColumn struct {
Name string `json:"name"`
Type string `json:"type"`
Nullable bool `json:"nullable"`
Default *string `json:"default,omitempty"`
PrimaryKey bool `json:"primary_key,omitempty"`
// Identity says the database numbers the column itself: "auto_increment"
// (MySQL, SQLite AUTOINCREMENT), "always" or "by default" (PostgreSQL),
// "identity" (SQL Server).
Identity string `json:"identity,omitempty"`
}
type UnsupportedError ¶
type UnsupportedError struct{ Objects []string }
UnsupportedError lists objects the built-in dump does not write.
func (*UnsupportedError) Error ¶
func (e *UnsupportedError) Error() string
Source Files
¶
- bigquery.go
- bigquery_rows.go
- clickhouse.go
- clickhouse_rows.go
- data_values.go
- dbx.go
- dump_mysql.go
- dump_pg.go
- gcp_bigquery.go
- mongo.go
- mongo_classify.go
- mysql.go
- ops.go
- ops_bigquery.go
- ops_clickhouse.go
- ops_oracle.go
- ops_snowflake.go
- ops_sqlserver.go
- oracle.go
- restore.go
- schema.go
- schema_bigquery.go
- schema_clickhouse.go
- schema_oracle.go
- schema_snowflake.go
- schema_sqlserver.go
- snowflake.go
- snowflake_auth.go
- snowflake_rows.go
- sqlserver.go
- users.go