workflow

package
v1.6.3 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: Apache-2.0 Imports: 38 Imported by: 1 module, 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

Index

Constants

View Source
const ProjectConfigName = ".sqlglot-go.yaml"

Variables

View Source
var ErrOlderVersion = errors.New("the plan file was made by an older version of sqlglot")

ErrOlderVersion marks a plan file in a format sqlglot no longer reads. Such files are never converted: the plan is made again against the live schema.

Functions

func Apply

func Apply(ctx context.Context, db *sql.DB, opts ApplyOptions) (any, error)

Apply validates a plan file and applies it. A schema artifact runs unit by unit, each in its own transaction where the dialect has them, so a failed run keeps the units before it and a second apply resumes after them.

func BundleFiles

func BundleFiles(files []File) (fs.FS, error)

func Decode

func Decode(data []byte, value any) error

func TransformSource added in v1.3.7

func TransformSource(expression, filename, content string, captures map[string]string, read func(string) (string, error)) (map[string]any, error)

TransformSource evaluates a project source expression with the shared file and XML helpers. The caller confines reads to the selected source tree.

func Validate

func Validate(opts ApplyOptions) error

Validate checks a plan file's integrity and acknowledgements without opening a database. Each artifact carries its own hash, which guards only that the file is what its planner wrote.

func Variables

func Variables(values []string) (map[string]cty.Value, error)

Types

type ApplyOptions

type ApplyOptions struct {
	Dialect     string
	Schema      string
	Artifact    json.RawMessage
	Acks        []string
	LockTimeout time.Duration
	CSV         csvdata.Options
}

ApplyOptions applies an artifact as reviewed: its own hash guards its integrity, and Acks acknowledge its destructive changes.

type ApplyResult added in v1.3.8

type ApplyResult struct {
	Hash    string              `json:"hash"`
	Outcome engine.Outcome      `json:"outcome"`
	Steps   int                 `json:"steps"`
	Units   []engine.UnitResult `json:"units,omitempty"`
}

ApplyResult is the outcome of applying a schema artifact: Steps counts the statements that ran, Units reports each unit's own outcome.

type Change added in v1.3.8

type Change struct {
	migrate.Change
	Rebuilds []migrate.Change `json:"rebuilds,omitempty"`
}

Change is a planned change with the rebuilds it forces: the dependents (an index, key, check, default, statistic or view) dropped and re-created around it, each named by its own change with Cause set to this one.

type ContentKind added in v1.3.0

type ContentKind string

ContentKind is what a bundle file holds: SQL scripts or HCL schema.

const (
	SQLContent ContentKind = "sql"
	HCLContent ContentKind = "hcl"
)

func DetectContent added in v1.3.0

func DetectContent(name, content string) (ContentKind, bool)

DetectContent is the kind of the file name holding content. A .sql or .hcl name is that kind whatever it holds, so its loader reports what is wrong with it. Any other file is HCL when it parses as an HCL body declaring something, SQL when its first word past comments opens a SQL statement, and neither otherwise.

type File

type File struct {
	Path    string `json:"path"`
	Content string `json:"content"`
}

func ClassifyFiles added in v1.3.0

func ClassifyFiles(files []File) ([]File, error)

ClassifyFiles names every file by the kind DetectContent finds, since the bundle loaders select files by extension: a .sql or .hcl path is kept, any other path's extension becomes the kind's. A file of neither kind, or two files named alike once classified, is an error.

type Options

type Options struct {
	Dialect string
	Schema  string
	Source  Source
	Bundle  fs.FS
	Project *Project
	// Labels select the project sources to plan (see Project.Select); they replace
	// the select of .sqlglot-go.yaml.
	Labels     []string
	Variables  []string
	Routines   string
	AllowDrops bool
	CSV        csvdata.Options
}

type PlanResult

