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 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 ¶
- func NewSchemaProvider(reader dbschema.SchemaReader, counter RecordsCounter, catalog, schema string) schemer.SchemaProvider
- func NewSchemaProviderWithErrorObserver(reader dbschema.SchemaReader, counter RecordsCounter, catalog, schema string, ...) schemer.SchemaProvider
- type RecordsCounter
- type SchemaLister
- type ViewLister
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.
Types ¶
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.
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, each by the reference that ListCollections gave it (a view is among the collections ListCollections lists). A scan reports a collection it lists as a view, and does not count its records.