catalog

package
v0.0.0-...-cdc0bb7 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2026 License: MIT Imports: 22 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package catalog renders CockroachDB schema definitions from a synced metadata snapshot and generates the migration between two snapshots.

It began as a copy of the parts of pg/catalog that render definitions, and differs where CockroachDB does. A primary key is part of CREATE TABLE, because CockroachDB gives a table created without one a hidden rowid key. A secondary index renders from its synced definition through CockroachDB's own formatter, which keeps STORING, hash sharding, inverted and partial indexes. The columns CockroachDB adds itself (the hidden rowid, a hash-sharded index's shard column) are left out. CockroachDB stores the relations of a view, an index and a SQL routine body under their database's name, which the rendering drops so a definition replays on a database of any name. CockroachDB holds no comment on a view, materialized view, sequence or routine, so none is written.

CockroachDB's syntax is parsed with its own parser, never pg's.

Index

Constants

This section is empty.

Variables

View Source
var Engine engine

Engine is CockroachDB's metadata migrator. It lives here rather than in cockroachdb/analysis because this package imports that one.

Functions

func DatabaseDefinition

func DatabaseDefinition(metadata *metadatapb.DatabaseSchemaMetadata, opts DefinitionOptions) (string, error)

DatabaseDefinition renders a synced database snapshot as a restore script, or with opts.SDLFormat as SDL.

func FunctionDefinition

func FunctionDefinition(schema string, function *metadatapb.FunctionMetadata) (string, error)

FunctionDefinition renders one function or procedure.

func LoadSDL

LoadSDL reads a schema written as CockroachDB DDL, such as DatabaseDefinition renders, into the metadata a sync would state for it. Every type, default, check and index key passes through CockroachDB's formatter, so SDL written by hand and SDL rendered from a sync load alike where CockroachDB would store them alike. Session statements (SET, SELECT set_config) and owner changes are skipped; any other statement LoadSDL does not model is an error.

func MaterializedViewDefinition

func MaterializedViewDefinition(schema string, view *metadatapb.MaterializedViewMetadata) (string, error)

MaterializedViewDefinition renders one materialized view and its indexes.

func MetadataMigration

func MetadataMigration(oldMeta, newMeta *metadatapb.DatabaseSchemaMetadata, opts MetadataMigrationOptions) (string, error)

MetadataMigration generates the migration from one synced CockroachDB snapshot to another.

func SchemaDefinition

func SchemaDefinition(schema *metadatapb.SchemaMetadata) (string, error)

SchemaDefinition renders one schema and the objects in it.

func SequenceDefinition

func SequenceDefinition(schema string, sequence *metadatapb.SequenceMetadata) (string, error)

SequenceDefinition renders one sequence.

func TableDefinition

func TableDefinition(schema string, table *metadatapb.TableMetadata, sequences []*metadatapb.SequenceMetadata) (string, error)

TableDefinition renders one table, its indexes and its foreign keys.

func ViewDefinition

func ViewDefinition(schema string, view *metadatapb.ViewMetadata) (string, error)

ViewDefinition renders one view.

Types

type Catalog

type Catalog struct {
	// contains filtered or unexported fields
}

Catalog is one database's schema, changed by DDL as CockroachDB changes it. It starts with the schema public, as a new database does.

Exec accepts by default. It refuses a statement with CockroachDB's SQLSTATE where TestSimulation's rows, or a check of the pinned server, show CockroachDB refusing it: an object missing, taken or still needed by another. A statement whose effect on other objects the metadata cannot hold is refused with an Error that carries no code. It does not check what a statement's expressions compute.

func New

func New() *Catalog

New returns the catalog of an empty database.

func (*Catalog) Exec

func (c *Catalog) Exec(sql string) ([]ExecResult, error)

Exec runs a script on the catalog, one statement at a time, and stops at the first statement CockroachDB would refuse, which changes nothing. It returns a syntax error, and changes nothing, when the script does not parse.

func (*Catalog) Metadata

func (c *Catalog) Metadata() *metadatapb.DatabaseSchemaMetadata

Metadata is what the catalog holds, as a sync of the database would state it.

type DefinitionFile

type DefinitionFile struct {
	// Name is the file's path, such as "schemas/public/tables/t.sql".
	Name string
	// Content is the file's SDL.
	Content string
}

DefinitionFile is one file of MultiFileDefinition's output.

func MultiFileDefinition

func MultiFileDefinition(metadata *metadatapb.DatabaseSchemaMetadata) ([]DefinitionFile, error)

MultiFileDefinition renders a synced database snapshot as SDL split into one file per object under schemas/<schema>/. It returns an empty, non-nil slice for a snapshot without schemas, and nil when the schemas render no files.

