Documentation
¶
Index ¶
- Constants
- Variables
- type ChangeDetector
- type ChangeLoader
- type Changes
- type DevLoader
- type DirChangeDetector
- type FileError
- type FileReport
- type GitChangeDetector
- type GitChangeDetectorOption
- type ReportWriter
- type Runner
- type SilentError
- type StepReport
- type SummaryReport
- func (r *SummaryReport) DiagnosticsCount() int
- func (r *SummaryReport) StepError(name, text string, err error) error
- func (r *SummaryReport) StepResult(name, text string, result *FileReport)
- func (r *SummaryReport) TotalChanges() int
- func (r *SummaryReport) VersionStatuses() string
- func (r *SummaryReport) WriteSchema(c *sqlclient.Client, diff *Changes)
- type TemplateWriter
Constants ¶
const ( StepIntegrityCheck = "Migration Integrity Check" StepDetectChanges = "Detect New Migration Files" StepLoadChanges = "Replay Migration Files" StepAnalyzeFile = "Analyze %s" )
A list of steps in CI report.
Variables ¶
var ( // TemplateFuncs are global functions available in templates. TemplateFuncs = cmdlog.WithColorFuncs(template.FuncMap{ "json": func(v any, args ...string) (string, error) { var ( b []byte err error ) switch len(args) { case 0: b, err = json.Marshal(v) case 1: b, err = json.MarshalIndent(v, "", args[0]) default: b, err = json.MarshalIndent(v, args[0], args[1]) } return string(b), err }, "sub": func(i, j int) int { return i - j }, "add": func(i, j int) int { return i + j }, "repeat": strings.Repeat, "join": strings.Join, "underline": func(s string) string { return color.New(color.Underline, color.Attribute(90)).Sprint(s) }, "maxWidth": func(s string, n int) []string { var ( j, k int words = strings.Fields(s) lines = make([]string, 0, len(words)) ) for i := 0; i < len(words); i++ { if k+len(words[i]) > n { lines = append(lines, strings.Join(words[j:i], " ")) k, j = 0, i } k += len(words[i]) } return append(lines, strings.Join(words[j:], " ")) }, }) // DefaultTemplate is the default template used by the CI job. DefaultTemplate = template.Must(template.New("report"). Funcs(TemplateFuncs). Parse(` {{- if .Files }} {{- $total := len .Files }}{{- with .TotalFiles }}{{- $total = . }}{{ end }} {{- $s := "s" }}{{ if eq $total 1 }}{{ $s = "" }}{{ end }} {{- if and .FromV .ToV }} {{- printf "Analyzing changes from version %s to %s (%d migration%s in total):\n" (cyan .FromV) (cyan .ToV) $total $s }} {{- else if .ToV }} {{- printf "Analyzing changes until version %s (%d migration%s in total):\n" (cyan .ToV) $total $s }} {{- else }} {{- printf "Analyzing changes (%d migration%s in total):\n" $total $s }} {{- end }} {{- println }} {{- range $i, $f := .Files }} {{- /* Replay or checksum errors. */ -}} {{- if and $f.Error (eq $f.File nil) (eq $i (sub (len $.Files) 1)) }} {{- printf " %s\n\n" (redBgWhiteFg (printf "Error: %s" $f.Error)) }} {{- break }} {{- end }} {{- $heading := printf "analyzing version %s" (cyan $f.Version) }} {{- $headinglen := len (printf "analyzing version %s" $f.Version) }} {{- println (yellow " --") $heading }} {{- if and $f.Error (not $f.Reports) }} {{- printf "Error: %s\n" $f.Name $f.Error }} {{- continue }} {{- end }} {{- range $i, $r := $f.Reports }} {{- if $r.Text }} {{- printf " %s %s:\n" (yellow "--") $r.Text }} {{- else if $r.Diagnostics }} {{- printf " %s Unnamed diagnostics detected:\n" (yellow "--") }} {{- end }} {{- range $d := $r.Diagnostics }} {{- $prefix := printf " %s L%d: " (cyan "--") ($f.Line $d.Pos) }} {{- print $prefix }} {{- $text := printf "%s %s" $d.Text (underline (print "https://atlasgo.io/lint/analyzers#" $d.Code)) }} {{- $lines := maxWidth $text (sub 85 (len $prefix)) }} {{- range $i, $line := $lines }}{{- if $i }}{{- print " " }}{{- end }}{{- println $line }}{{- end }} {{- end }} {{- else }} {{- printf " %s no diagnostics found\n" (cyan "--") }} {{- end }} {{- $fixes := $f.SuggestedFixes }} {{- if $fixes }} {{- $s := "es" }}{{- if eq (len $fixes) 1 }}{{ $s = "" }}{{ end }} {{- printf " %s suggested fix%s:\n" (yellow "--") $s }} {{- range $f := $fixes }} {{- $prefix := printf " %s " (cyan "->") }} {{- print $prefix }} {{- $lines := maxWidth $f.Message (sub 85 (len $prefix)) }} {{- range $i, $line := $lines }}{{- if $i }}{{- print " " }}{{- end }}{{- println $line }}{{- end }} {{- end }} {{- end }} {{- if or (not $f.Error) $f.Reports }} {{- printf " %s ok (%s)\n" (yellow "--") (yellow (.End.Sub .Start).String) }} {{- end }} {{- println }} {{- end }} {{- println (cyan " -------------------------") }} {{- printf " %s %s\n" (yellow "--") (.End.Sub .Start).String }} {{- with .VersionStatuses }} {{- printf " %s %s\n" (yellow "--") . }} {{- end }} {{- with .TotalChanges }} {{- $s := "s" }}{{ if eq . 1 }}{{ $s = "" }}{{ end }} {{- printf " %s %d schema change%s\n" (yellow "--") . $s }} {{- end }} {{- with .DiagnosticsCount }} {{- $s := "s" }}{{ if eq . 1 }}{{ $s = "" }}{{ end }} {{- printf " %s %d diagnostic%s\n" (yellow "--") . $s }} {{- end }} {{- end -}} `)) // JSONTemplate is the JSON template used by CI wrappers. JSONTemplate = template.Must(template.New("json"). Funcs(TemplateFuncs). Parse("{{ json . }}")) )
Functions ¶
This section is empty.
Types ¶
type ChangeDetector ¶
type ChangeDetector interface { // DetectChanges splits the files of a migration directory into the "base" files (already merged) and new ones. DetectChanges(context.Context) ([]migrate.File, []migrate.File, error) }
A ChangeDetector takes a migration directory and splits it into the "base" files (already merged) and new ones.
func LatestChanges ¶
func LatestChanges(dir migrate.Dir, n int) ChangeDetector
LatestChanges implements the ChangeDetector interface by selecting the latest N files as new. It is useful for executing analysis on files in development before they are committed or on all files in a directory.
type ChangeLoader ¶
type ChangeLoader interface { // LoadChanges converts each of the given migration files into one Changes. LoadChanges(context.Context, []migrate.File) (*Changes, error) }
A ChangeLoader takes a set of migration files and will create multiple schema.Changes out of it.
type Changes ¶
type Changes struct {
From, To *schema.Realm // Current and desired schema.
Files []*sqlcheck.File // Files for moving from current to desired state.
}
Changes holds schema changes information returned by the loader.
type DevLoader ¶
type DevLoader struct { // Dev environment used as a sandbox instantiated to the starting point (e.g. base branch). Dev *sqlclient.Client }
DevLoader implements the ChangesLoader interface using a dev-driver.
type DirChangeDetector ¶
type DirChangeDetector struct {
// Base and Head are the migration directories to compare.
// Base represents the current state, Head the desired state.
Base, Head migrate.Dir
}
A DirChangeDetector implements the ChangeDetector interface by comparing two migration directories.
func (DirChangeDetector) DetectChanges ¶
DetectChanges implements migratelint.ChangeDetector.
type FileError ¶
type FileError struct { File string Err error // Atlas or database error. Pos int // Position error, if known. }
FileError represents an error that occurred while processing a file.
type FileReport ¶
type FileReport struct { Name string `json:"Name,omitempty"` // Name of the file. Text string `json:"Text,omitempty"` // Contents of the file. Reports []sqlcheck.Report `json:"Reports,omitempty"` // List of reports. Error string `json:"Error,omitempty"` // File specific error. // Logging only info. Start time.Time `json:"-"` // Start time of the analysis. End time.Time `json:"-"` // End time of the analysis. *sqlcheck.File `json:"-"` // Underlying file. }
FileReport contains a summary of the analysis of a single file.
func NewFileReport ¶
func NewFileReport(f *sqlcheck.File) *FileReport
NewFileReport returns a new FileReport.
func (*FileReport) Line ¶
func (f *FileReport) Line(pos int) int
Line returns the line number from a position.
func (*FileReport) SuggestedFixes ¶
func (f *FileReport) SuggestedFixes() []sqlcheck.SuggestedFix
SuggestedFixes returns the list of suggested fixes for a specific report.
func (*FileReport) WriteReport ¶
func (f *FileReport) WriteReport(r sqlcheck.Report)
WriteReport implements sqlcheck.ReportWriter.
type GitChangeDetector ¶
type GitChangeDetector struct {
// contains filtered or unexported fields
}
GitChangeDetector implements the ChangeDetector interface by utilizing a git repository.
func NewGitChangeDetector ¶
func NewGitChangeDetector(dir migrate.Dir, opts ...GitChangeDetectorOption) (*GitChangeDetector, error)
NewGitChangeDetector configures a new GitChangeDetector.
func (*GitChangeDetector) DetectChanges ¶
func (d *GitChangeDetector) DetectChanges(ctx context.Context) ([]migrate.File, []migrate.File, error)
DetectChanges implements the ChangeDetector interface.
type GitChangeDetectorOption ¶
type GitChangeDetectorOption func(*GitChangeDetector) error
GitChangeDetectorOption allows configuring GitChangeDetector with functional arguments.
func WithBase ¶
func WithBase(base string) GitChangeDetectorOption
WithBase configures the git base branch name for a GitChangeDetector.
func WithMigrationsPath ¶
func WithMigrationsPath(path string) GitChangeDetectorOption
WithMigrationsPath configures the path for the migration directory.
func WithWorkDir ¶
func WithWorkDir(work string) GitChangeDetectorOption
WithWorkDir configures the git working directory for a GitChangeDetector.
type ReportWriter ¶
type ReportWriter interface {
WriteReport(*SummaryReport) error
}
ReportWriter is a type of report writer that writes a summary of analysis reports.
type Runner ¶
type Runner struct { // DevClient configures the "dev driver" to calculate // migration changes by the driver. Dev *sqlclient.Client // RunChangeDetector configures the ChangeDetector to // be used by the runner. ChangeDetector ChangeDetector // Dir is used for scanning and validating the migration directory. Dir migrate.Dir // Analyzers defines the analysis to be run in the CI job. Analyzers []sqlcheck.Analyzer // ReportWriter writes the summary report. ReportWriter ReportWriter // contains filtered or unexported fields }
Runner is used to execute CI jobs.
type SilentError ¶
type SilentError struct {
// contains filtered or unexported fields
}
SilentError is returned in case the wrapped error is already printed by the runner and should not be printed by its caller
func (SilentError) Unwrap ¶
func (err SilentError) Unwrap() error
type StepReport ¶
type StepReport struct { Name string `json:"Name,omitempty"` // Step name. Text string `json:"Text,omitempty"` // Step description. Error string `json:"Error,omitempty"` // Error that cause the execution to halt. Result *FileReport `json:"Result,omitempty"` // Result of the step. For example, a diagnostic. }
StepReport contains a summary of the analysis of a single step.
type SummaryReport ¶
type SummaryReport struct { URL string `json:"URL,omitempty"` // URL of the report, if exists. // Env holds the environment information. Env struct { Driver string `json:"Driver,omitempty"` // Driver name. URL *sqlclient.URL `json:"URL,omitempty"` // URL to dev database. Dir string `json:"Dir,omitempty"` // Path to migration directory. } // Schema versions found by the runner. Schema struct { Current string `json:"Current,omitempty"` // Current schema. Desired string `json:"Desired,omitempty"` // Desired schema. } // Steps of the analysis. Added in verbose mode. Steps []*StepReport `json:"Steps,omitempty"` // Files reports. Non-empty in case there are findings. Files []*FileReport `json:"Files,omitempty"` // Logging only info. Start time.Time `json:"-"` // Start time of the analysis. End time.Time `json:"-"` // End time of the analysis. FromV, ToV string `json:"-"` // From and to versions. TotalFiles int `json:"-"` // Total number of files to analyze. }
A SummaryReport contains a summary of the analysis of all files. It is used as an input to templates to report the CI results.
func NewSummaryReport ¶
func NewSummaryReport(c *sqlclient.Client, dir migrate.Dir) *SummaryReport
NewSummaryReport returns a new SummaryReport.
func (*SummaryReport) DiagnosticsCount ¶
func (r *SummaryReport) DiagnosticsCount() int
DiagnosticsCount returns the total number of diagnostics in the report.
func (*SummaryReport) StepError ¶
func (r *SummaryReport) StepError(name, text string, err error) error
StepError appends step error to the summary.
func (*SummaryReport) StepResult ¶
func (r *SummaryReport) StepResult(name, text string, result *FileReport)
StepResult appends step result to the summary.
func (*SummaryReport) TotalChanges ¶
func (r *SummaryReport) TotalChanges() int
TotalChanges returns the total number of changes that were analyzed.
func (*SummaryReport) VersionStatuses ¶
func (r *SummaryReport) VersionStatuses() string
VersionStatuses returns statuses description of all versions (migration files).
func (*SummaryReport) WriteSchema ¶
func (r *SummaryReport) WriteSchema(c *sqlclient.Client, diff *Changes)
WriteSchema writes the current and desired schema to the summary.
type TemplateWriter ¶
A TemplateWriter is a type of writer that writes output according to a template.
func (*TemplateWriter) WriteReport ¶
func (w *TemplateWriter) WriteReport(r *SummaryReport) error
WriteReport implements ReportWriter.