cmdlog

package
v0.13.1 Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2023 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ColorCyan         = color.CyanString
	ColorGreen        = color.HiGreenString
	ColorRed          = color.HiRedString
	ColorRedBgWhiteFg = color.New(color.FgHiWhite, color.BgHiRed).SprintFunc()
	ColorYellow       = color.YellowString
	// ColorTemplateFuncs are globally available functions to color strings in a report template.
	ColorTemplateFuncs = template.FuncMap{
		"cyan":         ColorCyan,
		"green":        ColorGreen,
		"red":          ColorRed,
		"redBgWhiteFg": ColorRedBgWhiteFg,
		"yellow":       ColorYellow,
	}
)
View Source
var (
	// StatusTemplateFuncs are global functions available in status report templates.
	StatusTemplateFuncs = merge(template.FuncMap{
		"json":       jsonEncode,
		"json_merge": jsonMerge,
		"table":      table,
		"default": func(report *MigrateStatus) (string, error) {
			var buf bytes.Buffer
			t, err := template.New("report").Funcs(ColorTemplateFuncs).Parse(`Migration Status:
{{- if eq .Status "OK"      }} {{ green .Status }}{{ end }}
{{- if eq .Status "PENDING" }} {{ yellow .Status }}{{ end }}
  {{ yellow "--" }} Current Version: {{ cyan .Current }}
{{- if gt .Total 0 }}{{ printf " (%s statements applied)" (yellow "%d" .Count) }}{{ end }}
  {{ yellow "--" }} Next Version:    {{ cyan .Next }}
{{- if gt .Total 0 }}{{ printf " (%s statements left)" (yellow "%d" .Left) }}{{ end }}
  {{ yellow "--" }} Executed Files:  {{ len .Applied }}{{ if gt .Total 0 }} (last one partially){{ end }}
  {{ yellow "--" }} Pending Files:   {{ len .Pending }}
{{ if gt .Total 0 }}
Last migration attempt had errors:
  {{ yellow "--" }} SQL:   {{ .SQL }}
  {{ yellow "--" }} {{ red "ERROR:" }} {{ .Error }}
{{ end }}`)
			if err != nil {
				return "", err
			}
			err = t.Execute(&buf, report)
			return buf.String(), err
		},
	}, ColorTemplateFuncs)

	// MigrateStatusTemplate holds the default template of the 'migrate status' command.
	MigrateStatusTemplate = template.Must(template.New("report").Funcs(StatusTemplateFuncs).Parse("{{ default . }}"))
)
View Source
var (
	// ApplyTemplateFuncs are global functions available in apply report templates.
	ApplyTemplateFuncs = merge(ColorTemplateFuncs, template.FuncMap{
		"dec":        dec,
		"upper":      strings.ToUpper,
		"json":       jsonEncode,
		"json_merge": jsonMerge,
	})

	// MigrateApplyTemplate holds the default template of the 'migrate apply' command.
	MigrateApplyTemplate = template.Must(template.
							New("report").
							Funcs(ApplyTemplateFuncs).
							Parse(`{{- if not .Pending -}}
No migration files to execute
{{- else -}}
Migrating to version {{ cyan .Target }}{{ with .Current }} from {{ cyan . }}{{ end }} ({{ len .Pending }} migrations in total):
{{ range $i, $f := .Applied }}
  {{ yellow "--" }} migrating version {{ cyan $f.File.Version }}{{ range $f.Applied }}
    {{ cyan "->" }} {{ . }}{{ end }}
  {{- with .Error }}
    {{ redBgWhiteFg .Text }}
  {{- else }}
  {{ yellow "--" }} ok ({{ yellow (.End.Sub .Start).String }})
  {{- end }}
{{ end }}
  {{ cyan "-------------------------" }}
  {{ yellow "--" }} {{ .End.Sub .Start }}
{{- $files := len .Applied }}
{{- $stmts := .CountStmts }}
{{- if .Error }}
  {{ yellow "--" }} {{ dec $files }} migrations ok (1 with errors)
  {{ yellow "--" }} {{ dec $stmts }} sql statements ok (1 with errors)
{{- else }}
  {{ yellow "--" }} {{ len .Applied }} migrations 
  {{ yellow "--" }} {{ .CountStmts  }} sql statements
{{- end }}
{{- end }}
`))
)
View Source
var (
	// InspectTemplateFuncs are global functions available in inspect report templates.
	InspectTemplateFuncs = template.FuncMap{
		"sql":  sqlInspect,
		"json": jsonEncode,
	}

	// SchemaInspectTemplate holds the default template of the 'schema inspect' command.
	SchemaInspectTemplate = template.Must(template.New("inspect").
							Funcs(InspectTemplateFuncs).
							Parse(`{{ $.MarshalHCL }}`))
)
View Source
var (
	// SchemaDiffFuncs are global functions available in diff report templates.
	SchemaDiffFuncs = template.FuncMap{
		"sql": sqlDiff,
	}
	// SchemaDiffTemplate holds the default template of the 'schema diff' command.
	SchemaDiffTemplate = template.Must(template.
						New("schema_diff").
						Funcs(SchemaDiffFuncs).
						Parse(`{{- with .Changes -}}
{{ sql $ }}
{{- else -}}
Schemas are synced, no changes to be made.
{{ end -}}
`))
)
View Source
var MigrateSetTemplate = template.Must(template.New("set").
	Funcs(ColorTemplateFuncs).Parse(`
{{- if and (not .Current) .Revisions -}}
All revisions deleted ({{ len .Revisions }} in total):
{{ else if and .Current .Revisions -}}
Current version is {{ cyan .Current.Version }} ({{ .Summary }}):
{{ end }}
{{- if .Revisions }}
{{ range .ByVersion }}
  {{- $text := .ColoredVersion }}{{ with .Description }}{{ $text = printf "%s (%s)" $text . }}{{ end }}
  {{- printf "  %s\n" $text }}
{{- end }}
{{ end -}}
`))

