Documentation
¶
Overview ¶
Package file contains functions for low-level migration files handling.
Index ¶
- Constants
- Variables
- func IsEmpty(dir string) (bool, error)
- func LineColumnFromOffset(data []byte, offset int) (line, column int)
- func LinesBeforeAndAfter(data []byte, line, before, after int, lineNumbers bool) []byte
- func ReadFilesBetween(prevBasePath, basePath string, filenameExtension string, force bool) (curVersion, dstVersion Version, migrations Migrations, err error)
- func RemoveContents(dir string) (err error)
- type DirReader
- type DirWriter
- type DumpReader
- type DumpWriter
- type File
- type Files
- type Migration
- type MigrationFile
- type MigrationFiles
- func (mf MigrationFiles) Between(prevFiles MigrationFiles, force bool) (curVersion, dstVersion Version, migrations Migrations, err error)
- func (mf MigrationFiles) DownTo(dstVersion Version) Migrations
- func (mf MigrationFiles) From(version Version, relativeN int) Migrations
- func (mf MigrationFiles) FromTo(startVersion, stopVersion Version) (migrations Migrations, err error)
- func (mf MigrationFiles) LastVersion() Version
- func (mf MigrationFiles) Len() int
- func (mf MigrationFiles) Less(i, j int) bool
- func (mf MigrationFiles) MissingVersion() Version
- func (mf MigrationFiles) Swap(i, j int)
- func (mf MigrationFiles) ToFirstFrom(version Version) Migrations
- func (mf MigrationFiles) ToLastFrom(version Version) Migrations
- func (mf MigrationFiles) ValidateBaseFiles(prevFiles MigrationFiles) error
- type Migrations
- type Opener
- type Openers
- type Version
Constants ¶
const TablesDir = "tables/"
TablesDir prefix for DumpWriter/DumpReader
Variables ¶
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 LineColumnFromOffset ¶
LineColumnFromOffset reads data and returns line and column integer for a given offset.
func LinesBeforeAndAfter ¶
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
RemoveContents removes all the files/directories inside the passed in dir
Types ¶
type DumpReader ¶ added in v1.3.0
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
Delete reads the file's content and writes to the passed in path
func (*File) ReadContent ¶
ReadContent reads the file's content if the content is nil
func (*File) Write ¶ added in v1.3.0
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 Migration ¶ added in v1.3.0
type Migration struct { Version // contains filtered or unexported fields }
func (*Migration) DownContent ¶ added in v1.3.0
func (*Migration) FileContent ¶ added in v1.3.0
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 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 NewVersion2 ¶ added in v1.3.0
func ParseVersion ¶ added in v1.3.0
Parse parses the version