Skip to content

Commit 5d2bd82

Browse files
committed
feat: implement backend support for tag colors, issue #4350
1 parent 74ffbed commit 5d2bd82

File tree

4 files changed

+157
-0
lines changed

4 files changed

+157
-0
lines changed

api/store/mongo/migrations/main.go

+1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ func GenerateMigrations() []migrate.Migration {
9292
migration80,
9393
migration81,
9494
migration82,
95+
migration83,
9596
}
9697
}
9798

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package migrations
2+
3+
import (
4+
"context"
5+
6+
"github.com/sirupsen/logrus"
7+
migrate "github.com/xakep666/mongo-migrate"
8+
"go.mongodb.org/mongo-driver/bson"
9+
"go.mongodb.org/mongo-driver/mongo"
10+
"go.mongodb.org/mongo-driver/mongo/options"
11+
"go.mongodb.org/mongo-driver/mongo/writeconcern"
12+
)
13+
14+
var migration83 = migrate.Migration{
15+
Version: 83,
16+
Description: "Creating Tag collection.",
17+
Up: migrate.MigrationFunc(func(ctx context.Context, db *mongo.Database) error {
18+
logrus.WithFields(logrus.Fields{
19+
"component": "migration",
20+
"version": 83,
21+
"action": "Up",
22+
}).Info("Applying migration")
23+
24+
if err := db.CreateCollection(ctx, "tags"); err != nil {
25+
return err
26+
}
27+
28+
indexName := mongo.IndexModel{
29+
Keys: bson.D{{Key: "name", Value: 1}},
30+
Options: options.Index().SetName("name").SetUnique(false),
31+
}
32+
33+
_, err := db.Collection("tags",
34+
options.Collection().SetWriteConcern(writeconcern.Majority()),
35+
).Indexes().CreateOne(ctx, indexName)
36+
if err != nil {
37+
return err
38+
}
39+
40+
indexTenant := mongo.IndexModel{
41+
Keys: bson.D{{Key: "tenant_id", Value: 1}},
42+
Options: options.Index().SetName("tenant_id").SetUnique(false),
43+
}
44+
45+
46+
_, err2 := db.Collection("tags",
47+
options.Collection().SetWriteConcern(writeconcern.Majority()),
48+
).Indexes().CreateOne(ctx, indexTenant)
49+
if err2 != nil {
50+
return err2
51+
}
52+
53+
return nil
54+
}),
55+
Down: migrate.MigrationFunc(func(ctx context.Context, db *mongo.Database) error {
56+
logrus.WithFields(logrus.Fields{
57+
"component": "migration",
58+
"version": 83,
59+
"action": "Down",
60+
}).Info("Reverting migration")
61+
62+
_, err := db.Collection("tags",
63+
options.Collection().SetWriteConcern(writeconcern.Majority()),
64+
).Indexes().DropOne(ctx, "names")
65+
66+
if err != nil {
67+
return err
68+
}
69+
70+
_, err2 := db.Collection("tags",
71+
options.Collection().SetWriteConcern(writeconcern.Majority()),
72+
).Indexes().DropOne(ctx, "tenant_id")
73+
74+
if err2 != nil {
75+
return err
76+
}
77+
78+
return db.Collection("tags",
79+
options.Collection().SetWriteConcern(writeconcern.Majority()),
80+
).Drop(ctx)
81+
}),
82+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package migrations
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/shellhub-io/shellhub/pkg/envs"
8+
envmock "github.com/shellhub-io/shellhub/pkg/envs/mocks"
9+
"github.com/shellhub-io/shellhub/pkg/models"
10+
"github.com/stretchr/testify/assert"
11+
"github.com/stretchr/testify/require"
12+
migrate "github.com/xakep666/mongo-migrate"
13+
"go.mongodb.org/mongo-driver/bson"
14+
)
15+
16+
func TestMigration83(t *testing.T) {
17+
ctx := context.Background()
18+
19+
mock := &envmock.Backend{}
20+
envs.DefaultBackend = mock
21+
22+
tests := []struct {
23+
description string
24+
setup func(t *testing.T)
25+
run func(t *testing.T)
26+
}{
27+
{
28+
description: "Apply up on migration 83 when there is at least one user",
29+
setup: func(t *testing.T) {
30+
_, err := c.Database("test").Collection("tags").InsertOne(ctx, models.Tags{
31+
Name: "red",
32+
Color: "#ff0000",
33+
Tenant: "00000000-0000-4000-0000-000000000000",
34+
})
35+
require.NoError(t, err)
36+
},
37+
run: func(t *testing.T) {
38+
result := c.Database("test").Collection("tags").FindOne(ctx, bson.M{})
39+
require.NoError(t, result.Err())
40+
41+
var tags models.Tags
42+
43+
err := result.Decode(&tags)
44+
require.NoError(t, err)
45+
46+
assert.Equal(t, "#ff0000", tags.Color)
47+
assert.Equal(t, "red", tags.Name)
48+
assert.Equal(t, "00000000-0000-4000-0000-000000000000", tags.Tenant)
49+
},
50+
},
51+
}
52+
53+
for _, test := range tests {
54+
t.Run(test.description, func(tt *testing.T) {
55+
tt.Cleanup(func() {
56+
assert.NoError(tt, srv.Reset())
57+
})
58+
59+
migrates := migrate.NewMigrate(c.Database("test"), GenerateMigrations()[83 - 1])
60+
require.NoError(tt, migrates.Up(context.Background(), migrate.AllAvailable))
61+
62+
test.setup(tt)
63+
64+
test.run(tt)
65+
})
66+
}
67+
}

pkg/models/tags.go

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package models
2+
3+
type Tags struct {
4+
Name string `json:name`
5+
Color string `json:color`
6+
Tenant string `json:tenant_id`
7+
}

0 commit comments

Comments
 (0)