-
-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement backend support for tag colors, issue #4350
- Loading branch information
Showing
4 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |