mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
* adds migrations closes #57 migrations only run if the database is not brand new. brand new databases will contain all the right fields when CREATE TABLE is called, this is for readability mostly more than efficiency (do not want to have to go through all of the database migrations to ascertain what columns a table has). upon startup of a new database, the migrations will be analyzed and the highest version set, so that future migrations will be run. this should also avoid running through all the migrations, which could bork db's easily enough (if the user just exits from impatience, say). otherwise, all migrations that a db has not yet seen will be run against it upon startup, this should be seamless to the user whether they had a db that had 0 migrations run on it before or N. this means users will not have to explicitly run any migrations on their dbs nor see any errors when we upgrade the db (so long as things go well). if migrations do not go so well, users will have to manually repair dbs (this is the intention of the `migrate` library and it seems sane), this should be rare, and I'm unsure myself how best to resolve not having gone through this myself, I would assume it will require running down migrations and then manually updating the migration field; in any case, docs once one of us has to go through this. migrations are written to files and checked into version control, and then use go-bindata to generate those files into go code and compiled in to be consumed by the migrate library (so that we don't have to put migration files on any servers) -- this is also in vcs. this seems to work ok. I don't like having to use the separate go-bindata tool but it wasn't really hard to install and then go generate takes care of the args. adding migrations should be relatively rare anyway, but tried to make it pretty painless. 1 migration to add created_at to the route is done here as an example of how to do migrations, as well as testing these things ;) -- `created_at` will be `0001-01-01T00:00:00.000Z` for any existing routes after a user runs this version. could spend the extra time adding 'today's date to any outstanding records, but that's not really accurate, the main thing is nobody will have to nuke their db with the migrations in place & we don't have any prod clusters really to worry about. all future routes will correctly have `created_at` set, and plan to add other timestamps but wanted to keep this patch as small as possible so only did routes.created_at. there are tests that a spankin new db will work as expected as well as a db after running all down & up migrations works. the latter tests only run on mysql and postgres, since sqlite3 does not like ALTER TABLE DROP COLUMN; up migrations will need to be tested manually for sqlite3 only, but in theory if they are simple and work on postgres and mysql, there is a good likelihood of success; the new migration from this patch works on sqlite3 fine. for now, we need to use `github.com/rdallman/migrate` to move forward, as getting integrated into upstream is proving difficult due to `github.com/go-sql-driver/mysql` being broken on master (yay dependencies). Fortunately for us, we vendor a version of the `mysql` bindings that actually works, thus, we are capable of using the `mattes/migrate` library with success due to that. this also will require go1.9 to use the new `database/sql.Conn` type, CI has been updated accordingly. some doc fixes too from testing.. and of course updated all deps. anyway, whew. this should let us add fields to the db without busting everybody's dbs. open to feedback on better ways, but this was overall pretty simple despite futzing with mysql. * add migrate pkg to deps, update deps use rdallman/migrate until we resolve in mattes land * add README in migrations package * add ref to mattes lib
113 lines
3.6 KiB
Go
113 lines
3.6 KiB
Go
// Package database provides the Database interface.
|
|
// All database drivers must implement this interface, register themselves,
|
|
// optionally provide a `WithInstance` function and pass the tests
|
|
// in package database/testing.
|
|
package database
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
nurl "net/url"
|
|
"sync"
|
|
)
|
|
|
|
var (
|
|
ErrLocked = fmt.Errorf("can't acquire lock")
|
|
)
|
|
|
|
const NilVersion int = -1
|
|
|
|
var driversMu sync.RWMutex
|
|
var drivers = make(map[string]Driver)
|
|
|
|
// 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().
|
|
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 garantueed 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.
|
|
Drop() error
|
|
}
|
|
|
|
// Open returns a new driver instance.
|
|
func Open(url string) (Driver, error) {
|
|
u, err := nurl.Parse(url)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if u.Scheme == "" {
|
|
return nil, fmt.Errorf("database driver: invalid URL scheme")
|
|
}
|
|
|
|
driversMu.RLock()
|
|
d, ok := drivers[u.Scheme]
|
|
driversMu.RUnlock()
|
|
if !ok {
|
|
return nil, fmt.Errorf("database driver: unknown driver %v (forgotten import?)", u.Scheme)
|
|
}
|
|
|
|
return d.Open(url)
|
|
}
|
|
|
|
// Register globally registers a driver.
|
|
func Register(name string, driver Driver) {
|
|
driversMu.Lock()
|
|
defer driversMu.Unlock()
|
|
if driver == nil {
|
|
panic("Register driver is nil")
|
|
}
|
|
if _, dup := drivers[name]; dup {
|
|
panic("Register called twice for driver " + name)
|
|
}
|
|
drivers[name] = driver
|
|
}
|