MigrateSetTemplate holds the default template of the 'migrate set' command.

View Source
var SchemaPlanTemplate = template.Must(template.
	New("plan").
	Funcs(ApplyTemplateFuncs).
	Parse(`{{- with .Changes.Pending -}}
-- Planned Changes:
{{ range . -}}
{{- if .Comment -}}
{{- printf "-- %s%s\n" (slice .Comment 0 1 | upper ) (slice .Comment 1) -}}
{{- end -}}
{{- printf "%s;\n" .Cmd -}}
{{- end -}}
{{- else -}}
Schema is synced, no changes to be made
{{ end -}}
`))

SchemaPlanTemplate holds the default template of the 'schema apply --dry-run' command.

Functions

This section is empty.

Types

type AppliedFile

type AppliedFile struct {
	migrate.File
	Start   time.Time
	End     time.Time
	Skipped int      // Amount of skipped SQL statements in a partially applied file.
	Applied []string // SQL statements applied with success
	Error   *StmtError
}

AppliedFile is part of an MigrateApply containing information about an applied file in a migration attempt.

func (*AppliedFile) MarshalJSON

func (f *AppliedFile) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

type Changes

type Changes struct {
	Applied []*migrate.Change `json:"Applied,omitempty"` // SQL changes applied with success
	Pending []*migrate.Change `json:"Pending,omitempty"` // SQL changes that were not applied
	Error   *StmtError        `json:"Error,omitempty"`   // Error that occurred during applying
}

Changes represents a list of changes that are pending or applied.

func (Changes) MarshalJSON

func (c Changes) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

type Env

type 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.
}

Env holds the environment information.

func NewEnv

func NewEnv(c *sqlclient.Client, dir migrate.Dir) Env

NewEnv returns an initialized Env.

type File

type File struct{ migrate.File }

File wraps migrate.File to implement json.Marshaler.

func (File) MarshalJSON

func (f File) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

type Files

type Files []migrate.File

Files is a slice of migrate.File. Implements json.Marshaler.

func (Files) MarshalJSON

func (f Files) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

type MigrateApply

type MigrateApply struct {
	Env
	Pending Files          `json:"Pending,omitempty"` // Pending migration files
	Applied []*AppliedFile `json:"Applied,omitempty"` // Applied files
	Current string         `json:"Current,omitempty"` // Current migration version
	Target  string         `json:"Target,omitempty"`  // Target migration version
	Start   time.Time
	End     time.Time
	// Error is set even then, if it was not caused by a statement in a migration file,
	// but by Atlas, e.g. when committing or rolling back a transaction.
	Error string `json:"Error,omitempty"`
}

MigrateApply contains a summary of a migration applying attempt on a database.

func NewMigrateApply

func NewMigrateApply(client *sqlclient.Client, dir migrate.Dir) *MigrateApply

NewMigrateApply returns an MigrateApply.

func (*MigrateApply) CountStmts

func (a *MigrateApply) CountStmts() (n int)

CountStmts returns the amount of applied statements.

func (*MigrateApply) Log

func (a *MigrateApply) Log(e migrate.LogEntry)

Log implements migrate.Logger.

func (*MigrateApply) MarshalJSON added in v0.9.0

func (a *MigrateApply) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

type MigrateSet added in v0.9.1

