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
155 lines
4.4 KiB
Go
155 lines
4.4 KiB
Go
package migrate
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"io"
|
|
"time"
|
|
)
|
|
|
|
// DefaultBufferSize sets the in memory buffer size (in Bytes) for every
|
|
// pre-read migration (see DefaultPrefetchMigrations).
|
|
var DefaultBufferSize = uint(100000)
|
|
|
|
// Migration holds information about a migration.
|
|
// It is initially created from data coming from the source and then
|
|
// used when run against the database.
|
|
type Migration struct {
|
|
// Identifier can be any string to help identifying
|
|
// the migration in the source.
|
|
Identifier string
|
|
|
|
// Version is the version of this migration.
|
|
Version uint
|
|
|
|
// TargetVersion is the migration version after this migration
|
|
// has been applied to the database.
|
|
// Can be -1, implying that this is a NilVersion.
|
|
TargetVersion int
|
|
|
|
// Body holds an io.ReadCloser to the source.
|
|
Body io.ReadCloser
|
|
|
|
// BufferedBody holds an buffered io.Reader to the underlying Body.
|
|
BufferedBody io.Reader
|
|
|
|
// BufferSize defaults to DefaultBufferSize
|
|
BufferSize uint
|
|
|
|
// bufferWriter holds an io.WriteCloser and pipes to BufferBody.
|
|
// It's an *Closer for flow control.
|
|
bufferWriter io.WriteCloser
|
|
|
|
// Scheduled is the time when the migration was scheduled/ queued.
|
|
Scheduled time.Time
|
|
|
|
// StartedBuffering is the time when buffering of the migration source started.
|
|
StartedBuffering time.Time
|
|
|
|
// FinishedBuffering is the time when buffering of the migration source finished.
|
|
FinishedBuffering time.Time
|
|
|
|
// FinishedReading is the time when the migration source is fully read.
|
|
FinishedReading time.Time
|
|
|
|
// BytesRead holds the number of Bytes read from the migration source.
|
|
BytesRead int64
|
|
}
|
|
|
|
// NewMigration returns a new Migration and sets the body, identifier,
|
|
// version and targetVersion. Body can be nil, which turns this migration
|
|
// into a "NilMigration". If no identifier is provided, it will default to "<empty>".
|
|
// targetVersion can be -1, implying it is a NilVersion.
|
|
//
|
|
// What is a NilMigration?
|
|
// Usually each migration version coming from source is expected to have an
|
|
// Up and Down migration. This is not a hard requirement though, leading to
|
|
// a situation where only the Up or Down migration is present. So let's say
|
|
// the user wants to migrate up to a version that doesn't have the actual Up
|
|
// migration, in that case we still want to apply the version, but with an empty
|
|
// body. We are calling that a NilMigration, a migration with an empty body.
|
|
//
|
|
// What is a NilVersion?
|
|
// NilVersion is a const(-1). When running down migrations and we are at the
|
|
// last down migration, there is no next down migration, the targetVersion should
|
|
// be nil. Nil in this case is represented by -1 (because type int).
|
|
func NewMigration(body io.ReadCloser, identifier string,
|
|
version uint, targetVersion int) (*Migration, error) {
|
|
tnow := time.Now()
|
|
m := &Migration{
|
|
Identifier: identifier,
|
|
Version: version,
|
|
TargetVersion: targetVersion,
|
|
Scheduled: tnow,
|
|
}
|
|
|
|
if body == nil {
|
|
if len(identifier) == 0 {
|
|
m.Identifier = "<empty>"
|
|
}
|
|
|
|
m.StartedBuffering = tnow
|
|
m.FinishedBuffering = tnow
|
|
m.FinishedReading = tnow
|
|
return m, nil
|
|
}
|
|
|
|
br, bw := io.Pipe()
|
|
m.Body = body // want to simulate low latency? newSlowReader(body)
|
|
m.BufferSize = DefaultBufferSize
|
|
m.BufferedBody = br
|
|
m.bufferWriter = bw
|
|
return m, nil
|
|
}
|
|
|
|
// String implements string.Stringer and is used in tests.
|
|
func (m *Migration) String() string {
|
|
return fmt.Sprintf("%v [%v=>%v]", m.Identifier, m.Version, m.TargetVersion)
|
|
}
|
|
|
|
// LogString returns a string describing this migration to humans.
|
|
func (m *Migration) LogString() string {
|
|
directionStr := "u"
|
|
if m.TargetVersion < int(m.Version) {
|
|
directionStr = "d"
|
|
}
|
|
return fmt.Sprintf("%v/%v %v", m.Version, directionStr, m.Identifier)
|
|
}
|
|
|
|
// Buffer buffers Body up to BufferSize.
|
|
// Calling this function blocks. Call with goroutine.
|
|
func (m *Migration) Buffer() error {
|
|
if m.Body == nil {
|
|
return nil
|
|
}
|
|
|
|
m.StartedBuffering = time.Now()
|
|
|
|
b := bufio.NewReaderSize(m.Body, int(m.BufferSize))
|
|
|
|
// start reading from body, peek won't move the read pointer though
|
|
// poor man's solution?
|
|
b.Peek(int(m.BufferSize))
|
|
|
|
m.FinishedBuffering = time.Now()
|
|
|
|
// write to bufferWriter, this will block until
|
|
// something starts reading from m.Buffer
|
|
n, err := b.WriteTo(m.bufferWriter)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
m.FinishedReading = time.Now()
|
|
m.BytesRead = n
|
|
|
|
// close bufferWriter so Buffer knows that there is no
|
|
// more data coming
|
|
m.bufferWriter.Close()
|
|
|
|
// it's safe to close the Body too
|
|
m.Body.Close()
|
|
|
|
return nil
|
|
}
|