Skip to content

Commit

Permalink
Extend variant to support attachment
Browse files Browse the repository at this point in the history
* Extend flipt.proto to support attachment in variant
* Extend DB with the new column
* Extend UI to support the new field

Co-authored-by: Kevin Ip <kevin.ip@paradigm.co>
  • Loading branch information
amayvs and kevin-ip committed Feb 2, 2022
1 parent f041dc2 commit 85e878b
Show file tree
Hide file tree
Showing 16 changed files with 121 additions and 10 deletions.
2 changes: 2 additions & 0 deletions cmd/flipt/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type Variant struct {
Key string `yaml:"key,omitempty"`
Name string `yaml:"name,omitempty"`
Description string `yaml:"description,omitempty"`
Attachment []byte `yaml:"attachment,omitempty"`
}

type Rule struct {
Expand Down Expand Up @@ -149,6 +150,7 @@ func runExport(_ []string) error {
Key: v.Key,
Name: v.Name,
Description: v.Description,
Attachment: v.Attachment,
})

variantKeys[v.Id] = v.Key
Expand Down
1 change: 1 addition & 0 deletions cmd/flipt/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ func runImport(args []string) error {
Key: v.Key,
Name: v.Name,
Description: v.Description,
Attachment: v.Attachment,
})

if err != nil {
Expand Down
1 change: 1 addition & 0 deletions config/migrations/mysql/1_variants_attachment.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE variants DROP COLUMN attachment;
1 change: 1 addition & 0 deletions config/migrations/mysql/1_variants_attachment.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE variants ADD COLUMN attachment MEDIUMBLOB AFTER description;
1 change: 1 addition & 0 deletions config/migrations/postgres/3_variants_attachment.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE variants DROP COLUMN attachment;
1 change: 1 addition & 0 deletions config/migrations/postgres/3_variants_attachment.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE variants ADD attachment BYTEA;
1 change: 1 addition & 0 deletions config/migrations/sqlite3/3_variants_attachment.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE variants DROP COLUMN attachment;
1 change: 1 addition & 0 deletions config/migrations/sqlite3/3_variants_attachment.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE variants ADD COLUMN attachment BLOB AFTER description;
40 changes: 35 additions & 5 deletions rpc/flipt/flipt.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions rpc/flipt/flipt.proto
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ message Variant {
string description = 5;
google.protobuf.Timestamp created_at = 6;
google.protobuf.Timestamp updated_at = 7;
bytes attachment = 8;
}

message CreateVariantRequest {
Expand All @@ -156,6 +157,7 @@ message CreateVariantRequest {
string key = 2;
string name = 3;
string description = 4;
bytes attachment = 5;
}

message UpdateVariantRequest {
Expand All @@ -170,6 +172,7 @@ message UpdateVariantRequest {
string key = 3;
string name = 4;
string description = 5;
bytes attachment = 6;
}

message DeleteVariantRequest {
Expand Down
26 changes: 26 additions & 0 deletions rpc/flipt/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ func TestValidate_CreateVariantRequest(t *testing.T) {
Key: "key",
Name: "name",
Description: "desc",
Attachment: []byte("attachment"),
},
wantErr: errors.EmptyFieldError("flagKey"),
},
Expand All @@ -239,6 +240,7 @@ func TestValidate_CreateVariantRequest(t *testing.T) {
Key: "",
Name: "name",
Description: "desc",
Attachment: []byte("attachment"),
},
wantErr: errors.EmptyFieldError("key"),
},
Expand All @@ -251,6 +253,16 @@ func TestValidate_CreateVariantRequest(t *testing.T) {
Description: "desc",
},
},
{
name: "validWithAttachment",
req: &CreateVariantRequest{
FlagKey: "flagKey",
Key: "key",
Name: "name",
Description: "desc",
Attachment: []byte("attachment"),
},
},
}

for _, tt := range tests {
Expand Down Expand Up @@ -280,6 +292,7 @@ func TestValidate_UpdateVariantRequest(t *testing.T) {
Key: "key",
Name: "name",
Description: "desc",
Attachment: []byte("attachment"),
},
wantErr: errors.EmptyFieldError("id"),
},
Expand All @@ -291,6 +304,7 @@ func TestValidate_UpdateVariantRequest(t *testing.T) {
Key: "key",
Name: "name",
Description: "desc",
Attachment: []byte("attachment"),
},
wantErr: errors.EmptyFieldError("flagKey"),
},
Expand All @@ -302,6 +316,7 @@ func TestValidate_UpdateVariantRequest(t *testing.T) {
Key: "",
Name: "name",
Description: "desc",
Attachment: []byte("attachment"),
},
wantErr: errors.EmptyFieldError("key"),
},
Expand All @@ -315,6 +330,17 @@ func TestValidate_UpdateVariantRequest(t *testing.T) {
Description: "desc",
},
},
{
name: "validWithAttachment",
req: &UpdateVariantRequest{
Id: "id",
FlagKey: "flagKey",
Key: "key",
Name: "name",
Description: "desc",
Attachment: []byte("attachment"),
},
},
}

