database

package
v1.0.0-alpha08 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2018 License: AGPL-3.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const NilVersion int64 = -1

Variables

View Source
var (
	ErrLocked = fmt.Errorf("can't acquire lock")
)
View Source
var Settings = []Setting{
	{
		// contains filtered or unexported fields
	},
}

Functions

func Register

func Register(name string, driver Driver)

Types

type Driver

type Driver interface {
	// Open returns a new driver instance configured with parameters
	// coming from the URL string. Migrate will call this function
	// only once per instance.
	Open(url string, isCMD bool, logger *log.Logger) (Driver, error)

	// Close closes the underlying database instance managed by the driver.
	// Migrate will call this function only once per instance.
	Close() error

	// Lock should acquire a database lock so that only one migration process
	// can run at a time. Migrate will call this function before Run is called.
	// If the implementation can't provide this functionality, return nil.
	// Return database.ErrLocked if database is already locked.
	Lock() error

	// Unlock should release the lock. Migrate will call this function after
	// all migrations have been run.
	UnLock() error

	// Run applies a migration to the database. migration is garantueed to be not nil.
	Run(migration io.Reader, fileType string) error

	// Reset Migration Query Args
	ResetQuery()

	// InsertVersion saves version
	// Migrate will call this function before and after each call to Run.
	// version must be >= -1. -1 means NilVersion.
	InsertVersion(version int64) error

	// SetVersion saves version and dirty state.
	// Migrate will call this function before and after each call to Run.
	// version must be >= -1. -1 means NilVersion.
	RemoveVersion(version int64) error

	// Version returns the currently active version and if the database is dirty.
	// When no migration has been applied, it must return version -1.
	// Dirty means, a previous migration failed and user interaction is required.
	Version() (version int64, dirty bool, err error)

	// Reset cleans public schema
	Reset() error

	// First returns the very first migration version available to the driver.
	// Migrate will call this function multiple times
	First() (version uint64, ok bool)

	// Last returns the latest version available in database
	Last() (version uint64, ok bool)

	// Prev returns the previous version for a given version available to the driver.
	// Migrate will call this function multiple times.
	// If there is no previous version available, it must return os.ErrNotExist.
	Prev(version uint64) (prevVersion uint64, ok bool)

	// Next returns the next version for a given version available to the driver.
	// Migrate will call this function multiple times.
	// If there is no next version available, it must return os.ErrNotExist.
	Next(version uint64) (nextVersion uint64, ok bool)

	Read(version uint64) (ok bool)

	SettingsDriver

	MetadataDriver
}

Driver is the interface every database driver must implement.

How to implement a database driver?

  1. Implement this interface.
  2. Optionally, add a function named `WithInstance`. This function should accept an existing DB instance and a Config{} struct and return a driver instance.
  3. Add a test that calls database/testing.go:Test()
  4. Add own tests for Open(), WithInstance() (when provided) and Close(). All other functions are tested by tests in database/testing. Saves you some time and makes sure all database drivers behave the same way.
  5. Call Register in init().
  6. Create a migrate/cli/build_<driver-name>.go file
  7. Add driver name in 'DATABASE' variable in Makefile

Guidelines:

  • Don't try to correct user input. Don't assume things. When in doubt, return an error and explain the situation to the user.
  • All configuration input must come from the URL string in func Open() or the Config{} struct in WithInstance. Don't os.Getenv().

func Open

func Open(url string, isCMD bool, logger *log.Logger) (Driver, error)

Open returns a new driver instance.

type MetadataDriver

type MetadataDriver interface {
	ExportMetadata() (interface{}, error)

	ResetMetadata() error

	ApplyMetadata(data interface{}) error

	Query(data []interface{}) error
}

type Migrations

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

Migrations wraps Migration and has an internal index to keep track of Migration order in database.

func NewMigrations

func NewMigrations() *Migrations

func (*Migrations) Append

func (i *Migrations) Append(version uint64)

func (*Migrations) First

func (i *Migrations) First() (version uint64, ok bool)

func (*Migrations) Last

func (i *Migrations) Last() (uint64, bool)

func (*Migrations) Next

func (i *Migrations) Next(version uint64) (nextVersion uint64, ok bool)

func (*Migrations) Prev

func (i *Migrations) Prev(version uint64) (prevVersion uint64, ok bool)

func (*Migrations) Read

func (i *Migrations) Read(version uint64) (ok bool)

type Setting

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

func (*Setting) GetDefaultValue

func (s *Setting) GetDefaultValue() string

func (*Setting) GetName

func (s *Setting) GetName() string

type SettingsDriver

type SettingsDriver interface {
	// Get Current setting from database
	GetSetting(name string) (value string, err error)

	// UpdateSetting updates a setting in database.
	UpdateSetting(name string, value string) error
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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