package database
import "github.com/golang-migrate/migrate/v4/database"
Package database provides the Driver interface. All database drivers must implement this interface, register themselves, optionally provide a `WithInstance` function and pass the tests in package database/testing.
Index ¶
- Constants
- Variables
- func CasRestoreOnErr(lock *atomic.Bool, o, n bool, casErr error, f func() error) error
- func GenerateAdvisoryLockId(databaseName string, additionalNames ...string) (string, error)
- func List() []string
- func Register(name string, driver Driver)
- type Driver
- type Error
Examples ¶
Constants ¶
const NilVersion int = -1
Variables ¶
var ( ErrLocked = fmt.Errorf("can't acquire lock") ErrNotLocked = fmt.Errorf("can't unlock, as not currently locked") )
Functions ¶
func CasRestoreOnErr ¶
CasRestoreOnErr CAS wrapper to automatically restore the lock state on error
func GenerateAdvisoryLockId ¶
GenerateAdvisoryLockId inspired by rails migrations, see https://goo.gl/8o9bCT
func List ¶
func List() []string
List lists the registered drivers
func Register ¶
Register globally registers a 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) (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 guaranteed to be not nil.
Run(migration io.Reader) 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.
SetVersion(version int, dirty bool) 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 int, dirty bool, err error)
// Drop deletes everything in the database.
// Note that this is a breaking action, a new call to Open() is necessary to
// ensure subsequent calls work as expected.
Drop() error
}
Driver is the interface every database driver must implement.
How to implement a database driver?
- Implement this interface.
- Optionally, add a function named `WithInstance`. This function should accept an existing DB instance and a Config{} struct and return a driver instance.
- Add a test that calls database/testing.go:Test()
- 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.
- Call Register in init().
- Create a internal/cli/build_<driver-name>.go file
- 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().
Example¶
Code:
{
// see database/stub for an example
// database/stub/stub.go has the driver implementation
// database/stub/stub_test.go runs database/testing/test.go:Test
}
func Open ¶
Open returns a new driver instance.
type Error ¶
type Error struct {
// Optional: the line number
Line uint
// Query is a query excerpt
Query []byte
// Err is a useful/helping error message for humans
Err string
// OrigErr is the underlying error
OrigErr error
}
Error should be used for errors involving queries ran against the database
func (Error) Error ¶
Source Files ¶
Directories ¶
| Path | Synopsis |
|---|---|
| database/cassandra | |
| database/clickhouse | |
| database/cockroachdb | |
| database/firebird | |
| database/mongodb | |
| database/multistmt | Package multistmt provides methods for parsing multi-statement database migrations |
| database/mysql | |
| database/neo4j | |
| database/pgx | |
| database/pgx/v5 | |
| database/postgres | |
| database/ql | |
| database/redshift | |
| database/rqlite | |
| database/snowflake | |
| database/spanner | |
| database/sqlcipher | |
| database/sqlite | |
| database/sqlite3 | |
| database/sqlserver | |
| database/stub | |
| database/testing | Package testing has the database tests. |
| database/yugabytedb |
- Version
- v4.18.3 (latest)
- Published
- Apr 17, 2025
- Platform
- linux/amd64
- Imports
- 7 packages
- Last checked
- 1 year ago –
Tools for package owners.