Repository: oom-ai/oomstore
Pull request #846 (2021-12-29 08:06:19)

Jinghan/update revision's  `snapshot_table`
This PR modifies metadata method `UpdateRevision`: able to update `snapshot_table`.

This feature will be used for creating snapshot tables for streaming features.


related to #805 



Patch:
diff --git a/internal/database/metadata/sqlutil/revision.go b/internal/database/metadata/sqlutil/revision.go
index 7bd31fb3..0c81658c 100644
--- a/internal/database/metadata/sqlutil/revision.go
+++ b/internal/database/metadata/sqlutil/revision.go
@@ -22,6 +22,9 @@ func UpdateRevision(ctx context.Context, sqlxCtx metadata.SqlxContext, opt metad
 	if opt.NewAnchored != nil {
 		and["anchored"] = *opt.NewAnchored
 	}
+	if opt.NewSnapshotTable != nil {
+		and["snapshot_table"] = *opt.NewSnapshotTable
+	}
 	cond, args, err := dbutil.BuildConditions(and, nil)
 	if err != nil {
 		return err
diff --git a/internal/database/metadata/types.go b/internal/database/metadata/types.go
index a07c8fe0..657d69d8 100644
--- a/internal/database/metadata/types.go
+++ b/internal/database/metadata/types.go
@@ -48,9 +48,10 @@ type UpdateGroupOpt struct {
 }
 
 type UpdateRevisionOpt struct {
-	RevisionID  int
-	NewRevision *int64
-	NewAnchored *bool
+	RevisionID       int
+	NewRevision      *int64
+	NewAnchored      *bool
+	NewSnapshotTable *string
 }
 
 type ListFeatureOpt struct {


Test patch:
diff --git a/internal/database/metadata/test_impl/revision.go b/internal/database/metadata/test_impl/revision.go
index 8d977020..fbea169f 100644
--- a/internal/database/metadata/test_impl/revision.go
+++ b/internal/database/metadata/test_impl/revision.go
@@ -102,6 +102,8 @@ func TestUpdateRevision(t *testing.T, prepareStore PrepareStoreFn, destroyStore
 	defer store.Close()
 
 	_, groupID := prepareEntityAndGroup(t, ctx, store)
+	group, err := store.GetGroup(ctx, groupID)
+	require.NoError(t, err)
 	revisionID, _, err := store.CreateRevision(ctx, metadata.CreateRevisionOpt{
 		Revision:      1000,
 		GroupID:       groupID,
@@ -111,9 +113,10 @@ func TestUpdateRevision(t *testing.T, prepareStore PrepareStoreFn, destroyStore
 	require.NoError(t, err)
 
 	testCases := []struct {
-		description string
-		opt         metadata.UpdateRevisionOpt
-		expected    error
+		description      string
+		opt              metadata.UpdateRevisionOpt
+		expected         error
+		expectedRevision *types.Revision
 	}{
 		{
 			description: "update revision successfully",
@@ -122,6 +125,30 @@ func TestUpdateRevision(t *testing.T, prepareStore PrepareStoreFn, destroyStore
 				NewAnchored: boolPtr(true),
 			},
 			expected: nil,
+			expectedRevision: &types.Revision{
+				ID:            revisionID,
+				GroupID:       groupID,
+				Revision:      1000,
+				SnapshotTable: "device_info_1000",
+				Anchored:      true,
+				Group:         group,
+			},
+		},
+		{
+			description: "update revision snapshot_table successfully",
+			opt: metadata.UpdateRevisionOpt{
+				RevisionID:       revisionID,
+				NewSnapshotTable: stringPtr("new_table"),
+			},
+			expected: nil,
+			expectedRevision: &types.Revision{
+				ID:            revisionID,
+				GroupID:       groupID,
+				Revision:      1000,
+				SnapshotTable: "new_table",
+				Anchored:      true,
+				Group:         group,
+			},
 		},
 		{
 			description: "cannot update revision, return err",
@@ -137,9 +164,13 @@ func TestUpdateRevision(t *testing.T, prepareStore PrepareStoreFn, destroyStore
 		t.Run(tc.description, func(t *testing.T) {
 			actual := store.UpdateRevision(ctx, tc.opt)
 			if tc.expected == nil {
-				require.NoError(t, actual)
+				assert.NoError(t, actual)
+				actualRevision, err := store.GetRevision(ctx, revisionID)
+				assert.NoError(t, err)
+				ignoreCreateAndModifyTime(actualRevision)
+				assert.Equal(t, tc.expectedRevision, actualRevision)
 			} else {
-				require.EqualError(t, actual, tc.expected.Error())
+				assert.EqualError(t, actual, tc.expected.Error())
 			}
 		})
 	}