type MigrateSet struct {
	// Revisions that were added, removed or updated.
	Revisions []RevisionOp `json:"Revisions,omitempty"`
	// Current version in the revisions table.
	Current *migrate.Revision `json:"Latest,omitempty"`
}

MigrateSet contains a summary of the migrate set command.

func (*MigrateSet) ByVersion added in v0.9.1

func (r *MigrateSet) ByVersion() []RevisionOp

ByVersion returns all revisions sorted by version.

func (*MigrateSet) Removed added in v0.9.1

func (r *MigrateSet) Removed(rev *migrate.Revision)

Removed records revision that was added.

func (*MigrateSet) Set added in v0.9.1

func (r *MigrateSet) Set(rev *migrate.Revision)

Set records revision that was added.

func (*MigrateSet) Summary added in v0.9.1

func (r *MigrateSet) Summary() string

Summary returns a summary of the set operation.

type MigrateStatus

type MigrateStatus struct {
	Env       `json:"Env"`
	Available Files               `json:"Available,omitempty"` // Available migration files
	Pending   Files               `json:"Pending,omitempty"`   // Pending migration files
	Applied   []*migrate.Revision `json:"Applied,omitempty"`   // Applied migration files
	Current   string              `json:"Current,omitempty"`   // Current migration version
	Next      string              `json:"Next,omitempty"`      // Next migration version
	Count     int                 `json:"Count,omitempty"`     // Count of applied statements of the last revision
	Total     int                 `json:"Total,omitempty"`     // Total statements of the last migration
	Status    string              `json:"Status,omitempty"`    // Status of migration (OK, PENDING)
	Error     string              `json:"Error,omitempty"`     // Last Error that occurred
	SQL       string              `json:"SQL,omitempty"`       // SQL that caused the last Error
}

MigrateStatus contains a summary of the migration status of a database.

func NewMigrateStatus

func NewMigrateStatus(c *sqlclient.Client, dir migrate.Dir) (*MigrateStatus, error)

NewMigrateStatus returns a new MigrateStatus.

func (*MigrateStatus) Left

func (r *MigrateStatus) Left() int

Left returns the amount of statements left to apply (if any).

type RevisionOp added in v0.9.1

type RevisionOp struct {
	*migrate.Revision
	Op string `json:"Op,omitempty"`
}

RevisionOp represents an operation done on a revision.

func (*RevisionOp) ColoredVersion added in v0.9.1

func (r *RevisionOp) ColoredVersion() string

ColoredVersion returns the version of the revision with a color.

type SchemaApply

type SchemaApply struct {
	Env
	Changes Changes `json:"Changes,omitempty"`
	// General error that occurred during execution.
	// e.g., when committing or rolling back a transaction.
	Error string `json:"Error,omitempty"`
}

SchemaApply contains a summary of a 'schema apply' execution on a database.

func NewSchemaApply

func NewSchemaApply(env Env, applied, pending []*migrate.Change, err *StmtError) *SchemaApply

NewSchemaApply returns a SchemaApply.

func NewSchemaPlan

func NewSchemaPlan(env Env, pending []*migrate.Change, err *StmtError) *SchemaApply

NewSchemaPlan returns a SchemaApply only with pending changes.

type SchemaDiff added in v0.10.0

type SchemaDiff struct {
	*sqlclient.Client
	From, To *schema.Realm
	Changes  []schema.Change
}

SchemaDiff contains a summary of the 'schema diff' command.

func (*SchemaDiff) MarshalSQL added in v0.13.0

func (s *SchemaDiff) MarshalSQL(indent ...string) (string, error)

MarshalSQL returns the default SQL representation of the schema.

type SchemaInspect added in v0.9.0

type SchemaInspect struct {
	*sqlclient.Client `json:"-"`
	Realm             *schema.Realm `json:"Schema,omitempty"` // Inspected realm.
}

SchemaInspect contains a summary of the 'schema inspect' command.

func (*SchemaInspect) MarshalHCL added in v0.9.0

func (s *SchemaInspect) MarshalHCL() (string, error)

MarshalHCL returns the default HCL representation of the schema. Used by the template declared above.

func (*SchemaInspect) MarshalJSON added in v0.9.0

func (s *SchemaInspect) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (*SchemaInspect) MarshalSQL added in v0.12.1

func (s *SchemaInspect) MarshalSQL(indent ...string) (string, error)

MarshalSQL returns the default SQL representation of the schema.

type StmtError

type StmtError struct {
	Stmt string `json:"Stmt,omitempty"` // SQL statement that failed.
	Text string `json:"Text,omitempty"` // Error message as returned by the database.
}

StmtError groups a statement with its execution error.

Jump to

Keyboard shortcuts

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