Repository: oom-ai/oomstore
Pull request #1112 (2022-01-27 10:03:47)

refactor: shorter dummy revision description
Since it is not clear either


Patch:
diff --git a/internal/database/metadata/mock_metadata/store.go b/internal/database/metadata/mock_metadata/store.go
index ef0778ee..4f9f81ff 100644
--- a/internal/database/metadata/mock_metadata/store.go
+++ b/internal/database/metadata/mock_metadata/store.go
@@ -97,12 +97,13 @@ func (mr *MockStoreMockRecorder) CreateGroup(ctx, opt interface{}) *gomock.Call
 }
 
 // CreateRevision mocks base method.
-func (m *MockStore) CreateRevision(ctx context.Context, opt metadata.CreateRevisionOpt) (int, error) {
+func (m *MockStore) CreateRevision(ctx context.Context, opt metadata.CreateRevisionOpt) (int, string, error) {
 	m.ctrl.T.Helper()
 	ret := m.ctrl.Call(m, "CreateRevision", ctx, opt)
 	ret0, _ := ret[0].(int)
-	ret1, _ := ret[1].(error)
-	return ret0, ret1
+	ret1, _ := ret[1].(string)
+	ret2, _ := ret[2].(error)
+	return ret0, ret1, ret2
 }
 
 // CreateRevision indicates an expected call of CreateRevision.
@@ -487,12 +488,13 @@ func (mr *MockDBStoreMockRecorder) CreateGroup(ctx, opt interface{}) *gomock.Cal
 }
 
 // CreateRevision mocks base method.
