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 ¶
- func ApplyDDL(db *sql.DB, statements []string, dialect db.Dialect) error
- func ApplySnapshotActivationDDL(ctx context.Context, database *sql.DB, dialect db.Dialect, statements []string) error
- func GenerateSnapshotActivationDDL(snapshot *doctype.ConfigSnapshot, getDocType func(string) *doctype.DocType, ...) ([]string, error)
- func LoadLiveSchema(database *sql.DB, dbName string, dialect db.Dialect) (map[string]*TableInfo, error)
- func MigrateSite(database *sql.DB, dbName string, newDoctypes []*doctype.DocType, ...) error
- func MigrateSiteFromRegistry(database *sql.DB, dbName string, registry *doctype.Registry, ...) error
- func PlanSnapshotActivation(database *sql.DB, dbName string, dialect db.Dialect, ...) ([]string, error)
- type ActivationPreview
- type ColumnAdd
- type ColumnInfo
- type ColumnRename
- type Diff
- type IndexAdd
- type IndexDrop
- type OrphanedColumn
- type TableInfo
- type TieredChange
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ApplyDDL ¶
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 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 ¶
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 ¶
GenerateDDLFromRegistry is a backward-compat wrapper. Prefer calling GenerateDDL directly with doctype slices.
type OrphanedColumn ¶
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.