/
target_mongodb.go
122 lines (108 loc) · 3.22 KB
/
target_mongodb.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package migration
import (
"time"
"github.com/globalsign/mgo"
"github.com/globalsign/mgo/bson"
)
// MongoDBTarget implements the migration.Target of the MongoDB.
//
// In order to get access to the MongoDB, migration.MongoDBTarget uses the MGo
// library (http://github.com/globalsign/mgo).
type MongoDBTarget struct {
db *mgo.Database
collectionName string
}
// mongoDBMigrationVersion represents the version stored on the MongoDB.
type mongoDBMigrationVersion struct {
ID time.Time `bson:"_id"`
}
// NewMongoDB returns a new instance of the migration.MongoDBTarget
func NewMongoDB(db *mgo.Database) *MongoDBTarget {
return &MongoDBTarget{
collectionName: DefaultMigrationTable,
db: db,
}
}
func (t *MongoDBTarget) runWithDB(cb func(db *mgo.Database) error) error {
return cb(t.db)
}
func (t *MongoDBTarget) collection(db *mgo.Database) *mgo.Collection {
return db.C(t.collectionName)
}
// Version implements the migration.Target.Version by fetching the current
// version of the database from the collection defined by
// migration.MongoDBTarget.SetCollectionName.
//
// It returns the current version of the database.
//
// Any error returned by the MGo, will be passed up to the caller.
func (t *MongoDBTarget) Version() (migrationID time.Time, err error) {
err = t.runWithDB(func(db *mgo.Database) error {
c := t.collection(db)
var version mongoDBMigrationVersion
q := c.Find(nil).Sort("-_id").Limit(1) // Most recent
if err = q.One(&version); err == nil {
migrationID = version.ID.UTC()
return nil
} else if err == mgo.ErrNotFound {
migrationID = NoVersion
return nil
}
migrationID = NoVersion
return err
})
return
}
// SetVersion implements the migration.Target.SetVersion by storing the passed
// version on the database.
//
// It returns eny error returned by the MGo.
func (t *MongoDBTarget) AddMigration(summary *Summary) error {
return t.runWithDB(func(db *mgo.Database) error {
c := t.collection(db)
if _, err := c.Upsert(
bson.M{"_id": summary.Migration.GetID()},
&mongoDBMigrationVersion{
ID: summary.Migration.GetID(),
}); err != nil {
return err
}
return nil
})
}
// RemoveMigration find and removes a migrations from the collection.
func (t *MongoDBTarget) RemoveMigration(summary *Summary) error {
return t.runWithDB(func(db *mgo.Database) error {
c := t.collection(db)
return c.Remove(map[string]interface{}{"_id": summary.Migration.GetID()})
})
}
func (t *MongoDBTarget) MigrationsExecuted() ([]time.Time, error) {
migrations := make([]mongoDBMigrationVersion, 0)
err := t.runWithDB(func(db *mgo.Database) error {
c := t.collection(db)
err := c.Find(nil).Sort("_id").All(&migrations)
if err != nil {
return err
}
return nil
})
if err != nil {
return nil, err
}
r := make([]time.Time, len(migrations))
for i, migration := range migrations {
r[i] = migration.ID
}
return r, nil
}
// SetCollectionName sets the name of the collection used to store the current
// version of the database.
func (t *MongoDBTarget) SetCollectionName(collection string) *MongoDBTarget {
t.collectionName = collection
return t
}
// Database returns the `*mgo.Database` reference of this target.
func (t *MongoDBTarget) Database() *mgo.Database {
return t.db
}