schema

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2026 License: AGPL-3.0, AGPL-3.0-or-later Imports: 8 Imported by: 0 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 schema manages database schema migration. It compares the DocType registry against the live database schema and generates/applies DDL to make them match.

Activation plans the DDL for a config snapshot by diffing it against the live schema; a stored change_list from doctype/dialect_macros.go only adds the explicit field and table removals the live diff cannot infer.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplyDDL

func ApplyDDL(db *sql.DB, statements []string, dialect db.Dialect) error

ApplyDDL executes the DDL statements against the database. Uses the dialect's ExecuteBatch for atomic execution (single HTTP round-trip for LibSQL, single transaction for MySQL/SQLite).

func ApplySnapshotActivationDDL

func ApplySnapshotActivationDDL(ctx context.Context, database *sql.DB, dialect db.Dialect, statements []string) error

ApplySnapshotActivationDDL executes an already-journaled plan using the supported dialect's DDL semantics. MySQL statements are intentionally applied one by one because DDL may commit independently; the durable caller must re-plan from live schema after interruption. LibSQL uses its batch transport so its supported multi-statement transaction semantics remain intact.

func GenerateSnapshotActivationDDL

func GenerateSnapshotActivationDDL(snapshot *doctype.ConfigSnapshot, getDocType func(string) *doctype.DocType, liveSchema map[string]*TableInfo, dialect db.Dialect, changeList, revisionID string) ([]string, error)

GenerateSnapshotActivationDDL derives the ordinary live-schema diff and preserves explicit historical field removals/type changes from a config version's change list. It is pure so both the HTTP compatibility route and the durable management executor can use exactly the same DDL plan.

func LoadLiveSchema

func LoadLiveSchema(database *sql.DB, dbName string, dialect db.Dialect) (map[string]*TableInfo, error)

LoadLiveSchema reads the current database schema using the dialect. Delegates to db.Dialect.LoadSchema and converts to the local TableInfo format.

func MigrateSite

func MigrateSite(database *sql.DB, dbName string, newDoctypes []*doctype.DocType, existingDoctypes *doctype.Registry, dialect db.Dialect) error

MigrateSite computes the schema diff for a site and applies it. newDoctypes is the target config (from the snapshot being activated). existingDoctypes is used for child-table lookups (finding child DocTypes by name). The registry itself is NOT mutated — callers update it after successful migration.

func MigrateSiteFromRegistry

func MigrateSiteFromRegistry(database *sql.DB, dbName string, registry *doctype.Registry, dialect db.Dialect) error

MigrateSiteFromRegistry is a backward-compat wrapper. Prefer calling MigrateSite directly with doctype slices.

func PlanSnapshotActivation

func PlanSnapshotActivation(database *sql.DB, dbName string, dialect db.Dialect, registry *doctype.Registry, snapshot *doctype.ConfigSnapshot, changeList, revisionID string) ([]string, error)

PlanSnapshotActivation loads the live tenant schema and computes the DDL needed before a full configuration snapshot can be committed. The returned statements must be durably journaled before execution because DDL is not transactional on every supported dialect.

Types

type ActivationPreview

type ActivationPreview struct {
	DDL      []string       `json:"ddl"`
	Changes  []TieredChange `json:"changes"`
	Blocked  []TieredChange `json:"blocked"`
	Warnings []TieredChange `json:"warnings"`
}

ActivationPreview contains the full impact analysis for activating a config change.

func AnalyzeImpact

func AnalyzeImpact(database *sql.DB, oldDT, newDT *doctype.DocType, reg *doctype.Registry, dialect db.Dialect) *ActivationPreview

AnalyzeImpact compares a proposed doctype against the existing one (if any) and classifies each change into safety tiers. It also counts affected rows.

type ColumnAdd

type ColumnAdd struct {
	Name     string
	Type     string
	Nullable bool
	Default  string
}

ColumnAdd describes a column to be added to an existing table.

type ColumnInfo

type ColumnInfo struct {
	Name             string
	Type             string
	Nullable         bool
	Default          sql.NullString
	Indexed          bool
	IndexNames       []string // names of indexes covering this column (for old→new name migration)
	UniqueIndexNames []string
}

ColumnInfo represents a column in the live database schema.

type ColumnRename

type ColumnRename struct {
	OldName string
	NewName string
}

ColumnRename describes a column to be renamed (from renamed_from).

type Diff

type Diff struct {
	NewTables     []string                  // Tables to CREATE
	NewColumns    map[string][]ColumnAdd    // Table → columns to ADD
	NewIndexes    map[string][]IndexAdd     // Table → indexes to CREATE
	DropIndexes   []IndexDrop               // Indexes to DROP (old-style names)
	RenameColumns map[string][]ColumnRename // Table → columns to RENAME
	Orphaned      []OrphanedColumn          // Columns in DB but not in registry
}

Diff represents the difference between the registry and the live schema.

func ComputeDiff

func ComputeDiff(doctypes []*doctype.DocType, getDocType func(string) *doctype.DocType, liveSchema map[string]*TableInfo, dialect db.SchemaDialect) *Diff

ComputeDiff compares a set of DocTypes against the live database schema and produces a Diff of changes needed. doctypes is the target config (from the snapshot being activated). getDocType looks up a DocType by name (for child table resolution).

func ComputeDiffFromRegistry

func ComputeDiffFromRegistry(registry *doctype.Registry, liveSchema map[string]*TableInfo, dialect db.SchemaDialect) *Diff

ComputeDiffFromRegistry is a backward-compat wrapper. Prefer calling ComputeDiff directly with doctype slices.

func (*Diff) GenerateDDL

func (d *Diff) GenerateDDL(doctypes []*doctype.DocType, getDocType func(string) *doctype.DocType, dialect db.Dialect) []string

GenerateDDL produces the SQL statements to apply the diff. doctypes is the target config, getDocType looks up child doctypes by name.

func (*Diff) GenerateDDLFromRegistry

func (d *Diff) GenerateDDLFromRegistry(registry *doctype.Registry, dialect db.Dialect) []string

GenerateDDLFromRegistry is a backward-compat wrapper. Prefer calling GenerateDDL directly with doctype slices.

func (*Diff) IsEmpty

func (d *Diff) IsEmpty() bool

IsEmpty returns true if there are no changes to apply.

type IndexAdd

type IndexAdd struct {
	Table   string
	Columns []string
	Unique  bool
	Search  bool
}

IndexAdd describes an index to be created.

type IndexDrop

type IndexDrop struct {
	Table string
	Name  string
}

IndexDrop describes an index to be dropped.

type OrphanedColumn

type OrphanedColumn struct {
	Table  string
	Column string
}

OrphanedColumn is a column that exists in the database but not in the registry.

type TableInfo

type TableInfo struct {
	Name    string
	Columns map[string]*ColumnInfo
}

TableInfo represents a table in the live database schema.

type TieredChange

type TieredChange struct {
	Tier    string `json:"tier"` // "safe", "warning", "blocked"
	DocType string `json:"doctype"`
	Field   string `json:"field,omitempty"`
	Change  string `json:"change"`
	Rows    int    `json:"rows"`
	DDL     string `json:"ddl,omitempty"`
	Message string `json:"message"`
}

TieredChange describes a single schema change with its safety classification.

Jump to

Keyboard shortcuts

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