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 Nov 27, 2024
1 parent 74ffbed commit f8e40e0
Show file tree
Hide file tree
Showing 4 changed files with 124 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
53 changes: 53 additions & 0 deletions api/store/mongo/migrations/migration_83.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
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"
)

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
}

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

_, err := db.Collection("tags").Indexes().CreateOne(ctx, index)
if err != nil {
return err
}

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").Indexes().DropOne(ctx, "names")
if err != nil {
return err
}

return db.Collection("tags").Drop(ctx)
}),
}
64 changes: 64 additions & 0 deletions api/store/mongo/migrations/migration_83_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
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",
})
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)
},
},
}

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)
})
}
}
6 changes: 6 additions & 0 deletions pkg/models/tags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package models

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

0 comments on commit f8e40e0

Please sign in to comment.