Skip to content

Commit

Permalink
feat: implement backend support for tag colors, issue #4350
Browse files Browse the repository at this point in the history
  • Loading branch information
haller33 committed Dec 5, 2024
1 parent 74ffbed commit 5d2bd82
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 0 deletions.
1 change: 1 addition & 0 deletions api/store/mongo/migrations/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ func GenerateMigrations() []migrate.Migration {
migration80,
migration81,
migration82,
migration83,
}
}

Expand Down
82 changes: 82 additions & 0 deletions api/store/mongo/migrations/migration_83.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package migrations

import (
"context"

"github.com/sirupsen/logrus"
migrate "github.com/xakep666/mongo-migrate"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/writeconcern"
)

var migration83 = migrate.Migration{
Version: 83,
Description: "Creating Tag collection.",
Up: migrate.MigrationFunc(func(ctx context.Context, db *mongo.Database) error {
logrus.WithFields(logrus.Fields{
"component": "migration",
"version": 83,
"action": "Up",
}).Info("Applying migration")

if err := db.CreateCollection(ctx, "tags"); err != nil {
return err
}

indexName := mongo.IndexModel{
Keys: bson.D{{Key: "name", Value: 1}},
Options: options.Index().SetName("name").SetUnique(false),
}

_, err := db.Collection("tags",
options.Collection().SetWriteConcern(writeconcern.Majority()),
).Indexes().CreateOne(ctx, indexName)
if err != nil {
return err
}

indexTenant := mongo.IndexModel{
Keys: bson.D{{Key: "tenant_id", Value: 1}},
Options: options.Index().SetName("tenant_id").SetUnique(false),
}


_, err2 := db.Collection("tags",
options.Collection().SetWriteConcern(writeconcern.Majority()),
).Indexes().CreateOne(ctx, indexTenant)
if err2 != nil {
return err2
}

return nil
}),
Down: migrate.MigrationFunc(func(ctx context.Context, db *mongo.Database) error {
logrus.WithFields(logrus.Fields{
"component": "migration",
"version": 83,
"action": "Down",
}).Info("Reverting migration")

_, err := db.Collection("tags",
options.Collection().SetWriteConcern(writeconcern.Majority()),
).Indexes().DropOne(ctx, "names")

if err != nil {
return err
}

_, err2 := db.Collection("tags",
options.Collection().SetWriteConcern(writeconcern.Majority()),
).Indexes().DropOne(ctx, "tenant_id")

if err2 != nil {
return err
}

return db.Collection("tags",
options.Collection().SetWriteConcern(writeconcern.Majority()),
).Drop(ctx)
}),
}
67 changes: 67 additions & 0 deletions api/store/mongo/migrations/migration_83_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package migrations

import (
"context"
"testing"

"github.com/shellhub-io/shellhub/pkg/envs"
envmock "github.com/shellhub-io/shellhub/pkg/envs/mocks"
"github.com/shellhub-io/shellhub/pkg/models"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
migrate "github.com/xakep666/mongo-migrate"
"go.mongodb.org/mongo-driver/bson"
)

func TestMigration83(t *testing.T) {
ctx := context.Background()

mock := &envmock.Backend{}
envs.DefaultBackend = mock

tests := []struct {
description string
setup func(t *testing.T)
run func(t *testing.T)
}{
{
description: "Apply up on migration 83 when there is at least one user",
setup: func(t *testing.T) {
_, err := c.Database("test").Collection("tags").InsertOne(ctx, models.Tags{
Name: "red",
Color: "#ff0000",
Tenant: "00000000-0000-4000-0000-000000000000",
})
require.NoError(t, err)
},
run: func(t *testing.T) {
result := c.Database("test").Collection("tags").FindOne(ctx, bson.M{})
require.NoError(t, result.Err())

var tags models.Tags

err := result.Decode(&tags)
require.NoError(t, err)

assert.Equal(t, "#ff0000", tags.Color)
assert.Equal(t, "red", tags.Name)
assert.Equal(t, "00000000-0000-4000-0000-000000000000", tags.Tenant)
},
},
}

for _, test := range tests {
t.Run(test.description, func(tt *testing.T) {
tt.Cleanup(func() {
assert.NoError(tt, srv.Reset())
})

migrates := migrate.NewMigrate(c.Database("test"), GenerateMigrations()[83 - 1])
require.NoError(tt, migrates.Up(context.Background(), migrate.AllAvailable))

test.setup(tt)

test.run(tt)
})
}
}
7 changes: 7 additions & 0 deletions pkg/models/tags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package models

type Tags struct {
Name string `json:name`
Color string `json:color`
Tenant string `json:tenant_id`
}

0 comments on commit 5d2bd82

Please sign in to comment.