for _, tt := range tests {
Expand Down
4 changes: 4 additions & 0 deletions server/flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ func TestCreateVariant(t *testing.T) {
Key: "key",
Name: "name",
Description: "desc",
Attachment: []byte("attachment"),
}
)

Expand All @@ -149,6 +150,7 @@ func TestCreateVariant(t *testing.T) {
Key: req.Key,
Name: req.Name,
Description: req.Description,
Attachment: req.Attachment,
}, nil)

got, err := s.CreateVariant(context.TODO(), req)
Expand All @@ -170,6 +172,7 @@ func TestUpdateVariant(t *testing.T) {
Key: "key",
Name: "name",
Description: "desc",
Attachment: []byte("attachment"),
}
)

Expand All @@ -179,6 +182,7 @@ func TestUpdateVariant(t *testing.T) {
Key: req.Key,
Name: req.Name,
Description: req.Description,
Attachment: req.Attachment,
}, nil)

got, err := s.UpdateVariant(context.TODO(), req)
Expand Down
13 changes: 8 additions & 5 deletions storage/sql/common/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,14 +187,15 @@ func (s *Store) CreateVariant(ctx context.Context, r *flipt.CreateVariantRequest
Key: r.Key,
Name: r.Name,
Description: r.Description,
Attachment: r.Attachment,
CreatedAt: now,
UpdatedAt: now,
}
)

if _, err := s.builder.Insert("variants").
Columns("id", "flag_key", "\"key\"", "name", "description", "created_at", "updated_at").
Values(v.Id, v.FlagKey, v.Key, v.Name, v.Description, &timestamp{v.CreatedAt}, &timestamp{v.UpdatedAt}).
Columns("id", "flag_key", "\"key\"", "name", "description", "attachment", "created_at", "updated_at").
Values(v.Id, v.FlagKey, v.Key, v.Name, v.Description, v.Attachment, &timestamp{v.CreatedAt}, &timestamp{v.UpdatedAt}).
ExecContext(ctx); err != nil {
return nil, err
}
Expand All @@ -208,6 +209,7 @@ func (s *Store) UpdateVariant(ctx context.Context, r *flipt.UpdateVariantRequest
Set("\"key\"", r.Key).
Set("name", r.Name).
Set("description", r.Description).
Set("attachment", r.Attachment).
Set("updated_at", &timestamp{timestamppb.Now()}).
Where(sq.And{sq.Eq{"id": r.Id}, sq.Eq{"flag_key": r.FlagKey}})

Expand All @@ -232,11 +234,11 @@ func (s *Store) UpdateVariant(ctx context.Context, r *flipt.UpdateVariantRequest
v = &flipt.Variant{}
)

if err := s.builder.Select("id, \"key\", flag_key, name, description, created_at, updated_at").
if err := s.builder.Select("id, \"key\", flag_key, name, description, attachment, created_at, updated_at").
From("variants").
Where(sq.And{sq.Eq{"id": r.Id}, sq.Eq{"flag_key": r.FlagKey}}).
QueryRowContext(ctx).
Scan(&v.Id, &v.Key, &v.FlagKey, &v.Name, &v.Description, &createdAt, &updatedAt); err != nil {
Scan(&v.Id, &v.Key, &v.FlagKey, &v.Name, &v.Description, &v.Attachment, &createdAt, &updatedAt); err != nil {
return nil, err
}

Expand All @@ -256,7 +258,7 @@ func (s *Store) DeleteVariant(ctx context.Context, r *flipt.DeleteVariantRequest
}

func (s *Store) variants(ctx context.Context, flag *flipt.Flag) (err error) {
query := s.builder.Select("id, flag_key, \"key\", name, description, created_at, updated_at").
query := s.builder.Select("id, flag_key, \"key\", name, description, attachment, created_at, updated_at").
From("variants").
Where(sq.Eq{"flag_key": flag.Key}).
OrderBy("created_at ASC")
Expand Down Expand Up @@ -284,6 +286,7 @@ func (s *Store) variants(ctx context.Context, flag *flipt.Flag) (err error) {
&variant.Key,
&variant.Name,
&variant.Description,
&variant.Attachment,
&createdAt,
&updatedAt); err != nil {
return err
Expand Down
Loading

0 comments on commit 85e878b

Please sign in to comment.