-func (m *MockDBStore) CreateRevision(ctx context.Context, opt metadata.CreateRevisionOpt) (int, error) {
+func (m *MockDBStore) CreateRevision(ctx context.Context, opt metadata.CreateRevisionOpt) (int, string, error) {
 	m.ctrl.T.Helper()
 	ret := m.ctrl.Call(m, "CreateRevision", ctx, opt)
 	ret0, _ := ret[0].(int)
-	ret1, _ := ret[1].(error)
-	return ret0, ret1
+	ret1, _ := ret[1].(string)
+	ret2, _ := ret[2].(error)
+	return ret0, ret1, ret2
 }
 
 // CreateRevision indicates an expected call of CreateRevision.
diff --git a/internal/database/metadata/mysql/db.go b/internal/database/metadata/mysql/db.go
index 596d3489..7bffa8d6 100644
--- a/internal/database/metadata/mysql/db.go
+++ b/internal/database/metadata/mysql/db.go
@@ -104,16 +104,17 @@ func (db *DB) ListGroup(ctx context.Context, entityID *int, groupIDs *[]int) (ty
 	return sqlutil.ListGroup(ctx, db, entityID, groupIDs)
 }
 
-func (db *DB) CreateRevision(ctx context.Context, opt metadata.CreateRevisionOpt) (int, error) {
+func (db *DB) CreateRevision(ctx context.Context, opt metadata.CreateRevisionOpt) (int, string, error) {
 	var (
-		revisionID int
-		err        error
+		revisionID    int
+		snapshotTable string
+		err           error
 	)
 	err = db.WithTransaction(ctx, func(c context.Context, tx metadata.DBStore) error {
-		revisionID, err = tx.CreateRevision(c, opt)
+		revisionID, snapshotTable, err = tx.CreateRevision(c, opt)
 		return err
 	})
-	return revisionID, err
+	return revisionID, snapshotTable, err
 }
 
 func (db *DB) UpdateRevision(ctx context.Context, opt metadata.UpdateRevisionOpt) error {
diff --git a/internal/database/metadata/mysql/revision.go b/internal/database/metadata/mysql/revision.go
index 79482b97..898b024e 100644
--- a/internal/database/metadata/mysql/revision.go
+++ b/internal/database/metadata/mysql/revision.go
@@ -9,7 +9,7 @@ import (
 	"github.com/oom-ai/oomstore/internal/database/metadata"
 )
 
-func createRevision(ctx context.Context, sqlxCtx metadata.SqlxContext, opt metadata.CreateRevisionOpt) (int, error) {
+func createRevision(ctx context.Context, sqlxCtx metadata.SqlxContext, opt metadata.CreateRevisionOpt) (int, string, error) {
 	var snapshotTable, cdcTable string
 	if opt.SnapshotTable != nil {
 		snapshotTable = *opt.SnapshotTable
@@ -23,15 +23,15 @@ func createRevision(ctx context.Context, sqlxCtx metadata.SqlxContext, opt metad
 	if err != nil {
 		if er, ok := err.(*mysql.MySQLError); ok {
 			if er.Number == ER_DUP_ENTRY {
-				return 0, errdefs.Errorf("revision already exists: groupID=%d, revision=%d", opt.GroupID, opt.Revision)
+				return 0, "", errdefs.Errorf("revision already exists: groupID=%d, revision=%d", opt.GroupID, opt.Revision)
 			}
 		}
-		return 0, errdefs.WithStack(err)
+		return 0, "", errdefs.WithStack(err)
 	}
 	revisionID, err := res.LastInsertId()
 	if err != nil {
-		return 0, errdefs.WithStack(err)
+		return 0, "", errdefs.WithStack(err)
 	}
 
-	return int(revisionID), nil
+	return int(revisionID), snapshotTable, nil
 }
diff --git a/internal/database/metadata/mysql/tx.go b/internal/database/metadata/mysql/tx.go
index dcd0a012..81b0a0bb 100644
--- a/internal/database/metadata/mysql/tx.go
+++ b/internal/database/metadata/mysql/tx.go
@@ -72,7 +72,7 @@ func (tx *Tx) GetFeatureByName(ctx context.Context, groupName string, featureNam
 	return sqlutil.GetFeatureByName(ctx, tx, groupName, featureName)
 }
 
-func (tx *Tx) CreateRevision(ctx context.Context, opt metadata.CreateRevisionOpt) (int, error) {
+func (tx *Tx) CreateRevision(ctx context.Context, opt metadata.CreateRevisionOpt) (int, string, error) {
 	return createRevision(ctx, tx, opt)
 }
 
diff --git a/internal/database/metadata/postgres/db.go b/internal/database/metadata/postgres/db.go
index e350e047..9c5179b0 100644
--- a/internal/database/metadata/postgres/db.go
+++ b/internal/database/metadata/postgres/db.go
@@ -93,16 +93,17 @@ func (db *DB) GetFeatureByName(ctx context.Context, groupName string, featureNam
 	return sqlutil.GetFeatureByName(ctx, db, groupName, featureName)
 }
 
-func (db *DB) CreateRevision(ctx context.Context, opt metadata.CreateRevisionOpt) (int, error) {
+func (db *DB) CreateRevision(ctx context.Context, opt metadata.CreateRevisionOpt) (int, string, error) {
 	var (
-		revisionID int
-		err        error
+		revisionID    int
+		snapshotTable string
+		err           error
 	)
 	err = db.WithTransaction(ctx, func(c context.Context, tx metadata.DBStore) error {
-		revisionID, err = tx.CreateRevision(ctx, opt)
+		revisionID, snapshotTable, err = tx.CreateRevision(ctx, opt)
 		return err
 	})
-	return revisionID, err
+	return revisionID, snapshotTable, err
 }
 
 func (db *DB) UpdateRevision(ctx context.Context, opt metadata.UpdateRevisionOpt) error {
diff --git a/internal/database/metadata/postgres/revision.go b/internal/database/metadata/postgres/revision.go
index 3a91dd61..d750aa61 100644
--- a/internal/database/metadata/postgres/revision.go
+++ b/internal/database/metadata/postgres/revision.go
@@ -10,7 +10,7 @@ import (
 	"github.com/oom-ai/oomstore/internal/database/metadata"
 )
 
-func createRevision(ctx context.Context, sqlxCtx metadata.SqlxContext, opt metadata.CreateRevisionOpt) (int, error) {
+func createRevision(ctx context.Context, sqlxCtx metadata.SqlxContext, opt metadata.CreateRevisionOpt) (int, string, error) {
 	var snapshotTable, cdcTable string
 	if opt.SnapshotTable != nil {
 		snapshotTable = *opt.SnapshotTable
@@ -24,11 +24,11 @@ func createRevision(ctx context.Context, sqlxCtx metadata.SqlxContext, opt metad
 	if err := sqlxCtx.GetContext(ctx, &revisionID, insertQuery, opt.GroupID, opt.Revision, snapshotTable, cdcTable, opt.Anchored, opt.Description); err != nil {
 		if e2, ok := err.(*pq.Error); ok {
 			if e2.Code == pgerrcode.UniqueViolation {
-				return 0, errdefs.Errorf("revision already exists: groupID=%d, revision=%d", opt.GroupID, opt.Revision)
+				return 0, "", errdefs.Errorf("revision already exists: groupID=%d, revision=%d", opt.GroupID, opt.Revision)
 			}
 		}
-		return 0, errdefs.WithStack(err)
+		return 0, "", errdefs.WithStack(err)
 	}
 
-	return revisionID, nil
+	return revisionID, snapshotTable, nil
 }
diff --git a/internal/database/metadata/postgres/tx.go b/internal/database/metadata/postgres/tx.go
index 2ab96e4f..fb911e38 100644
--- a/internal/database/metadata/postgres/tx.go
+++ b/internal/database/metadata/postgres/tx.go
@@ -72,7 +72,7 @@ func (tx *Tx) GetFeatureByName(ctx context.Context, groupName string, featureNam
 	return sqlutil.GetFeatureByName(ctx, tx, groupName, featureName)
 }
 
-func (tx *Tx) CreateRevision(ctx context.Context, opt metadata.CreateRevisionOpt) (int, error) {
+func (tx *Tx) CreateRevision(ctx context.Context, opt metadata.CreateRevisionOpt) (int, string, error) {
 	return createRevision(ctx, tx, opt)
 }
 
diff --git a/internal/database/metadata/sqlite/db.go b/internal/database/metadata/sqlite/db.go
index e5cd99b6..ebb6a680 100644
--- a/internal/database/metadata/sqlite/db.go
+++ b/internal/database/metadata/sqlite/db.go
@@ -94,16 +94,17 @@ func (db *DB) ListGroup(ctx context.Context, entityID *int, groupIDs *[]int) (ty
 	return sqlutil.ListGroup(ctx, db, entityID, groupIDs)
 }
 
-func (db *DB) CreateRevision(ctx context.Context, opt metadata.CreateRevisionOpt) (int, error) {
+func (db *DB) CreateRevision(ctx context.Context, opt metadata.CreateRevisionOpt) (int, string, error) {
 	var (
-		revisionID int
-		err        error
+		revisionID    int
+		snapshotTable string
+		err           error
 	)
 	err = db.WithTransaction(ctx, func(c context.Context, tx metadata.DBStore) error {
-		revisionID, err = tx.CreateRevision(c, opt)
+		revisionID, snapshotTable, err = tx.CreateRevision(c, opt)
 		return err
 	})
-	return revisionID, err
+	return revisionID, snapshotTable, err
 }
 
 func (db *DB) UpdateRevision(ctx context.Context, opt metadata.UpdateRevisionOpt) error {
diff --git a/internal/database/metadata/sqlite/revision.go b/internal/database/metadata/sqlite/revision.go
index 85732850..7f506ee4 100644
--- a/internal/database/metadata/sqlite/revision.go
+++ b/internal/database/metadata/sqlite/revision.go
@@ -10,7 +10,7 @@ import (
 	"github.com/oom-ai/oomstore/internal/database/metadata"
 )
 
-func createRevision(ctx context.Context, sqlxCtx metadata.SqlxContext, opt metadata.CreateRevisionOpt) (int, error) {
+func createRevision(ctx context.Context, sqlxCtx metadata.SqlxContext, opt metadata.CreateRevisionOpt) (int, string, error) {
 	var snapshotTable, cdcTable string
 	if opt.SnapshotTable != nil {
 		snapshotTable = *opt.SnapshotTable
@@ -24,15 +24,15 @@ func createRevision(ctx context.Context, sqlxCtx metadata.SqlxContext, opt metad
 	if err != nil {
 		if sqliteErr, ok := err.(*sqlite.Error); ok {
 			if sqliteErr.Code() == sqlite3.SQLITE_CONSTRAINT_UNIQUE {
-				return 0, errdefs.Errorf("revision already exists: groupID=%d, revision=%d", opt.GroupID, opt.Revision)
+				return 0, "", errdefs.Errorf("revision already exists: groupID=%d, revision=%d", opt.GroupID, opt.Revision)
 			}
 		}
-		return 0, errdefs.WithStack(err)
+		return 0, "", errdefs.WithStack(err)
 	}
 	revisionID, err := res.LastInsertId()
 	if err != nil {
-		return 0, errdefs.WithStack(err)
+		return 0, "", errdefs.WithStack(err)
 	}
 
-	return int(revisionID), nil
+	return int(revisionID), snapshotTable, nil
 }
diff --git a/internal/database/metadata/sqlite/tx.go b/internal/database/metadata/sqlite/tx.go
index bf30b934..5d21cb35 100644
--- a/internal/database/metadata/sqlite/tx.go
+++ b/internal/database/metadata/sqlite/tx.go
@@ -73,7 +73,7 @@ func (tx *Tx) GetFeatureByName(ctx context.Context, groupName string, featureNam
 
 }
 
-func (tx *Tx) CreateRevision(ctx context.Context, opt metadata.CreateRevisionOpt) (int, error) {
+func (tx *Tx) CreateRevision(ctx context.Context, opt metadata.CreateRevisionOpt) (int, string, error) {
 	return createRevision(ctx, tx, opt)
 }
 
diff --git a/internal/database/metadata/store.go b/internal/database/metadata/store.go
index 3b7dbd3a..d6c73c06 100644
--- a/internal/database/metadata/store.go
+++ b/internal/database/metadata/store.go
@@ -40,7 +40,7 @@ type DBStore interface {
 	ListGroup(ctx context.Context, entityID *int, groupIDs *[]int) (types.GroupList, error)
 
 	// revision
-	CreateRevision(ctx context.Context, opt CreateRevisionOpt) (int, error)
+	CreateRevision(ctx context.Context, opt CreateRevisionOpt) (int, string, error)
 	UpdateRevision(ctx context.Context, opt UpdateRevisionOpt) error
 	GetRevision(ctx context.Context, id int) (*types.Revision, error)
 	GetRevisionBy(ctx context.Context, groupID int, revision int64) (*types.Revision, error)
diff --git a/pkg/oomstore/import.go b/pkg/oomstore/import.go
index 5371df60..cc939dbd 100644
--- a/pkg/oomstore/import.go
+++ b/pkg/oomstore/import.go
@@ -57,7 +57,7 @@ func (s *OomStore) csvReaderImport(ctx context.Context, opt *importOpt, dataSour
 		return 0, err
 	}
 
-	newRevisionID, err := s.createRevision(ctx, metadata.CreateRevisionOpt{
+	newRevisionID, _, err := s.createRevision(ctx, metadata.CreateRevisionOpt{
 		Revision:      time.Now().UnixMilli(),
 		GroupID:       opt.group.ID,
 		SnapshotTable: nil,
@@ -113,7 +113,7 @@ func (s *OomStore) tableLinkImport(ctx context.Context, opt *importOpt, dataSour
 	} else {
 		revision = *opt.Revision
 	}
-	newRevisionID, err := s.createRevision(ctx, metadata.CreateRevisionOpt{
+	newRevisionID, _, err := s.createRevision(ctx, metadata.CreateRevisionOpt{
 		Revision:      revision,
 		GroupID:       opt.group.ID,
 		SnapshotTable: &dataSource.TableName,
diff --git a/pkg/oomstore/import_stream.go b/pkg/oomstore/import_stream.go
index 7e70d251..06231dd6 100644
--- a/pkg/oomstore/import_stream.go
+++ b/pkg/oomstore/import_stream.go
@@ -113,7 +113,7 @@ func (s *OomStore) tableLinkImportStream(ctx context.Context, opt types.ImportSt
 		return err
 	}
 
-	_, err = s.createRevision(ctx, metadata.CreateRevisionOpt{
+	_, _, err = s.createRevision(ctx, metadata.CreateRevisionOpt{
 		Revision:    *tableSchema.TimeRange.MinUnixMilli,
 		GroupID:     group.ID,
 		CdcTable:    &dataSource.TableName,
diff --git a/pkg/oomstore/push_processor.go b/pkg/oomstore/push_processor.go
index b98a4de1..6e668ff3 100644
--- a/pkg/oomstore/push_processor.go
+++ b/pkg/oomstore/push_processor.go
@@ -163,7 +163,7 @@ func (s *OomStore) newRevisionForStream(ctx context.Context, groupID int, revisi
 
 	snapshotTable := ""
 	cdcTable := dbutil.OfflineStreamCdcTableName(groupID, revision)
-	if _, err := s.createRevision(ctx, metadata.CreateRevisionOpt{
+	if _, _, err := s.createRevision(ctx, metadata.CreateRevisionOpt{
 		GroupID:       groupID,
 		Revision:      revision,
 		SnapshotTable: &snapshotTable,
diff --git a/pkg/oomstore/revision.go b/pkg/oomstore/revision.go
index bfa5e6da..f0a76925 100644
--- a/pkg/oomstore/revision.go
+++ b/pkg/oomstore/revision.go
@@ -25,8 +25,9 @@ func (s *OomStore) GetRevisionBy(ctx context.Context, groupID int, revision int6
 	return s.metadata.GetRevisionBy(ctx, groupID, revision)
 }
 
-func (s *OomStore) createRevision(ctx context.Context, opt metadata.CreateRevisionOpt) (int, error) {
+func (s *OomStore) createRevision(ctx context.Context, opt metadata.CreateRevisionOpt) (int, string, error) {
 	var revisionID int
+	var snapshotTable string
 	var dummyRevision *types.Revision
 
 	if err := s.metadata.WithTransaction(ctx, func(c context.Context, tx metadata.DBStore) error {
@@ -36,10 +37,10 @@ func (s *OomStore) createRevision(ctx context.Context, opt metadata.CreateRevisi
 				return err
 			}
 
-			if _, err = tx.CreateRevision(ctx, metadata.CreateRevisionOpt{
+			if _, _, err = tx.CreateRevision(ctx, metadata.CreateRevisionOpt{
 				Revision:    0,
 				GroupID:     opt.GroupID,
-				Description: "dummy revision will be used at Join and Export",
+				Description: "dummy revision",
 			}); err != nil {
 				return err
 			}
@@ -50,19 +51,19 @@ func (s *OomStore) createRevision(ctx context.Context, opt metadata.CreateRevisi
 			}
 		}
 
-		revisionID, err = tx.CreateRevision(ctx, opt)
+		revisionID, snapshotTable, err = tx.CreateRevision(ctx, opt)
 		return err
 	}); err != nil {
-		return 0, err
+		return 0, "", err
 	}
 
 	if dummyRevision != nil {
 		if err := s.createSnapshotAndCdcTable(ctx, dummyRevision); err != nil {
-			return 0, err
+			return 0, "", err
 		}
 	}
 
-	return revisionID, nil
+	return revisionID, snapshotTable, nil
 }
 
 func (s *OomStore) createSnapshotAndCdcTable(ctx context.Context, revision *types.Revision) error {


Test patch:
diff --git a/internal/database/metadata/test_impl/revision.go b/internal/database/metadata/test_impl/revision.go
index b0f2b1e3..1bce4f80 100644
--- a/internal/database/metadata/test_impl/revision.go
+++ b/internal/database/metadata/test_impl/revision.go
@@ -79,7 +79,7 @@ func TestCreateRevision(t *testing.T, prepareStore PrepareStoreFn, destroyStore
 
 	for _, tc := range testCases {
 		t.Run(tc.description, func(t *testing.T) {
-			actual, err := store.CreateRevision(ctx, tc.opt)
+			actual, _, err := store.CreateRevision(ctx, tc.opt)
 			require.Equal(t, tc.expected, actual)
 			if tc.expectedError != nil {
 				require.EqualError(t, err, tc.expectedError.Error())
@@ -104,7 +104,7 @@ func TestUpdateRevision(t *testing.T, prepareStore PrepareStoreFn, destroyStore
 	_, groupID := prepareEntityAndGroup(t, ctx, store)
 	group, err := store.GetGroup(ctx, groupID)
 	require.NoError(t, err)
-	revisionID, err := store.CreateRevision(ctx, metadata.CreateRevisionOpt{
+	revisionID, _, err := store.CreateRevision(ctx, metadata.CreateRevisionOpt{
 		Revision:      1000,
 		GroupID:       groupID,
 		SnapshotTable: stringPtr("offline_stream_snapshot_device_info_1000"),
@@ -203,7 +203,7 @@ func TestGetRevision(t *testing.T, prepareStore PrepareStoreFn, destroyStore Des
 	defer store.Close()
 
 	_, groupID := prepareEntityAndGroup(t, ctx, store)
-	revisionID, err := store.CreateRevision(ctx, metadata.CreateRevisionOpt{
+	revisionID, _, err := store.CreateRevision(ctx, metadata.CreateRevisionOpt{
 		Revision:      1000,
 		GroupID:       groupID,
 		SnapshotTable: stringPtr("device_info_1000"),
@@ -266,7 +266,7 @@ func TestGetRevisionBy(t *testing.T, prepareStore PrepareStoreFn, destroyStore D
 	defer store.Close()
 
 	_, groupID := prepareEntityAndGroup(t, ctx, store)
-	revisionID, err := store.CreateRevision(ctx, metadata.CreateRevisionOpt{
+	revisionID, _, err := store.CreateRevision(ctx, metadata.CreateRevisionOpt{
 		Revision:      1000,
 		GroupID:       groupID,
 		SnapshotTable: stringPtr("device_info_1000"),
@@ -390,7 +390,7 @@ func prepareRevisions(t *testing.T, ctx context.Context, store metadata.Store) (
 	})
 	require.NoError(t, err)
 	require.NoError(t, store.Refresh())
-	revisionID1, err := store.CreateRevision(ctx, metadata.CreateRevisionOpt{
+	revisionID1, _, err := store.CreateRevision(ctx, metadata.CreateRevisionOpt{
 		Revision:      1000,
 		GroupID:       groupID,
 		SnapshotTable: stringPtr("device_info_1000"),
@@ -398,7 +398,7 @@ func prepareRevisions(t *testing.T, ctx context.Context, store metadata.Store) (
 	})
 	require.NoError(t, err)
 
-	revisionID2, err := store.CreateRevision(ctx, metadata.CreateRevisionOpt{
+	revisionID2, _, err := store.CreateRevision(ctx, metadata.CreateRevisionOpt{
 		Revision:      2000,
 		GroupID:       groupID,
 		SnapshotTable: stringPtr("device_info_2000"),
diff --git a/oomcli/test/test_get_meta_revision.sh b/oomcli/test/test_get_meta_revision.sh
index 797d4097..88b3d7e4 100755
--- a/oomcli/test/test_get_meta_revision.sh
+++ b/oomcli/test/test_get_meta_revision.sh
@@ -9,7 +9,7 @@ import_device_sample > /dev/null
 case='oomcli get meta revision works'
 expected='
 ID,REVISION,GROUP,SNAPSHOT-TABLE,CDC-TABLE,DESCRIPTION,ANCHORED,CREATE-TIME,MODIFY-TIME
-1,0,phone,offline_batch_snapshot_1_1,,dummy revision will be used at Join and Export
+1,0,phone,offline_batch_snapshot_1_1,,dummy revision
 2,1638519905556,phone,offline_batch_snapshot_1_2,,test data,true,2021-12-03T08:25:05Z,2021-12-03T08:25:05Z
 '
 actual=$(oomcli get meta revision --group phone -o csv --wide)