type DefinitionOptions

type DefinitionOptions struct {
	// PrintHeader prefixes the restore script with session settings and sets
	// the default tablespace before the first table.
	PrintHeader bool
	// SDLFormat renders the declarative form instead of the restore script.
	// It ignores PrintHeader.
	SDLFormat bool
}

DefinitionOptions selects the form DatabaseDefinition renders.

type Error

type Error struct {
	Code    string
	Message string
}

Error is a statement CockroachDB refuses. Code is the SQLSTATE CockroachDB refuses it with, or empty for a form the catalog does not model.

func (*Error) Error

func (e *Error) Error() string

type ExecResult

type ExecResult struct {
	SQL   string
	Line  int
	Error error
}

ExecResult is one statement Exec ran: its text, the 1-based line it starts on, and the error CockroachDB would refuse it with, or nil.

type MetadataMigrationOptions

type MetadataMigrationOptions struct {
	// SkipSchemas lists schema names left out of the migration, such as a
	// backup schema.
	SkipSchemas []string
}

MetadataMigrationOptions configures MetadataMigration.

type MigrationOp

type MigrationOp struct {
	Type         MigrationOpType
	SchemaName   string
	ObjectName   string
	ParentObject string
	SQL          string
}

MigrationOp is one statement of a migration. ParentObject names the table of a column, constraint or index.

type MigrationOpType

type MigrationOpType string

MigrationOpType classifies one statement of a migration.

const (
	OpCreateSchema    MigrationOpType = "CreateSchema"
	OpDropSchema      MigrationOpType = "DropSchema"
	OpCreateExtension MigrationOpType = "CreateExtension"
	OpDropExtension   MigrationOpType = "DropExtension"
	OpCreateType      MigrationOpType = "CreateType"
	OpDropType        MigrationOpType = "DropType"
	OpAlterType       MigrationOpType = "AlterType"
	OpCreateSequence  MigrationOpType = "CreateSequence"
	OpDropSequence    MigrationOpType = "DropSequence"
	OpAlterSequence   MigrationOpType = "AlterSequence"
	OpCreateTable     MigrationOpType = "CreateTable"
	OpDropTable       MigrationOpType = "DropTable"
	OpAddColumn       MigrationOpType = "AddColumn"
	OpDropColumn      MigrationOpType = "DropColumn"
	OpAlterColumn     MigrationOpType = "AlterColumn"
	OpAlterTable      MigrationOpType = "AlterTable"
	OpAddConstraint   MigrationOpType = "AddConstraint"
	OpDropConstraint  MigrationOpType = "DropConstraint"
	OpCreateIndex     MigrationOpType = "CreateIndex"
	OpDropIndex       MigrationOpType = "DropIndex"
	OpCreateView      MigrationOpType = "CreateView"
	OpDropView        MigrationOpType = "DropView"
	OpCreateFunction  MigrationOpType = "CreateFunction"
	OpDropFunction    MigrationOpType = "DropFunction"
	OpAlterFunction   MigrationOpType = "AlterFunction"
	OpComment         MigrationOpType = "Comment"
)

The statements a migration holds.

type MigrationPlan

type MigrationPlan struct {
	Ops []MigrationOp
}

MigrationPlan is a migration's statements in the order they run.

func BuildSDLMigrationPlan

func BuildSDLMigrationPlan(sourceSDL, targetSDL string, opts SDLMigrationOptions) (*MigrationPlan, error)

BuildSDLMigrationPlan loads two SDL texts with LoadSDL and returns the migration from the first to the second, or nil when they hold the same schema.

func MetadataMigrationPlan

func MetadataMigrationPlan(oldMeta, newMeta *metadatapb.DatabaseSchemaMetadata, opts MetadataMigrationOptions) (*MigrationPlan, error)

MetadataMigrationPlan is MetadataMigration's plan. It compares the two snapshots object by object, each in the form the definitions render, so two databases of different names compare equal where their objects do.

The statements run in phases: foreign keys, the views a change rebuilds and the indexes and checks that change are dropped first; then removed tables, columns, routines, sequences, types and schemas; then new schemas, types and sequences, the columns of kept tables, and new tables, views and routines in dependency order; then indexes, checks, foreign keys and comments.

func (*MigrationPlan) SQL

func (p *MigrationPlan) SQL() string

SQL returns the statements joined with ";\n".

type SDLMigrationOptions

type SDLMigrationOptions struct {
	// SkipSchemas lists schema names left out of the migration, such as a
	// backup schema.
	SkipSchemas []string
}

SDLMigrationOptions configures BuildSDLMigrationPlan.

Jump to

Keyboard shortcuts

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