56 lines
1.1 KiB
Go
56 lines
1.1 KiB
Go
|
package database
|
||
|
|
||
|
import (
|
||
|
"net/http"
|
||
|
|
||
|
"github.com/blang/semver"
|
||
|
"github.com/fjl/go-couchdb"
|
||
|
)
|
||
|
|
||
|
const (
|
||
|
VersionDocumentId = "version"
|
||
|
)
|
||
|
|
||
|
type MigrationVersionDocument struct {
|
||
|
Rev string `json:"_rev,omitempty"`
|
||
|
Version semver.Version
|
||
|
}
|
||
|
|
||
|
type MigrationDatabase struct {
|
||
|
*couchdb.DB
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
GetVersion returns the current version as stored in the migration database.
|
||
|
If no document was found, defaults to returning "0.0.0" as semver.Version
|
||
|
object.
|
||
|
*/
|
||
|
func (mdb *MigrationDatabase) GetVersion() (version semver.Version, rev string, err error) {
|
||
|
v := new(MigrationVersionDocument)
|
||
|
err = mdb.DB.Get(VersionDocumentId, v, nil)
|
||
|
if err != nil {
|
||
|
isActuallyAnError := true
|
||
|
if cerr, ok := err.(*couchdb.Error); ok {
|
||
|
// CouchDB error
|
||
|
if cerr.StatusCode == http.StatusNotFound {
|
||
|
isActuallyAnError = false
|
||
|
}
|
||
|
}
|
||
|
if isActuallyAnError {
|
||
|
return
|
||
|
}
|
||
|
err = nil
|
||
|
} else {
|
||
|
rev = v.Rev
|
||
|
version = v.Version
|
||
|
}
|
||
|
return
|
||
|
}
|
||
|
|
||
|
func (mdb *MigrationDatabase) PutVersion(version semver.Version, rev string) (newrev string, err error) {
|
||
|
newrev, err = mdb.DB.Put(VersionDocumentId, MigrationVersionDocument{
|
||
|
Version: version,
|
||
|
}, rev)
|
||
|
return
|
||
|
}
|