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 16, 2024
1 parent 542130b commit dd95253
Show file tree
Hide file tree
Showing 4 changed files with 158 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 @@ -95,6 +95,7 @@ func GenerateMigrations() []migrate.Migration {
migration83,
migration84,
migration85,
migration86,
}
}

Expand Down
82 changes: 82 additions & 0 deletions api/store/mongo/migrations/migration_86.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 migration86 = migrate.Migration{
Version: 86,
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": 86,
"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_86_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 TestMigration86(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()[86 - 1])
require.NoError(tt, migrates.Up(context.Background(), migrate.AllAvailable))

test.setup(tt)

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

type Tags struct {
ID string `json:"_id" bson:"_id"`
Name string `json:"name" bson:"name"`
Color string `json:"color" bson:"color"`
Tenant string `json:"tenant_id" bson:"tenant_id"`
}

0 comments on commit dd95253

Please sign in to comment.