file

package
v1.5.2 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2023 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package file contains functions for low-level migration files handling.

Index

Constants

View Source
const TablesDir = "tables/"

TablesDir prefix for DumpWriter/DumpReader

Variables

View Source
var V2 bool

V2 set to true to use version 2 for schema migrations which enables major versions. V2 is not backwards compatible with previous version. So don't set this to true and then set it to false.

Functions

func IsEmpty added in v1.3.0

func IsEmpty(dir string) (bool, error)

IsEmpty returns true if the directory is empty

func LineColumnFromOffset

func LineColumnFromOffset(data []byte, offset int) (line, column int)

LineColumnFromOffset reads data and returns line and column integer for a given offset.

func LinesBeforeAndAfter

func LinesBeforeAndAfter(data []byte, line, before, after int, lineNumbers bool) []byte

LinesBeforeAndAfter reads n lines before and after a given line. Set lineNumbers to true, to prepend line numbers.

func ReadFilesBetween added in v1.3.0

func ReadFilesBetween(prevBasePath, basePath string, filenameExtension string, force bool) (curVersion, dstVersion Version, migrations Migrations, err error)

ReadFilesBetween reads the previous and current files and returns the files needed to go from the previous version to the current version

func RemoveContents added in v1.3.0

func RemoveContents(dir string) (err error)

RemoveContents removes all the files/directories inside the passed in dir

Types

type DirReader added in v1.3.0

type DirReader struct {
	BaseDir string
	V2      bool
}

DirReader struct

func (*DirReader) Files added in v1.3.0

func (d *DirReader) Files(dir string) (Openers, error)

Files returns opens a writer for the passed in file name

type DirWriter added in v1.3.0

type DirWriter struct {
	BaseDir string
}

DirWriter struct

func (*DirWriter) Close added in v1.3.0

func (d *DirWriter) Close() error

func (*DirWriter) Writer added in v1.3.0

func (d *DirWriter) Writer(dir, name string) (io.WriteCloser, error)

Writer opens a writer for the passed in file name

type DumpReader added in v1.3.0

type DumpReader interface {
	Files(dir string) (Openers, error)
}

DumpReader interface

func NewZipReader added in v1.3.0

func NewZipReader(zipFile string) (DumpReader, error)

NewZipReader returns a new DumpReader

type DumpWriter added in v1.3.0

type DumpWriter interface {
	Writer(dir, name string) (io.WriteCloser, error)
	Close() error
}

DumpWriter interface

func NewZipWriter added in v1.3.0

func NewZipWriter(zipFile, tmpFile string) (DumpWriter, error)

NewZipWriter returns a new DumpWriter

type File

type File struct {
	Open func() (io.ReadCloser, error)

	// the name of the file
	FileName string

	// version parsed from filename
	Version

	// the actual migration name parsed from filename
	Name string

	// content of the file
	Content []byte

	// UP or DOWN migration
	Direction direction.Direction
}

File represents one file on disk. Example: 001_initial_plan_to_do_sth.up.sql

func (*File) Delete added in v1.3.0

func (f *File) Delete(prevDir string) (err error)

Delete reads the file's content and writes to the passed in path

func (*File) ReadContent

func (f *File) ReadContent() error

ReadContent reads the file's content if the content is nil

func (*File) Write added in v1.3.0

func (f *File) Write(baseDir string, mkDir bool) (err error)

Write reads the file's content and writes to the passed in path

func (*File) WriteContent added in v1.3.0

func (f *File) WriteContent(getWriter func(majorDir string, name string) (io.WriteCloser, error), release bool) (err error)

WriteContent reads the file's content and writes to the writer

type Files

type Files []*File

Files is a slice of Files

type Migration added in v1.3.0

type Migration struct {
	Version
	// contains filtered or unexported fields
}

func (*Migration) DownContent added in v1.3.0

func (m *Migration) DownContent() ([]byte, error)

func (*Migration) File added in v1.3.0

func (m *Migration) File() *File

func (*Migration) FileContent added in v1.3.0

func (m *Migration) FileContent() (up, down []byte, err error)

func (*Migration) Up added in v1.3.0

func (m *Migration) Up() bool

func (*Migration) UpContent added in v1.3.0

func (m *Migration) UpContent() ([]byte, error)

type MigrationFile

type MigrationFile struct {
	// version of the migration file, parsed from the filenames
	Version

	// reference to the *up* migration file
	UpFile *File

	// reference to the *down* migration file
	DownFile *File
}

MigrationFile represents both the UP and the DOWN migration file.

func (MigrationFile) Migration added in v1.3.0

