Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement flag metadata #3196

Merged
merged 14 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/migrations/cockroachdb/10_flag_metadata.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE flags ADD COLUMN metadata JSON AFTER enabled;
1 change: 1 addition & 0 deletions config/migrations/mysql/12_flag_metadata.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE flags ADD COLUMN metadata JSON AFTER enabled;
1 change: 1 addition & 0 deletions config/migrations/postgres/13_flag_metadata.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE flags ADD COLUMN metadata JSON AFTER enabled;
1 change: 1 addition & 0 deletions config/migrations/sqlite3/12_flag_metadata.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE flags ADD COLUMN metadata JSON AFTER enabled;
43 changes: 39 additions & 4 deletions internal/storage/sql/common/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@ func emptyAsNil(str string) *string {
// GetFlag gets a flag with variants by key
func (s *Store) GetFlag(ctx context.Context, p storage.ResourceRequest) (*flipt.Flag, error) {
var (
metadata sql.NullString
createdAt fliptsql.Timestamp
updatedAt fliptsql.Timestamp

flag = &flipt.Flag{}

err = s.builder.Select("namespace_key, \"key\", \"type\", name, description, enabled, created_at, updated_at").
err = s.builder.Select("namespace_key, \"key\", \"type\", name, description, enabled, metadata, created_at, updated_at").
From("flags").
Where(sq.Eq{"namespace_key": p.Namespace(), "\"key\"": p.Key}).
QueryRowContext(ctx).
Expand All @@ -52,8 +53,10 @@ func (s *Store) GetFlag(ctx context.Context, p storage.ResourceRequest) (*flipt.
&flag.Name,
&flag.Description,
&flag.Enabled,
&metadata,
&createdAt,
&updatedAt)
&updatedAt,
)
)

if err != nil {
Expand All @@ -67,6 +70,15 @@ func (s *Store) GetFlag(ctx context.Context, p storage.ResourceRequest) (*flipt.
flag.CreatedAt = createdAt.Timestamp
flag.UpdatedAt = updatedAt.Timestamp

if metadata.Valid {
compactedMetadata, err := compactJSONString(metadata.String)
if err != nil {
return flag, err
}

flag.Metadata = compactedMetadata
}

query := s.builder.Select("id, namespace_key, flag_key, \"key\", name, description, attachment, created_at, updated_at").
From("variants").
Where(sq.Eq{"namespace_key": flag.NamespaceKey, "flag_key": flag.Key}).
Expand Down Expand Up @@ -137,7 +149,7 @@ func (s *Store) ListFlags(ctx context.Context, req *storage.ListRequest[storage.
flags []*flipt.Flag
results = storage.ResultSet[*flipt.Flag]{}

query = s.builder.Select("namespace_key, \"key\", \"type\", name, description, enabled, created_at, updated_at").
query = s.builder.Select("namespace_key, \"key\", \"type\", name, description, enabled, metadata, created_at, updated_at").
From("flags").
Where(sq.Eq{"namespace_key": req.Predicate.Namespace()}).
OrderBy(fmt.Sprintf("created_at %s", req.QueryParams.Order))
Expand Down Expand Up @@ -179,6 +191,7 @@ func (s *Store) ListFlags(ctx context.Context, req *storage.ListRequest[storage.
var (
flag = &flipt.Flag{}

fMetadata sql.NullString
fCreatedAt fliptsql.Timestamp
fUpdatedAt fliptsql.Timestamp
)
Expand All @@ -190,6 +203,7 @@ func (s *Store) ListFlags(ctx context.Context, req *storage.ListRequest[storage.
&flag.Name,
&flag.Description,
&flag.Enabled,
&fMetadata,
&fCreatedAt,
&fUpdatedAt); err != nil {
return results, err
Expand All @@ -198,6 +212,15 @@ func (s *Store) ListFlags(ctx context.Context, req *storage.ListRequest[storage.
flag.CreatedAt = fCreatedAt.Timestamp
flag.UpdatedAt = fUpdatedAt.Timestamp

if fMetadata.Valid {
compactedMetadata, err := compactJSONString(fMetadata.String)
if err != nil {
return results, err
}

flag.Metadata = compactedMetadata
}

flags = append(flags, flag)
flagsByKey[flag.Key] = flag
}
Expand Down Expand Up @@ -328,27 +351,38 @@ func (s *Store) CreateFlag(ctx context.Context, r *flipt.CreateFlagRequest) (*fl
Name: r.Name,
Description: r.Description,
Enabled: r.Enabled,
Metadata: r.Metadata,
CreatedAt: now,
UpdatedAt: now,
}
)

metadata := emptyAsNil(r.Metadata)
if _, err := s.builder.Insert("flags").
Columns("namespace_key", "\"key\"", "\"type\"", "name", "description", "enabled", "created_at", "updated_at").
Columns("namespace_key", "\"key\"", "\"type\"", "name", "description", "enabled", "metadata", "created_at", "updated_at").
Values(
flag.NamespaceKey,
flag.Key,
flag.Type,
flag.Name,
flag.Description,
flag.Enabled,
metadata,
&fliptsql.Timestamp{Timestamp: flag.CreatedAt},
&fliptsql.Timestamp{Timestamp: flag.UpdatedAt},
).
ExecContext(ctx); err != nil {
return nil, err
}

if metadata != nil {
compactedMetadata, err := compactJSONString(*metadata)
if err != nil {
return nil, err
}
flag.Metadata = compactedMetadata
}

return flag, nil
}

Expand All @@ -362,6 +396,7 @@ func (s *Store) UpdateFlag(ctx context.Context, r *flipt.UpdateFlagRequest) (*fl
Set("name", r.Name).
Set("description", r.Description).
Set("enabled", r.Enabled).
Set("metadata", emptyAsNil(r.Metadata)).
Set("updated_at", &fliptsql.Timestamp{Timestamp: flipt.Now()}).
Where(sq.And{sq.Eq{"namespace_key": r.NamespaceKey}, sq.Eq{"\"key\"": r.Key}})

Expand Down
8 changes: 8 additions & 0 deletions internal/storage/sql/flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func (s *DBTestSuite) TestGetFlag() {
Name: "foo",
Description: "bar",
Enabled: true,
Metadata: `{"key":"value"}`,
})

require.NoError(t, err)
Expand All @@ -41,6 +42,7 @@ func (s *DBTestSuite) TestGetFlag() {
assert.Equal(t, flag.Name, got.Name)
assert.Equal(t, flag.Description, got.Description)
assert.Equal(t, flag.Enabled, got.Enabled)
assert.Equal(t, `{"key":"value"}`, flag.Metadata)
assert.NotZero(t, flag.CreatedAt)
assert.NotZero(t, flag.UpdatedAt)
}
Expand Down Expand Up @@ -458,6 +460,7 @@ func (s *DBTestSuite) TestCreateFlag() {
Name: "foo",
Description: "bar",
Enabled: true,
Metadata: `{"key":"value"}`,
})

require.NoError(t, err)
Expand All @@ -467,6 +470,7 @@ func (s *DBTestSuite) TestCreateFlag() {
assert.Equal(t, "foo", flag.Name)
assert.Equal(t, "bar", flag.Description)
assert.True(t, flag.Enabled)
assert.Equal(t, `{"key":"value"}`, flag.Metadata)
assert.NotZero(t, flag.CreatedAt)
assert.Equal(t, flag.CreatedAt.Seconds, flag.UpdatedAt.Seconds)
}
Expand Down Expand Up @@ -547,6 +551,7 @@ func (s *DBTestSuite) TestUpdateFlag() {
Name: "foo",
Description: "bar",
Enabled: true,
Metadata: `{"key":"value"}`,
})

require.NoError(t, err)
Expand All @@ -556,6 +561,7 @@ func (s *DBTestSuite) TestUpdateFlag() {
assert.Equal(t, "foo", flag.Name)
assert.Equal(t, "bar", flag.Description)
assert.True(t, flag.Enabled)
assert.Equal(t, `{"key":"value"}`, flag.Metadata)
assert.NotZero(t, flag.CreatedAt)
assert.Equal(t, flag.CreatedAt.Seconds, flag.UpdatedAt.Seconds)

Expand All @@ -564,6 +570,7 @@ func (s *DBTestSuite) TestUpdateFlag() {
Name: flag.Name,
Description: "foobar",
Enabled: true,
Metadata: `{"key":"value", "foo": "bar"}`,
})

require.NoError(t, err)
Expand All @@ -573,6 +580,7 @@ func (s *DBTestSuite) TestUpdateFlag() {
assert.Equal(t, flag.Name, updated.Name)
assert.Equal(t, "foobar", updated.Description)
assert.True(t, flag.Enabled)
assert.Equal(t, `{"key":"value","foo":"bar"}`, updated.Metadata)
assert.NotZero(t, updated.CreatedAt)
assert.NotZero(t, updated.UpdatedAt)
}
Expand Down
10 changes: 5 additions & 5 deletions internal/storage/sql/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ import (
)

var expectedVersions = map[Driver]uint{
SQLite: 11,
LibSQL: 11, // libsql driver uses the same migrations as sqlite3
Postgres: 12,
MySQL: 11,
CockroachDB: 9,
SQLite: 12,
LibSQL: 12, // libsql driver uses the same migrations as sqlite3
Postgres: 13,
MySQL: 12,
CockroachDB: 10,
Clickhouse: 3,
}

Expand Down
8 changes: 4 additions & 4 deletions rpc/flipt/analytics/analytics.pb.go

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

Loading
Loading