Documentation
¶
Overview ¶
package mgrt provides a collection of functions for performing revisions against any given database connection.
Index ¶
- Variables
- func PerformRevisions(db *DB, revs0 ...*Revision) error
- func Register(typ string, db *DB)
- func RevisionPerformed(db *DB, rev *Revision) error
- type Collection
- type DB
- type Errors
- type Revision
- func GetRevision(db *DB, id string) (*Revision, error)
- func GetRevisions(db *DB, n int) ([]*Revision, error)
- func NewRevision(author, comment string) *Revision
- func NewRevisionCategory(category, author, comment string) *Revision
- func OpenRevision(path string) (*Revision, error)
- func UnmarshalRevision(r io.Reader) (*Revision, error)
- type RevisionError
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalid is returned whenever an invalid Revision ID is encountered. A // Revision ID is considered invalid when the time layout 20060102150405 // cannot be used for parse the ID. ErrInvalid = errors.New("revision id invalid") // ErrPerformed is returned whenever a Revision has already been performed. // This can be treated as a benign error. ErrPerformed = errors.New("revision performed") ErrNotFound = errors.New("revision not found") )
Functions ¶
func PerformRevisions ¶
PerformRevisions will perform the given revisions against the given database. The given revisions will be sorted into ascending order first before they are performed. If any of the given revisions have already been performed then the Errors type will be returned containing *RevisionError for each revision that was already performed.
func Register ¶
Register will register the given *DB for the given database type. If the given type is a duplicate, then this panics. If the given *DB is nil, then this panics.
func RevisionPerformed ¶
RevisionPerformed checks to see if the given Revision has been performed against the given database.
Types ¶
type Collection ¶
type Collection struct {
// contains filtered or unexported fields
}
Collection stores revisions in a binary tree. This ensures that when they are retrieved, they will be retrieved in ascending order from when they were initially added.
func (*Collection) Len ¶
func (c *Collection) Len() int
Len returns the number of items in the collection.
func (*Collection) Put ¶
func (c *Collection) Put(r *Revision) error
Put puts the given Revision in the current Collection.
func (*Collection) Slice ¶
func (c *Collection) Slice() []*Revision
Slice returns a sorted slice of all the revisions in the collection.
type DB ¶
type DB struct { *sql.DB // Type is the type of database being connected to. This will be passed to // sql.Open when the connection is being opened. Type string // Init is the function to call to initialize the database for performing // revisions. Init func(*sql.DB) error // Parameterize is the function that is called to parameterize the query // that will be executed against the database. This will make sure the // correct SQL dialect is being used for the type of database. Parameterize func(string) string }
DB is a thin abstraction over the *sql.DB struct from the stdlib.
type Revision ¶
type Revision struct { ID string // ID is the unique ID of the Revision. Category string // Category of the revision. Author string // Author is who authored the original Revision. Comment string // Comment provides a short description for the Revision. SQL string // SQL is the code that will be executed when the Revision is performed. PerformedAt time.Time // PerformedAt is when the Revision was executed. }
Revision is the type that represents what SQL code has been executed against a database as a revision. Typically, this would be changes made to the database schema itself.
func GetRevision ¶
GetRevision get's the Revision with the given ID.
func GetRevisions ¶
GetRevisions returns a list of all the revisions that have been performed against the given database. If n is <= 0 then all of the revisions will be retrieved, otherwise, only the given amount will be retrieved. The returned revisions will be ordered by their performance date descending.
func NewRevision ¶
NewRevision creates a new Revision with the given author, and comment.
func NewRevisionCategory ¶ added in v3.2.0
NewRevisionCategory creates a new Revision in the given category with the given author and comment.
func OpenRevision ¶
OpenRevision opens the revision at the given path.
func UnmarshalRevision ¶
UnmarshalRevision will unmarshal a Revision from the given io.Reader. This will expect to see a comment block header that contains the metadata about the Revision itself. This will check to see if the given Revision ID is valid. A Revision id is considered valid when it can be parsed into a valid time via time.Parse using the layout of 20060102150405.
func (*Revision) Perform ¶
Perform will perform the current Revision against the given database. If the Revision is emtpy, then nothing happens. If the Revision has already been performed, then ErrPerformed is returned.
func (*Revision) Slug ¶ added in v3.2.1
Slug returns the slug of the revision ID, this will be in the format of category/id if the revision belongs to a category.
func (*Revision) String ¶
String returns the string representation of the Revision. This will be the comment block header followed by the Revision SQL itself.
func (*Revision) Title ¶
Title will extract the title from the comment of the current Revision. First, this will truncate the title to being 72 characters. If the comment was longer than 72 characters, then the title will be suffixed with "...". If a LF character can be found in the title, then the title will be truncated again up to where that LF character occurs.
type RevisionError ¶
type RevisionError struct { ID string // ID is the ID of the revisions that errored. Err error // Err is the underlying error itself. }
RevisionError represents an error that occurred with a revision.
func (*RevisionError) Error ¶
func (e *RevisionError) Error() string
func (*RevisionError) Unwrap ¶
func (e *RevisionError) Unwrap() error
Unwrap returns the underlying error that caused the original RevisionError.