Skip to content

Commit 741da34

Browse files
Asakizgustavosbarreto
authored andcommitted
api: restruct the migrations
Signed-off-by: asakiz <[email protected]>
1 parent 03e4dc7 commit 741da34

25 files changed

+1006
-758
lines changed

api/store/mongo/migrations.go

Lines changed: 2 additions & 753 deletions
Large diffs are not rendered by default.

api/store/mongo/migrations/main.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package migrations
2+
3+
import (
4+
"context"
5+
6+
migrate "github.com/xakep666/mongo-migrate"
7+
"go.mongodb.org/mongo-driver/bson"
8+
"go.mongodb.org/mongo-driver/mongo"
9+
)
10+
11+
func GenerateMigrations() []migrate.Migration {
12+
return []migrate.Migration{
13+
migration_1,
14+
migration_2,
15+
migration_3,
16+
migration_4,
17+
migration_5,
18+
migration_6,
19+
migration_7,
20+
migration_8,
21+
migration_9,
22+
migration_10,
23+
migration_11,
24+
migration_12,
25+
migration_13,
26+
migration_14,
27+
migration_15,
28+
migration_16,
29+
migration_17,
30+
migration_18,
31+
migration_19,
32+
migration_20,
33+
migration_21,
34+
migration_22,
35+
}
36+
}
37+
38+
func renameField(db *mongo.Database, coll, from, to string) error {
39+
_, err := db.Collection(coll).UpdateMany(context.Background(), bson.M{}, bson.M{"$rename": bson.M{from: to}})
40+
return err
41+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package migrations
2+
3+
import (
4+
migrate "github.com/xakep666/mongo-migrate"
5+
"go.mongodb.org/mongo-driver/mongo"
6+
)
7+
8+
var migration_1 = migrate.Migration{
9+
Version: 1,
10+
Up: func(db *mongo.Database) error {
11+
return nil
12+
},
13+
Down: func(db *mongo.Database) error {
14+
return nil
15+
},
16+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package migrations
2+
3+
import (
4+
"context"
5+
6+
migrate "github.com/xakep666/mongo-migrate"
7+
"go.mongodb.org/mongo-driver/bson"
8+
"go.mongodb.org/mongo-driver/mongo"
9+
"go.mongodb.org/mongo-driver/mongo/options"
10+
)
11+
12+
var migration_10 = migrate.Migration{
13+
Version: 10,
14+
Up: func(db *mongo.Database) error {
15+
mod := mongo.IndexModel{
16+
Keys: bson.D{{"session_record", 1}},
17+
Options: options.Index().SetName("session_record").SetUnique(false),
18+
}
19+
if _, err := db.Collection("users").Indexes().CreateOne(context.TODO(), mod); err != nil {
20+
return err
21+
}
22+
_, err := db.Collection("users").UpdateMany(context.TODO(), bson.M{}, bson.M{"$set": bson.M{"session_record": true}})
23+
return err
24+
},
25+
Down: func(db *mongo.Database) error {
26+
return nil
27+
},
28+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package migrations
2+
3+
import (
4+
"context"
5+
6+
migrate "github.com/xakep666/mongo-migrate"
7+
"go.mongodb.org/mongo-driver/bson"
8+
"go.mongodb.org/mongo-driver/mongo"
9+
"go.mongodb.org/mongo-driver/mongo/options"
10+
)
11+
12+
var migration_11 = migrate.Migration{
13+
Version: 11,
14+
Up: func(db *mongo.Database) error {
15+
mod := mongo.IndexModel{
16+
Keys: bson.D{{"created_at", 1}},
17+
Options: options.Index().SetName("ttl").SetExpireAfterSeconds(60),
18+
}
19+
_, err := db.Collection("private_keys").Indexes().CreateOne(context.TODO(), mod)
20+
if err != nil {
21+
return err
22+
}
23+
24+
return nil
25+
},
26+
Down: func(db *mongo.Database) error {
27+
_, err := db.Collection("private_keys").Indexes().DropOne(context.TODO(), "ttl")
28+
return err
29+
},
30+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package migrations
2+
3+
import (
4+
"context"
5+
6+
migrate "github.com/xakep666/mongo-migrate"
7+
"go.mongodb.org/mongo-driver/bson"
8+
"go.mongodb.org/mongo-driver/mongo"
9+
"go.mongodb.org/mongo-driver/mongo/options"
10+
)
11+
12+
var migration_12 = migrate.Migration{
13+
Version: 12,
14+
Up: func(db *mongo.Database) error {
15+
mod := mongo.IndexModel{
16+
Keys: bson.D{{"tenant_id", 1}},
17+
Options: options.Index().SetName("tenant_id").SetUnique(true),
18+
}
19+
if _, err := db.Collection("namespaces").Indexes().CreateOne(context.TODO(), mod); err != nil {
20+
return err
21+
}
22+
mod = mongo.IndexModel{
23+
Keys: bson.D{{"name", 1}},
24+
Options: options.Index().SetName("name").SetUnique(true),
25+
}
26+
_, err := db.Collection("namespaces").Indexes().CreateOne(context.TODO(), mod)
27+
return err
28+
},
29+
Down: func(db *mongo.Database) error {
30+
_, err := db.Collection("namespaces").Indexes().DropOne(context.TODO(), "tenant_id")
31+
return err
32+
},
33+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package migrations
2+
3+
import (
4+
"context"
5+
6+
migrate "github.com/xakep666/mongo-migrate"
7+
"go.mongodb.org/mongo-driver/bson"
8+
"go.mongodb.org/mongo-driver/mongo"
9+
"go.mongodb.org/mongo-driver/mongo/options"
10+
)
11+
12+
var migration_13 = migrate.Migration{
13+
Version: 13,
14+
Up: func(db *mongo.Database) error {
15+
mod := mongo.IndexModel{
16+
Keys: bson.D{{"uid", 1}},
17+
Options: options.Index().SetName("uid").SetUnique(true),
18+
}
19+
_, err := db.Collection("devices").Indexes().CreateOne(context.TODO(), mod)
20+
if err != nil {
21+
return err
22+
}
23+
24+
mod = mongo.IndexModel{
25+
Keys: bson.D{{"last_seen", 1}},
26+
Options: options.Index().SetName("last_seen").SetExpireAfterSeconds(30),
27+
}
28+
_, err = db.Collection("connected_devices").Indexes().CreateOne(context.TODO(), mod)
29+
if err != nil {
30+
return err
31+
}
32+
33+
mod = mongo.IndexModel{
34+
Keys: bson.D{{"uid", 1}},
35+
Options: options.Index().SetName("uid").SetUnique(false),
36+
}
37+
_, err = db.Collection("connected_devices").Indexes().CreateOne(context.TODO(), mod)
38+
if err != nil {
39+
return err
40+
}
41+
42+
mod = mongo.IndexModel{
43+
Keys: bson.D{{"uid", 1}},
44+
Options: options.Index().SetName("uid").SetUnique(true),
45+
}
46+
_, err = db.Collection("sessions").Indexes().CreateOne(context.TODO(), mod)
47+
if err != nil {
48+
return err
49+
}
50+
51+
mod = mongo.IndexModel{
52+
Keys: bson.D{{"last_seen", 1}},
53+
Options: options.Index().SetName("last_seen").SetExpireAfterSeconds(30),
54+
}
55+
_, err = db.Collection("active_sessions").Indexes().CreateOne(context.TODO(), mod)
56+
if err != nil {
57+
return err
58+
}
59+
60+
mod = mongo.IndexModel{
61+
Keys: bson.D{{"uid", 1}},
62+
Options: options.Index().SetName("uid").SetUnique(false),
63+
}
64+
_, err = db.Collection("active_sessions").Indexes().CreateOne(context.TODO(), mod)
65+
if err != nil {
66+
return err
67+
}
68+
69+
mod = mongo.IndexModel{
70+
Keys: bson.D{{"username", 1}},
71+
Options: options.Index().SetName("username").SetUnique(true),
72+
}
73+
_, err = db.Collection("users").Indexes().CreateOne(context.TODO(), mod)
74+
if err != nil {
75+
return err
76+
}
77+
78+
mod = mongo.IndexModel{
79+
Keys: bson.D{{"tenant_id", 1}},
80+
Options: options.Index().SetName("tenant_id").SetUnique(true),
81+
}
82+
_, err = db.Collection("users").Indexes().CreateOne(context.TODO(), mod)
83+
if err != nil {
84+
return err
85+
}
86+
return nil
87+
},
88+
Down: func(db *mongo.Database) error {
89+
if _, err := db.Collection("devices").Indexes().DropOne(context.TODO(), "uid"); err != nil {
90+
return err
91+
}
92+
if _, err := db.Collection("connected_devices").Indexes().DropOne(context.TODO(), "last_seen"); err != nil {
93+
return err
94+
}
95+
if _, err := db.Collection("connected_devices").Indexes().DropOne(context.TODO(), "uid"); err != nil {
96+
return err
97+
}
98+
if _, err := db.Collection("sessions").Indexes().DropOne(context.TODO(), "uid"); err != nil {
99+
return err
100+
}
101+
if _, err := db.Collection("active_sessions").Indexes().DropOne(context.TODO(), "last_seen"); err != nil {
102+
return err
103+
}
104+
if _, err := db.Collection("users").Indexes().DropOne(context.TODO(), "username"); err != nil {
105+
return err
106+
}
107+
_, err := db.Collection("users").Indexes().DropOne(context.TODO(), "tenant_id")
108+
return err
109+
},
110+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package migrations
2+
3+
import (
4+
"context"
5+
6+
"github.com/shellhub-io/shellhub/pkg/models"
7+
migrate "github.com/xakep666/mongo-migrate"
8+
"go.mongodb.org/mongo-driver/bson"
9+
"go.mongodb.org/mongo-driver/mongo"
10+
)
11+
12+
var migration_14 = migrate.Migration{
13+
Version: 14,
14+
Up: func(db *mongo.Database) error {
15+
type user struct {
16+
Username string `json:"username" bson:",omitempty"`
17+
TenantID string `json:"tenant_id" bson:"tenant_id"`
18+
ID string `json:"id,omitempty" bson:"_id,omitempty"`
19+
SessionRecord bool `json:"session_record" bson:"session_record,omitempty"`
20+
}
21+
if _, err := db.Collection("users").Indexes().DropOne(context.TODO(), "tenant_id"); err != nil {
22+
return err
23+
}
24+
if _, err := db.Collection("users").Indexes().DropOne(context.TODO(), "session_record"); err != nil {
25+
return err
26+
}
27+
28+
cursor, err := db.Collection("users").Find(context.TODO(), bson.D{})
29+
if err != nil {
30+
return err
31+
}
32+
defer cursor.Close(context.TODO())
33+
for cursor.Next(context.TODO()) {
34+
user := new(user)
35+
err := cursor.Decode(&user)
36+
if err != nil {
37+
return err
38+
}
39+
settings := &models.NamespaceSettings{SessionRecord: true}
40+
namespace := &models.Namespace{
41+
Owner: user.ID,
42+
Members: []interface{}{user.ID},
43+
TenantID: user.TenantID,
44+
Name: user.Username,
45+
Settings: settings,
46+
}
47+
_, err = db.Collection("namespaces").InsertOne(context.TODO(), &namespace)
48+
if err != nil {
49+
return nil
50+
}
51+
52+
if _, err := db.Collection("users").UpdateOne(context.TODO(), bson.M{"tenant_id": user.TenantID}, bson.M{"$unset": bson.M{"tenant_id": ""}}); err != nil {
53+
return err
54+
}
55+
if _, err := db.Collection("users").UpdateOne(context.TODO(), bson.M{"tenant_id": user.TenantID}, bson.M{"$unset": bson.M{"session_record": ""}}); err != nil {
56+
return err
57+
}
58+
}
59+
60+
return cursor.Err()
61+
},
62+
Down: func(db *mongo.Database) error {
63+
cursor, err := db.Collection("namespaces").Find(context.TODO(), bson.D{})
64+
if err != nil {
65+
return err
66+
}
67+
defer cursor.Close(context.TODO())
68+
for cursor.Next(context.TODO()) {
69+
namespace := new(models.Namespace)
70+
err := cursor.Decode(&namespace)
71+
if err != nil {
72+
return err
73+
}
74+
75+
_, err = db.Collection("users").UpdateOne(context.TODO(), bson.M{"_id": namespace.Owner}, bson.M{"$set": bson.M{"tenant": namespace.TenantID}})
76+
if err != nil {
77+
return err
78+
}
79+
}
80+
81+
return err
82+
},
83+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package migrations
2+
3+
import (
4+
"context"
5+
6+
migrate "github.com/xakep666/mongo-migrate"
7+
"go.mongodb.org/mongo-driver/bson"
8+
"go.mongodb.org/mongo-driver/mongo"
9+
)
10+
11+
var migration_15 = migrate.Migration{
12+
Version: 15,
13+
Up: func(db *mongo.Database) error {
14+
_, err := db.Collection("namespaces").UpdateMany(context.TODO(), bson.M{}, []bson.M{
15+
{
16+
"$set": bson.M{
17+
"name": bson.M{"$toLower": "$name"},
18+
},
19+
},
20+
})
21+
return err
22+
},
23+
Down: func(db *mongo.Database) error {
24+
return nil
25+
},
26+
}

0 commit comments

Comments
 (0)