type PlanResult struct {
	Dialect  string          `json:"dialect"`
	Schema   string          `json:"schema"`
	Artifact json.RawMessage `json:"artifact"`
	Hash     string          `json:"hash"`
	// Acks are the IDs of the destructive changes the plan carries, each of which apply must acknowledge.
	Acks     []string `json:"acks"`
	Steps    []Step   `json:"steps"`
	Changes  []Change `json:"changes"`
	Warnings []string `json:"warnings"`
	// Applicable is false for an advisory plan, which can be reviewed but not applied.
	Applicable bool `json:"applicable"`
	// Advisory lists why the plan is not applicable. A saved plan file must
	// carry it, or re-importing the file would make the plan applicable.
	Advisory []string        `json:"advisory,omitempty"`
	SQL      string          `json:"sql"`
	Sources  []ProjectSource `json:"sources,omitempty"`
	// Scope, Units, Preserved and Refusals describe a schema plan: what it manages,
	// how it is grouped into transactions, the drops its declared scope leaves
	// alone, and the objects inspection refused that no change touches.
	Scope     *engine.Scope     `json:"scope,omitempty"`
	Units     []Unit            `json:"units,omitempty"`
	Preserved []migrate.Change  `json:"preserved,omitempty"`
	Refusals  []engine.Refusal  `json:"refusals,omitempty"`
	CSVPlan   *csvdata.Artifact `json:"csvPlan,omitempty"`
	// ScriptPlan is the plan file of a SQL Server script bundle.
	ScriptPlan *scriptplan.PlanFile `json:"scriptPlan,omitempty"`
	Original   json.RawMessage      `json:"-"`
}

func Plan

func Plan(ctx context.Context, db *sql.DB, opts Options) (PlanResult, error)

Plan plans one migration source against the target and returns its review. A schema source (hcl or snapshot) plans in complete scope: every object of the target's schema not in the source is dropped, and each drop must be acknowledged to apply. A bundle plans in declared scope unless AllowDrops.

func Review

func Review(data []byte, dialect, schema string) (PlanResult, error)

Review reads a plan file and reports what applying it does. It opens no database: a schema artifact is checked against its own hash and re-planned from its own snapshots, so an edited statement, unit or change is refused.

type Project added in v1.3.3

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

Project is the set of sources a config maps. Its files are the ones its selectors keep; all holds every mapped file so an explicit Select replaces the config's default selection instead of narrowing it.

func LoadProject added in v1.3.3

func LoadProject(fsys fs.FS) (*Project, error)

LoadProject resolves the configured project sources. A nil project means the filesystem has no source mappings.

func (*Project) Bundle added in v1.3.8

func (project *Project) Bundle() (fs.FS, map[string]scripts.Source, []ProjectSource, error)

Bundle assembles the selected sources into a bundle filesystem, the script metadata keyed by bundle path, and the source inventory.

func (*Project) Files added in v1.3.8

func (project *Project) Files() []ProjectFile

Files lists the selected sources in path order. The result is a copy.

func (*Project) Preview added in v1.3.3

func (project *Project) Preview() ProjectPreview

func (*Project) Select added in v1.3.8

func (project *Project) Select(selectors []string) (*Project, error)

Select returns the project narrowed to the sources whose labels satisfy the selectors, which replace the config's default select entirely. No selectors returns the project as it is. Selecting nothing is an error.

type ProjectConfig added in v1.3.3

type ProjectConfig struct {
	Version  int              `yaml:"version"`
	Mappings []ProjectMapping `yaml:"mappings"`
	// Select is the default label selector, used when no explicit selectors are given.
	Select      []string          `yaml:"select"`
	Ignore      []string          `yaml:"ignore"`
	Connections map[string]string `yaml:"connections" json:"-"`
}

func ParseProjectConfig added in v1.3.8

func ParseProjectConfig(data []byte) (ProjectConfig, error)

ParseProjectConfig decodes and validates a .sqlglot-go.yaml document.

func ReadProjectConfig added in v1.3.3

func ReadProjectConfig(fsys fs.FS) (*ProjectConfig, error)

ReadProjectConfig reads a YAML config without opening migration files or targets.

func (ProjectConfig) Load added in v1.3.3

func (cfg ProjectConfig) Load(fsys fs.FS) (*Project, error)

Load maps the files of fsys. The config's select is applied as the default selection: Files and Bundle see only the sources it keeps, and Select can replace it.

type ProjectFile added in v1.3.8

type ProjectFile struct {
	ProjectSource
	BundlePath string
	Content    string
	// Inputs are the project-relative paths whose bytes went into Content: the
	// mapped file first, then each file its transform read, sorted and listed once.
	Inputs []string `json:"inputs,omitempty"`
}

ProjectFile is a mapped source with the path it takes in the bundle and the content the bundle holds (a transform's result, not the file's bytes).

type ProjectMapping added in v1.3.8

type ProjectMapping struct {
	Match     string              `yaml:"match"`
	Kind      ContentKind         `yaml:"kind"`
	Labels    map[string][]string `yaml:"labels"`
	Transform string              `yaml:"transform"`
}

type ProjectPreview added in v1.3.3

type ProjectPreview struct {
	Mapped     int `json:"mapped"`
	Ignored    int `json:"ignored"`
	Deselected int `json:"deselected,omitempty"`
}

ProjectPreview counts the sources the selectors keep (Mapped), the matched files an ignore rule dropped, and the mapped sources the selectors left out.

func PreviewProject added in v1.3.3

func PreviewProject(files []File) (ProjectPreview, error)

PreviewProject validates uploaded files without opening a database.

type ProjectSource added in v1.3.3

type ProjectSource struct {
	Path   string              `json:"path"`
	Kind   ContentKind         `json:"kind"`
	ID     string              `json:"id"`
	Name   string              `json:"name,omitempty"`
	Phase  string              `json:"phase,omitempty"`
	Labels map[string][]string `json:"labels,omitempty"`
}

type ProjectUpload added in v1.3.3

type ProjectUpload struct {
	Required []string `json:"required"`
	Plain    bool     `json:"plain,omitempty"`
}

func ProjectUploadPaths added in v1.3.3

func ProjectUploadPaths(config string, paths []string) (ProjectUpload, error)

ProjectUploadPaths finds the files needed to run the same mapper used by the CLI. A transform may read files under the matched file's directory.

type RunOptions

type RunOptions struct {
	Mode            string
	Target          dbtarget.OpenOptions
	Plan            Options
	Acks            []string
	ArtifactContent json.RawMessage
	LockTimeout     string
}

type RunResult

type RunResult struct {
	Plan     *PlanResult `json:"plan,omitempty"`
	Mode     string      `json:"mode"`
	Hash     string      `json:"hash,omitempty"`
	Acks     []string    `json:"acks,omitempty"`
	Artifact any         `json:"artifact,omitempty"`
	Result   any         `json:"result,omitempty"`
}

func Execute

func Execute(ctx context.Context, opts RunOptions, record *history.Record, t *task.Task) (payload RunResult, returnErr error)

type Source

type Source struct {
	Kind    string `json:"kind"`
	Content string `json:"content,omitempty"`
	Files   []File `json:"files,omitempty"`
}

type Step

type Step struct {
	ID          string `json:"id"`
	SQL         string `json:"sql"`
	Destructive bool   `json:"destructive"`
	// Change is the planned change the statement implements, Unit the unit that
	// runs it and Phase its place in that change (drop, alter or create).
	Change           string `json:"change,omitempty"`
	Unit             string `json:"unit,omitempty"`
	Phase            string `json:"phase,omitempty"`
	NonTransactional bool   `json:"nonTransactional,omitempty"`
}

Step is one statement of the plan, in execution order.

type Unit added in v1.3.8

type Unit struct {
	ID            string                `json:"id"`
	Changes       []string              `json:"changes"`
	Objects       []migrate.Name        `json:"objects"`
	Transactional bool                  `json:"transactional"`
	Preconditions []engine.Precondition `json:"preconditions,omitempty"`
	// Steps are the IDs of the statements the unit runs, in order.
	Steps []string `json:"steps"`
}

Unit is one transaction of the plan, with the data checks it runs first.

Jump to

Keyboard shortcuts

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