darwin

package module
v0.0.0-...-e03bebc Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2024 License: MIT Imports: 6 Imported by: 11

README

Go Report Card GoDoc

Darwin

Database schema evolution library for Go

Example

Check the _examples folder.

LICENSE

The MIT License (MIT)

Copyright (c) 2016 Claudemiro

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Migrate

func Migrate(d Driver, migrations []Migration, infoFn func(MigrationInfo)) error

Migrate executes the missing migrations in database.

func Validate

func Validate(d Driver, migrations []Migration) error

Validate if the database migrations are applied and consistent

Types

type Darwin

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

Darwin is a helper struct to access the Validate and migration functions

func New

func New(driver Driver, migrations []Migration) Darwin

New returns a new Darwin struct

func NewWithInfoCallback

func NewWithInfoCallback(driver Driver, migrations []Migration, infoFn func(MigrationInfo)) Darwin

NewWithInfoCallback allows the user to pass a infoFn callback function that will be executed on every migration.

func (Darwin) Info

func (d Darwin) Info() ([]MigrationInfo, error)

Info returns the status of all migrations

func (Darwin) Migrate

func (d Darwin) Migrate() error

Migrate executes the missing migrations in database

func (Darwin) Validate

func (d Darwin) Validate() error

Validate if the database migrations are applied and consistent

type Dialect

type Dialect interface {
	// CreateTableSQL returns the SQL to create the schema table
	CreateTableSQL() string

	// InsertSQL returns the SQL to insert a new migration in the schema table
	InsertSQL() string

	// AllSQL returns a SQL to get all entries in the table
	AllSQL() string
}

Dialect is used to use multiple databases

type Driver

type Driver interface {
	Create() error
	Insert(e MigrationRecord) error
	All() ([]MigrationRecord, error)
	Exec(string) (time.Duration, error)
}

Driver a database driver abstraction

type DuplicateMigrationVersionError

type DuplicateMigrationVersionError struct {
	Version int
}

DuplicateMigrationVersionError is used to report when the migration list has duplicated entries

func (DuplicateMigrationVersionError) Error

type GenericDriver

type GenericDriver struct {
	DB      *sql.DB
	Dialect Dialect
}

GenericDriver is the default Driver, it can be configured to any database.

func NewGenericDriver

func NewGenericDriver(db *sql.DB, dialect Dialect) *GenericDriver

NewGenericDriver creates a new GenericDriver configured with db and dialect. Panic if db or dialect is nil

func (*GenericDriver) All

func (m *GenericDriver) All() ([]MigrationRecord, error)

All returns all migrations applied

func (*GenericDriver) Create

func (m *GenericDriver) Create() error

Create creates the table darwin_migrations if necessary

func (*GenericDriver) Exec

func (m *GenericDriver) Exec(script string) (time.Duration, error)

Exec execute sql scripts into database

func (*GenericDriver) Insert

func (m *GenericDriver) Insert(e MigrationRecord) error

Insert insert a migration entry into database

type IllegalMigrationVersionError

type IllegalMigrationVersionError struct {
	Version int
}

IllegalMigrationVersionError is used to report when the migration has an illegal Version number

func (IllegalMigrationVersionError) Error

type InvalidChecksumError

type InvalidChecksumError struct {
	Version int
}

InvalidChecksumError is used to report when a migration was modified

func (InvalidChecksumError) Error

func (i InvalidChecksumError) Error() string

type Migration

type Migration struct {
	Version     int
	Description string
	Script      string
}

Migration represents a database migrations.

func (Migration) Checksum

func (m Migration) Checksum() string

Checksum calculate the Script md5

type MigrationInfo

type MigrationInfo struct {
	Status    Status
	Error     error
	Migration Migration
}

MigrationInfo is a struct used in the infoChan to inform clients about the migration being applied.

func Info

func Info(d Driver, migrations []Migration) ([]MigrationInfo, error)

Info returns the status of all migrations

type MigrationRecord

type MigrationRecord struct {
	Version       int
	Description   string
	Checksum      string
	AppliedAt     time.Time
	ExecutionTime time.Duration
}

MigrationRecord is the entry in schema table

type MySQLDialect

type MySQLDialect struct{}

MySQLDialect a Dialect configured for MySQL

func (MySQLDialect) AllSQL

func (m MySQLDialect) AllSQL() string

AllSQL returns a SQL to get all entries in the table

func (MySQLDialect) CreateTableSQL

func (m MySQLDialect) CreateTableSQL() string

CreateTableSQL returns the SQL to create the schema table

func (MySQLDialect) InsertSQL

func (m MySQLDialect) InsertSQL() string

InsertSQL returns the SQL to insert a new migration in the schema table

type PostgresDialect

type PostgresDialect struct{}

PostgresDialect a Dialect configured for PostgreSQL

func (PostgresDialect) AllSQL

func (p PostgresDialect) AllSQL() string

AllSQL returns a SQL to get all entries in the table

func (PostgresDialect) CreateTableSQL

func (p PostgresDialect) CreateTableSQL() string

CreateTableSQL returns the SQL to create the schema table

func (PostgresDialect) InsertSQL

func (p PostgresDialect) InsertSQL() string

InsertSQL returns the SQL to insert a new migration in the schema table

type RemovedMigrationError

type RemovedMigrationError struct {
	Version int
}

RemovedMigrationError is used to report when a migration is removed from the list

func (RemovedMigrationError) Error

func (r RemovedMigrationError) Error() string

type SqliteDialect

type SqliteDialect struct{}

SqliteDialect a Dialect configured for Sqlite3

func (SqliteDialect) AllSQL

func (s SqliteDialect) AllSQL() string

AllSQL returns a SQL to get all entries in the table

func (SqliteDialect) CreateTableSQL

func (s SqliteDialect) CreateTableSQL() string

CreateTableSQL returns the SQL to create the schema table

func (SqliteDialect) InsertSQL

func (s SqliteDialect) InsertSQL() string

InsertSQL returns the SQL to insert a new migration in the schema table

type Status

type Status int

Status is a migration status value

const (
	// Ignored means that the migrations was not applied to the database
	Ignored Status = iota
	// Applied means that the migrations was successfully applied to the database
	Applied
	// Pending means that the migrations is a new migration, and it is waiting to be applied to the database
	Pending
	// Error means that the migration could not be applied to the database
	Error
)

func (Status) String

func (s Status) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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