dalgoschema

package
v0.144.3 Latest Latest
Warning

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

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

Documentation

Overview

Package dalgoschema is a schemer.SchemaProvider built on DALgo's dbschema.SchemaReader, so one scanner serves every database whose DALgo adapter can read its own schema. PostgreSQL is the first.

A SchemaReader describes a table by name and keeps native names and types out of reach, so what a scan can report is what the reader reports: the portable column type (a type the reader cannot map is a string), the column's default as the text of its expression, the primary key, foreign keys, unique constraints and indexes, under the exact names the reader returns.

A SchemaReader cannot tell a view from a table, so unless the reader also is a ViewLister or a SchemaLister every collection is reported as a table. A reader that is a SchemaLister (the PostgreSQL reader of dalgo2postgres) is read in every schema it lists, not in one: a collection is in the schema its reference names, or else in the schema the provider was given, which is the only one a reader that lists no schema in its references reads, so for that reader a foreign key into another schema is left out of the scan.

A provider made with Options can also say what it leaves out of a scan, and why, instead of failing it (Options.Warnings), and can report what the reader keeps of the database's own catalog (Options.SourceDefinitions): the SQLite scan uses both.

A scan opens connections to somebody's server, so the provider bounds how many reads and counts it has in flight at once (maxConcurrentReads): the core scanner starts a goroutine per table for each of them, and without a bound a schema of a few dozen tables would ask for more connections than a default PostgreSQL server grants.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewSchemaProvider

func NewSchemaProvider(reader dbschema.SchemaReader, counter RecordsCounter, catalog, schema string) schemer.SchemaProvider

NewSchemaProvider returns a schemer.SchemaProvider that reads catalog through reader. schema is the schema the reader inspects: every collection whose reference does not name another is reported in it. counter may be nil, in which case no table has a record count.

The provider is safe for the concurrent use the scanner makes of it: it reads each table's description, indexes and constraints once and shares them, and it never has more than maxConcurrentReads calls to reader and counter in flight.

func NewSchemaProviderWithErrorObserver added in v0.59.3

func NewSchemaProviderWithErrorObserver(reader dbschema.SchemaReader, counter RecordsCounter, catalog, schema string, observe func(error)) schemer.SchemaProvider

NewSchemaProviderWithErrorObserver calls observe for each reader failure before the scanner can flatten its error chain. The observer must be safe for concurrent calls.

func NewSchemaProviderWithOptions added in v0.118.0

func NewSchemaProviderWithOptions(reader dbschema.SchemaReader, counter RecordsCounter, catalog, schema string, opts Options) schemer.SchemaProvider

NewSchemaProviderWithOptions is NewSchemaProvider with opts.

Types

type Options added in v0.118.0

type Options struct {
	// Observe is called for each reader failure before the scanner can flatten its
	// error chain. It must be safe for concurrent calls. nil observes nothing.
	Observe func(error)

	// Warnings is where the provider names, one line each, what it leaves out of a
	// scan and why: a collection with no name, a view it cannot read (see
	// LeaveOutUnreadableViews), and a foreign key to a table the reader did not list.
	// nil says nothing of the first two and logs a foreign key that is left out.
	Warnings io.Writer

	// LeaveOutUnreadableViews describes every view when the collections are listed
	// and leaves out one the reader cannot describe (it names a table that was
	// dropped), so that the scan of a database with a mistake in it reads the rest.
	// Without it such a view is listed, and reading its columns fails.
	LeaveOutUnreadableViews bool

	// SourceDefinitions reports what the reader keeps of the database's own catalog
	// (dbschema.SourceDefinition) where it keeps it: a column's declared type and the
	// text of its default, and every index of a table with its origin, those the
	// portable definition cannot hold included (the index of a primary key, one on
	// an expression). Without it a column has its portable type.
	SourceDefinitions bool

	// ZeroBasedOrdinals counts a table's columns from 0, as SQLite's PRAGMA
	// table_info does and as SQLite projects were scanned, instead of from 1, as
	// information_schema does.
	ZeroBasedOrdinals bool
}

Options are what a provider does beyond reading what the reader reports.

type RecordsCounter

type RecordsCounter interface {
	CountRecords(ctx context.Context, schema, table string) (*int, error)
}

RecordsCounter reads the number of records of one table. A nil count with a nil error means the count is not available, which is not the same as zero.

func NewNativeCounter

func NewNativeCounter(db dal.DB) RecordsCounter

NewNativeCounter counts a table's records with one COUNT(*) structured query, and only when db runs that aggregate natively.

A database that does not advertise a native COUNT would have DALgo read every row of the table through this process and count them here. A schema scan must never do that to someone's database, so such a table has no count: the count is unknown, and it appears on its own once the database's adapter declares the aggregate.

func NewNativeCounterWithCapabilities added in v0.80.0

func NewNativeCounterWithCapabilities(db dal.DB, capabilities dal.QueryCapabilities) RecordsCounter

NewNativeCounterWithCapabilities is NewNativeCounter for a database whose adapter runs COUNT(*) natively without declaring it (dalgo2sqlite): the caller states the capabilities instead of the adapter.

type SchemaLister added in v0.57.0

type SchemaLister interface {
	ListSchemas(ctx context.Context) ([]string, error)
	ListSchemaCollections(ctx context.Context, schema string) ([]dal.CollectionRef, error)
	ListSchemaViews(ctx context.Context, schema string) ([]dal.CollectionRef, error)
}

SchemaLister is the optional capability of a reader that holds more than one schema: it lists the schemas, and the collections and the views of each, every reference naming the schema it is in. dbschema has no interface for it, so a reader declares these methods as its own: dalgo2postgres does. A scan reads every schema such a reader lists, in the order it lists them, and a collection it lists as a view is reported as a view.

type ViewLister added in v0.55.0

type ViewLister interface {
	ListViews(ctx context.Context) ([]dal.CollectionRef, error)
}

ViewLister is the optional capability of a reader that can tell a view from a table: it lists the collections that are views. A reader may list them among its collections too (ListCollections), each by the same reference, or apart from them (dalgo2sqlite lists tables only), in which case they are added after them. A scan reports a collection it lists as a view, and does not count its records.

Jump to

Keyboard shortcuts

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