migrate

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2022 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

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 *StatusReport) (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)

	// DefaultStatusTemplate holds the default template of the 'migrate status' command.
	DefaultStatusTemplate = 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,
		"json":       jsonEncode,
		"json_merge": jsonMerge,
	})

	// DefaultApplyTemplate holds the default template of the 'migrate apply' command.
	DefaultApplyTemplate = template.Must(template.
							New("report").
							Funcs(ApplyTemplateFuncs).
							Parse(`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 .Error }}
  {{- 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 }}
`))
)
View Source
var (
	// ColorTemplateFuncs are globally available functions to color strings in a report template.
	ColorTemplateFuncs = template.FuncMap{
		"cyan":         color.CyanString,
		"green":        color.HiGreenString,
		"red":          color.HiRedString,
		"redBgWhiteFg": color.New(color.FgHiWhite, color.BgHiRed).SprintFunc(),
		"yellow":       color.YellowString,
	}
)

Functions

This section is empty.

Types

type AppliedFile added in v0.8.0

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   *struct {
		SQL   string // SQL statement that failed.
		Error string // Error returned by the database.
	}
}

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

func (*AppliedFile) MarshalJSON added in v0.8.0

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

MarshalJSON implements json.Marshaler.

type ApplyReport added in v0.8.0

type ApplyReport struct {
	Report `json:"-"`
	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"`
}

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

func NewApplyReport added in v0.8.0

func NewApplyReport(client *sqlclient.Client, dir migrate.Dir) *ApplyReport

NewApplyReport returns an ApplyReport.

func (*ApplyReport) CountStmts added in v0.8.0

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

CountStmts returns the amount of applied statements.

func (*ApplyReport) Log added in v0.8.0

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

Log implements migrate.Logger.

type EntRevisions

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

EntRevisions provides implementation for the migrate.RevisionReadWriter interface.

func NewEntRevisions

func NewEntRevisions(ctx context.Context, ac *sqlclient.Client, opts ...Option) (*EntRevisions, error)

NewEntRevisions creates a new EntRevisions with the given sqlclient.Client.

func (*EntRevisions) DeleteRevision added in v0.7.1

func (r *EntRevisions) DeleteRevision(ctx context.Context, v string) error

DeleteRevision deletes a revision from the revisions table.

func (*EntRevisions) Ident added in v0.6.0

func (r *EntRevisions) Ident() *migrate.TableIdent

Ident returns the table identifier.

func (*EntRevisions) Migrate added in v0.6.4

func (r *EntRevisions) Migrate(ctx context.Context) (err error)

Migrate attempts to create / update the revisions table. This is separated since Ent attempts to wrap the migration execution in a transaction and assumes the underlying connection is of type *sql.DB, which is not true for actually reading and writing revisions.

func (*EntRevisions) ReadRevision added in v0.6.0

func (r *EntRevisions) ReadRevision(ctx context.Context, v string) (*migrate.Revision, error)

ReadRevision reads a revision from the revisions table.

ReadRevision will not return results only saved in cache.

func (*EntRevisions) ReadRevisions

func (r *EntRevisions) ReadRevisions(ctx context.Context) ([]*migrate.Revision, error)

ReadRevisions reads the revisions from the revisions table.

ReadRevisions will not return results only saved to cache.

func (*EntRevisions) WriteRevision

func (r *EntRevisions) WriteRevision(ctx context.Context, rev *migrate.Revision) error

WriteRevision writes a revision to the revisions table.

type Env added in v0.8.0

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 added in v0.8.0

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

NewEnv returns an initialized Env.

type File added in v0.8.0

type File struct{ migrate.File }

File wraps migrate.File to implement json.Marshaler.

func (File) MarshalJSON added in v0.8.0

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

MarshalJSON implements json.Marshaler.

type Files added in v0.8.0

type Files []migrate.File

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

func (Files) MarshalJSON added in v0.8.0

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

MarshalJSON implements json.Marshaler.

type Option

type Option func(*EntRevisions) error

Option allows to configure EntRevisions by using functional arguments.

func WithSchema

func WithSchema(s string) Option

WithSchema configures the schema to use for the revision table.

type Report added in v0.8.0

type Report interface {
	// contains filtered or unexported methods
}

A Report represents collected information about the execution of a 'atlas migrate' command execution.

type ReportWriter added in v0.8.0

type ReportWriter interface {
	WriteReport(Report) error
}

ReportWriter writes a Report.

type StatusReport added in v0.8.0

type StatusReport struct {
	Report    `json:"-"`
	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
}

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

func NewStatusReport added in v0.8.0

func NewStatusReport(c *sqlclient.Client, dir migrate.Dir) (*StatusReport, error)

NewStatusReport returns a new StatusReport.

func (*StatusReport) Left added in v0.8.0

func (r *StatusReport) Left() int

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

type StatusReporter added in v0.8.0

type StatusReporter struct {
	// Client configures the connection to the database to file a StatusReport for.
	Client *sqlclient.Client
	// Dir is used for scanning and validating the migration directory.
	Dir migrate.Dir
	// ReportWriter writes the summary report.
	ReportWriter ReportWriter
	// Schema name the revision table resides in.
	Schema string
}

StatusReporter is used to gather information about migration status.

func (*StatusReporter) Report added in v0.8.0

func (r *StatusReporter) Report(ctx context.Context) error

Report creates and writes a StatusReport.

type TemplateWriter added in v0.8.0

type TemplateWriter struct {
	T *template.Template
	W io.Writer
}

A TemplateWriter is ReportWriter that writes output according to a template.

func (*TemplateWriter) WriteReport added in v0.8.0

func (w *TemplateWriter) WriteReport(r Report) error

WriteReport implements ReportWriter.

Directories

Path Synopsis
ent

Jump to

Keyboard shortcuts

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