introspect

package
v0.1.13 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package introspect defines schema introspection: discovering entity metadata from a live database and matching configured entities to it. The Introspector interface lives here; providers in x/ implement it. Catalog resolves the table an entity reads, and Reconcile, pure logic run at startup, fails fast on mismatched configuration (a referenced column that no longer exists in the DB) and fills descriptions from database comments.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Catalog added in v0.1.11

type Catalog struct {
	Tables []entity.Entity
	// Default is where an entity without a schema reads; when unknown, such
	// an entity matches a table name unique in the catalog.
	Default string
	// FoldCase matches schema and table names case-insensitively, as the
	// database compares them (see Physical.FoldCase).
	FoldCase bool
	// contains filtered or unexported fields
}

Catalog is a set of discovered tables and the schema unqualified table names resolve to. Every lookup of the table an entity reads goes through it, so drift checks, comment defaults and imports agree with the generated SQL.

func LoadCatalog added in v0.1.11

func LoadCatalog(ctx context.Context, in Introspector, schemas []string, withDefault bool) (Catalog, error)

LoadCatalog discovers the named schemas and, with withDefault, the default one, and records which schema is the default.

func (*Catalog) Index added in v0.1.12

func (c *Catalog) Index()

Index builds the name index LoadCatalog returns catalogs with, so lookups of every configured entity do not scan every table. A catalog put together otherwise is indexed once its Tables are set.

func (Catalog) Lookup added in v0.1.11

func (c Catalog) Lookup(schema, table string) (entity.Entity, bool)

Lookup finds the table named by schema (empty for the default) and table.

type Drift

type Drift struct {
	// Missing lists configured entities/fields absent from the DB
	// (entity name, or "entity.field").
	Missing []string
	// Extra lists DB columns not present in the config.
	Extra []string
	// TypeChanged lists fields whose DB type differs from the config.
	TypeChanged []string
}

Drift describes differences between configured and discovered schemas.

func Reconcile added in v0.1.11

func Reconcile(configured []entity.Entity, c Catalog) ([]entity.Entity, Drift)

Reconcile matches configured table and view entities to their tables in the catalog. It returns the entities with each field's domain (type, nullability, defaults) and empty entity and field descriptions taken from the database (a configured description overrides a comment), and the drift: missing entities by name, missing and extra fields as "entity.field". Procedures are returned unchanged. It is pure and deterministic.

type Introspector

type Introspector interface {
	Discover(ctx context.Context, sources []string) ([]entity.Entity, error)
}

Introspector discovers entity metadata (tables, columns, keys, procedure parameters) from a live database. Implementations live in x/providers.

type Physical added in v0.1.11

type Physical struct {
	// Server identifies the database server; "" when unknown.
	Server string
	// Catalog is the database holding the schemas (PostgreSQL); "" where a
	// schema is a database (MySQL).
	Catalog string
	// FoldCase reports that schema and table names compare
	// case-insensitively (MySQL lower_case_table_names).
	FoldCase bool
}

Physical says where a connection's relations live, so assembly can tell when two entities, possibly configured on different datasources, expose the same relation.

func (Physical) RelationKey added in v0.1.11

func (p Physical) RelationKey(schema, table string) string

RelationKey identifies schema.table on this server; schema must already be resolved (not empty for the default).

type PhysicalNamer added in v0.1.11

type PhysicalNamer interface {
	Physical(ctx context.Context) (Physical, error)
}

PhysicalNamer is implemented by introspectors that can identify their server and how it compares names.

type Privilege added in v0.1.11

type Privilege uint8

Privilege is what a connection's account may do: granted, denied, or unknown when the database does not say (such as privileges MySQL grants through roles). It informs administrators only; the database decides.

const (
	PrivilegeUnknown Privilege = iota
	PrivilegeGranted
	PrivilegeDenied
)

func GrantedIf added in v0.1.11

func GrantedIf(granted bool) Privilege

GrantedIf maps a known answer to a privilege.

func (Privilege) String added in v0.1.11

func (p Privilege) String() string

type PrivilegeInspector added in v0.1.11

type PrivilegeInspector interface {
	// ReadOnly reports a server or session that refuses every write (a
	// replica, a hot standby, read_only).
	ReadOnly(ctx context.Context) (bool, error)
	// TablePrivileges reports the privileges on tables of schema ("" for the
	// default one) in one round of queries; a table the connection cannot
	// see is left out.
	TablePrivileges(ctx context.Context, schema string, tables []string) (map[string]TablePrivileges, error)
	// ProcedurePrivilege reports EXECUTE on a procedure; unknown when its
	// name is overloaded.
	ProcedurePrivilege(ctx context.Context, schema, name string) (Privilege, error)
}

PrivilegeInspector is implemented by introspectors that can report the privileges of their connection's account.

type SchemaLister added in v0.1.11

type SchemaLister interface {
	Schemas(ctx context.Context) (all []string, current string, err error)
}

SchemaLister is implemented by introspectors that can name the schemas of their database: all user schemas, and the default one where unqualified table names resolve ("" when there is none). Discover with no schemas scans the default one.

type TablePrivileges added in v0.1.11

type TablePrivileges struct {
	Select, Insert, Update, Delete Privilege
	// Columns lists, per action without a table-level grant, the columns
	// granted at column level ("select", "insert", "update").
	Columns map[string][]string
}

TablePrivileges are a connection's privileges on one table or view, with column-level privileges where the table-level one is not granted but some columns are.

Jump to

Keyboard shortcuts

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