diff --git a/api/store/mongo/migrations/main.go b/api/store/mongo/migrations/main.go index 7a320b8acb4..a09f6316651 100644 --- a/api/store/mongo/migrations/main.go +++ b/api/store/mongo/migrations/main.go @@ -92,6 +92,7 @@ func GenerateMigrations() []migrate.Migration { migration80, migration81, migration82, + migration83, } } diff --git a/api/store/mongo/migrations/migration_83.go b/api/store/mongo/migrations/migration_83.go new file mode 100644 index 00000000000..4fc89db5687 --- /dev/null +++ b/api/store/mongo/migrations/migration_83.go @@ -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) + }), +} diff --git a/api/store/mongo/migrations/migration_83_test.go b/api/store/mongo/migrations/migration_83_test.go new file mode 100644 index 00000000000..f71ec57abfe --- /dev/null +++ b/api/store/mongo/migrations/migration_83_test.go @@ -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) + }) + } +} diff --git a/pkg/models/tags.go b/pkg/models/tags.go new file mode 100644 index 00000000000..83c90ef6156 --- /dev/null +++ b/pkg/models/tags.go @@ -0,0 +1,7 @@ +package models + +type Tags struct { + Name string `json:name` + Color string `json:color` + Tenant string `json:tenant_id` +}