Documentation
¶
Index ¶
- Constants
- Variables
- func Apply(ctx context.Context, db *sql.DB, opts ApplyOptions) (any, error)
- func BundleFiles(files []File) (fs.FS, error)
- func Decode(data []byte, value any) error
- func TransformSource(expression, filename, content string, captures map[string]string, ...) (map[string]any, error)
- func Validate(opts ApplyOptions) error
- func Variables(values []string) (map[string]cty.Value, error)
- type ApplyOptions
- type ApplyResult
- type Change
- type ContentKind
- type File
- type Options
- type PlanResult
- type Project
- type ProjectConfig
- type ProjectFile
- type ProjectMapping
- type ProjectPreview
- type ProjectSource
- type ProjectUpload
- type RunOptions
- type RunResult
- type Source
- type Step
- type Unit
Constants ¶
const ProjectConfigName = ".sqlglot-go.yaml"
Variables ¶
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 ¶
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 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.
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
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 ¶
func ClassifyFiles ¶ added in v1.3.0
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 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 ¶
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
LoadProject resolves the configured project sources. A nil project means the filesystem has no source mappings.
func (*Project) Bundle ¶ added in v1.3.8
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
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.
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 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 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.