pglint

package
v1.156.0 Latest Latest
Warning

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

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

Documentation

Overview

Package pglint reads an application database migration before it saves (#2405, #2449, #2539): the statements that lock, rewrite or remove data a live table holds, the statements that leave the database open to a role that should not reach it or need a privilege an application's role must not hold, the features the server's major lacks, and the roles a migration names outside its schema. It reads SQL lexically and touches no database; the postgres toolkit's review and an application import run it.

Index

Constants

View Source
const (
	// SeverityWarning is a statement that locks or rewrites a live table; it
	// is reported and recorded, and the migration saves.
	SeverityWarning = "warning"
	// SeverityDestructive is a statement that removes data; the migration
	// saves only with an agreed summary of what it removes.
	SeverityDestructive = "destructive"
)

The severities a finding has.

View Source
const (
	RuleIndexLocks          = "create_index_locks"
	RuleNotNullNoDefault    = "not_null_without_default"
	RuleVolatileDefault     = "volatile_default_rewrites"
	RuleTypeChangeRewrites  = "type_change_rewrites"
	RuleConstraintValidates = "constraint_validates"
	RuleDropTable           = "drop_table"
	RuleDropColumn          = "drop_column"
	RuleTruncate            = "truncate"
	RuleUnboundedDelete     = "unbounded_delete"
	RuleUnboundedUpdate     = "unbounded_update"
	RuleRenameReferenced    = "rename_referenced"
	RuleLockTimeoutRaised   = "lock_timeout_raised"
	RuleFromZeroUnchecked   = "from_zero_unchecked"
)

The lint's rules.

View Source
const (
	RuleIndexVirtualColumn = "index_on_virtual_column"
	RuleNeedsNewerServer   = "feature_needs_newer_server"
	RuleTargetUnread       = "target_unread"
)

The rules.

View Source
const MigrationLockTimeout = 10 * time.Second

MigrationLockTimeout is how long one migration's statement waits for a lock a running query holds, so a migration never queues behind a long read while every later query queues behind it: what the postgres toolkit sets, and the bound the lock_timeout rule holds a migration to.

View Source
const SeverityRefused = "refused"

SeverityRefused is a statement that needs a privilege an application's role must not hold or reaches outside the database; the migration does not save.

Variables

This section is empty.

Functions

func Lint

func Lint(up string, target Target) ([]Finding, []Rename)

Lint reads a migration's up SQL for the server and schema target describes. It returns what it found and the objects the migration renames, which the caller looks up in the saved scripts.

Types

type Finding

type Finding struct {
	Rule     string `json:"rule"`
	Severity string `json:"severity"`
	// Statement is the statement's place in the up SQL, from 1.
	Statement int `json:"statement,omitempty"`
	// Object is the table, column or setting the statement acts on.
	Object string `json:"object,omitempty"`
	// Effect is what the statement does to a live table.
	Effect string `json:"effect"`
	// Safe is the form that does not.
	Safe string `json:"safe_form,omitempty"`
	// Scripts names the saved scripts that name a renamed object.
	Scripts []string `json:"scripts,omitempty"`
}

Finding is one thing the lint found in a migration.

func Destructive

func Destructive(fs []Finding) []Finding

Destructive is the findings that need an agreed summary.

func Refused

func Refused(fs []Finding) []Finding

Refused is the findings no migration may save with.

type Rename

type Rename struct {
	Statement int
	// Table is the table renamed, or the table whose column is renamed.
	Table string
	// Column is the column renamed; empty for a table.
	Column string
}

Rename is a table or column a migration renames.

func (Rename) Name

func (r Rename) Name() string

Name is the identifier a script would name: the column, or the table.

func (Rename) Object

func (r Rename) Object() string

Object is the renamed object as a finding names it.

type Target

type Target struct {
	// Major is the server's major, 0 when it could not be read.
	Major int
	// RowSecurity is the schema's tables with row-level security enabled.
	RowSecurity map[string]bool
	// Virtual is the schema's virtual generated columns, by table.
	Virtual map[string]map[string]bool
	// Schema is the schema the migration is for, whose name every role the
	// migration names starts with (#2539); empty checks no role.
	Schema string
}

Target is what the review knows of the server and schema a migration is for. A zero Target leaves the version rules silent.

Jump to

Keyboard shortcuts

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