func (mf MigrationFile) Migration(d direction.Direction) (m Migration)

Migration returns the migration for the passed in direction

func (MigrationFile) WriteFileContents added in v1.3.0

func (mf MigrationFile) WriteFileContents(getWriter func(string, string) (io.WriteCloser, error), release bool) (err error)

func (MigrationFile) WriteFiles added in v1.3.0

func (mf MigrationFile) WriteFiles(baseDir string) (err error)

type MigrationFiles

type MigrationFiles []MigrationFile

MigrationFiles is a slice of MigrationFiles

func GetMigrationFiles added in v1.3.0

func GetMigrationFiles(openers Openers, filenameExtension string) (files MigrationFiles, err error)

func ReadMigrationFiles

func ReadMigrationFiles(basePath string, filenameExtension string) (files MigrationFiles, err error)

ReadMigrationFiles reads all migration files from a given path

func (MigrationFiles) Between added in v1.3.0

func (mf MigrationFiles) Between(prevFiles MigrationFiles, force bool) (curVersion, dstVersion Version, migrations Migrations, err error)

Between either returns migrations to migrate down using the previous migrations or it returns migrations to migrate up from the end of the previous migrations to the current migrations. 'force' should only be used if the text is different, but the end result is the same. Such as adding/removing comments or adding 'IF EXISTS'/'IF NOT EXISTS'

func (MigrationFiles) DownTo added in v1.3.0

func (mf MigrationFiles) DownTo(dstVersion Version) Migrations

DownTo fetches all (down) migration files including the migration file of the current version to the very first migration file.

func (MigrationFiles) From

func (mf MigrationFiles) From(version Version, relativeN int) Migrations

From travels relatively through migration files.

+1 will fetch the next up migration file
+2 will fetch the next two up migration files
+n will fetch ...
-1 will fetch the the previous down migration file
-2 will fetch the next two previous down migration files
-n will fetch ...

func (MigrationFiles) FromTo added in v1.3.0

func (mf MigrationFiles) FromTo(startVersion, stopVersion Version) (migrations Migrations, err error)

FromTo returns the migration files between the two passed in versions

func (MigrationFiles) LastVersion added in v1.3.0

func (mf MigrationFiles) LastVersion() Version

LastVersion returns the last version or empty

func (MigrationFiles) Len

func (mf MigrationFiles) Len() int

Len is the number of elements in the collection. Required by Sort Interface{}

func (MigrationFiles) Less

func (mf MigrationFiles) Less(i, j int) bool

Less reports whether the element with index i should sort before the element with index j. Required by Sort Interface{}

func (MigrationFiles) MissingVersion added in v1.3.0

func (mf MigrationFiles) MissingVersion() Version

func (MigrationFiles) Swap

func (mf MigrationFiles) Swap(i, j int)

Swap swaps the elements with indexes i and j. Required by Sort Interface{}

func (MigrationFiles) ToFirstFrom

func (mf MigrationFiles) ToFirstFrom(version Version) Migrations

ToFirstFrom fetches all (down) migration files including the migration file of the current version to the very first migration file.

func (MigrationFiles) ToLastFrom

func (mf MigrationFiles) ToLastFrom(version Version) Migrations

ToLastFrom fetches all (up) migration files to the most recent migration file. The migration file of the current version is not included.

func (MigrationFiles) ValidateBaseFiles added in v1.3.0

func (mf MigrationFiles) ValidateBaseFiles(prevFiles MigrationFiles) error

ValidateBaseFiles validates that the base files have the same versions and upfile content

type Migrations added in v1.3.0

type Migrations []Migration

func (Migrations) Len added in v1.3.0

func (m Migrations) Len() int

func (Migrations) Less added in v1.3.0

func (m Migrations) Less(i, j int) bool

func (Migrations) Swap added in v1.3.0

func (m Migrations) Swap(i, j int)

type Opener added in v1.3.0

type Opener struct {
	Name string
	Open func() (io.ReadCloser, error)
}

Opener struct

type Openers added in v1.3.0

type Openers []Opener

Openers is a list of openers

type Version added in v1.3.0

type Version interface {
	Inc(major bool) Version
	String() string
	Major() uint64
	Minor() uint64
	MajorString() string
	MinorString() string
	Compare(other Version) int
}

func NewVersion added in v1.3.0

func NewVersion(version uint64) Version

func NewVersion2 added in v1.3.0

func NewVersion2(major, minor uint64) Version

func ParseVersion added in v1.3.0

func ParseVersion(s string) (Version, error)

Parse parses the version

Jump to

Keyboard shortcuts

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