rename stableID to ID (#425)

embedding a baseModel means that objects like backup
have a backup.StableID, but no backup.ID nor backup.
backupID.  This could be confusing for users.  This change
swaps the value and type names, so that baseModels
include an ID value of type StableID.
This commit is contained in:
Keepers 2022-07-27 14:54:58 -06:00 committed by GitHub
parent cc810fa3ab
commit e35acb79ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 50 additions and 48 deletions

View File

@ -66,7 +66,7 @@ func tagsForModel(s model.Schema, tags map[string]string) (map[string]string, er
// type or if a bad model type is given. // type or if a bad model type is given.
func tagsForModelWithID( func tagsForModelWithID(
s model.Schema, s model.Schema,
id model.ID, id model.StableID,
tags map[string]string, tags map[string]string,
) (map[string]string, error) { ) (map[string]string, error) {
if !s.Valid() { if !s.Valid() {
@ -106,10 +106,10 @@ func putInner(
base := m.Base() base := m.Base()
if create { if create {
base.StableID = model.ID(uuid.NewString()) base.ID = model.StableID(uuid.NewString())
} }
tmpTags, err := tagsForModelWithID(s, base.StableID, base.Tags) tmpTags, err := tagsForModelWithID(s, base.ID, base.Tags)
if err != nil { if err != nil {
// Will be wrapped at a higher layer. // Will be wrapped at a higher layer.
return err return err
@ -165,7 +165,7 @@ func baseModelFromMetadata(m *manifest.EntryMetadata) (*model.BaseModel, error)
res := &model.BaseModel{ res := &model.BaseModel{
ModelStoreID: m.ID, ModelStoreID: m.ID,
StableID: model.ID(id), ID: model.StableID(id),
Tags: m.Labels, Tags: m.Labels,
} }
@ -219,7 +219,7 @@ func (ms *ModelStore) GetIDsForType(
func (ms *ModelStore) getModelStoreID( func (ms *ModelStore) getModelStoreID(
ctx context.Context, ctx context.Context,
s model.Schema, s model.Schema,
id model.ID, id model.StableID,
) (manifest.ID, error) { ) (manifest.ID, error) {
if !s.Valid() { if !s.Valid() {
return "", errors.New("unrecognized model schema") return "", errors.New("unrecognized model schema")
@ -255,7 +255,7 @@ func (ms *ModelStore) getModelStoreID(
func (ms *ModelStore) Get( func (ms *ModelStore) Get(
ctx context.Context, ctx context.Context,
s model.Schema, s model.Schema,
id model.ID, id model.StableID,
data model.Model, data model.Model,
) error { ) error {
if !s.Valid() { if !s.Valid() {
@ -320,7 +320,7 @@ func (ms *ModelStore) checkPrevModelVersion(
return errors.WithStack(errUnrecognizedSchema) return errors.WithStack(errUnrecognizedSchema)
} }
id, err := ms.getModelStoreID(ctx, s, b.StableID) id, err := ms.getModelStoreID(ctx, s, b.ID)
if err != nil { if err != nil {
return err return err
} }
@ -405,7 +405,7 @@ func (ms *ModelStore) Update(
// Delete deletes the model with the given StableID. Turns into a noop if id is // Delete deletes the model with the given StableID. Turns into a noop if id is
// not empty but the model does not exist. Returns an error if multiple models // not empty but the model does not exist. Returns an error if multiple models
// have the same StableID. // have the same StableID.
func (ms *ModelStore) Delete(ctx context.Context, s model.Schema, id model.ID) error { func (ms *ModelStore) Delete(ctx context.Context, s model.Schema, id model.StableID) error {
if !s.Valid() { if !s.Valid() {
return errors.WithStack(errUnrecognizedSchema) return errors.WithStack(errUnrecognizedSchema)
} }

View File

@ -116,11 +116,11 @@ func (suite *ModelStoreIntegrationSuite) TestNoIDsErrors() {
theModelType := model.BackupOpSchema theModelType := model.BackupOpSchema
noStableID := &fooModel{Bar: uuid.NewString()} noStableID := &fooModel{Bar: uuid.NewString()}
noStableID.StableID = "" noStableID.ID = ""
noStableID.ModelStoreID = manifest.ID(uuid.NewString()) noStableID.ModelStoreID = manifest.ID(uuid.NewString())
noModelStoreID := &fooModel{Bar: uuid.NewString()} noModelStoreID := &fooModel{Bar: uuid.NewString()}
noModelStoreID.StableID = model.ID(uuid.NewString()) noModelStoreID.ID = model.StableID(uuid.NewString())
noModelStoreID.ModelStoreID = "" noModelStoreID.ModelStoreID = ""
assert.Error(t, suite.m.Update(suite.ctx, theModelType, noStableID)) assert.Error(t, suite.m.Update(suite.ctx, theModelType, noStableID))
@ -155,11 +155,11 @@ func (suite *ModelStoreIntegrationSuite) TestBadTypeErrors() {
require.NoError(t, suite.m.Put(suite.ctx, model.BackupOpSchema, foo)) require.NoError(t, suite.m.Put(suite.ctx, model.BackupOpSchema, foo))
returned := &fooModel{} returned := &fooModel{}
assert.Error(t, suite.m.Get(suite.ctx, model.RestoreOpSchema, foo.StableID, returned)) assert.Error(t, suite.m.Get(suite.ctx, model.RestoreOpSchema, foo.ID, returned))
assert.Error( assert.Error(
t, suite.m.GetWithModelStoreID(suite.ctx, model.RestoreOpSchema, foo.ModelStoreID, returned)) t, suite.m.GetWithModelStoreID(suite.ctx, model.RestoreOpSchema, foo.ModelStoreID, returned))
assert.Error(t, suite.m.Delete(suite.ctx, model.RestoreOpSchema, foo.StableID)) assert.Error(t, suite.m.Delete(suite.ctx, model.RestoreOpSchema, foo.ID))
} }
func (suite *ModelStoreIntegrationSuite) TestPutGet() { func (suite *ModelStoreIntegrationSuite) TestPutGet() {
@ -204,10 +204,10 @@ func (suite *ModelStoreIntegrationSuite) TestPutGet() {
} }
require.NotEmpty(t, foo.ModelStoreID) require.NotEmpty(t, foo.ModelStoreID)
require.NotEmpty(t, foo.StableID) require.NotEmpty(t, foo.ID)
returned := &fooModel{} returned := &fooModel{}
err = suite.m.Get(suite.ctx, test.s, foo.StableID, returned) err = suite.m.Get(suite.ctx, test.s, foo.ID, returned)
require.NoError(t, err) require.NoError(t, err)
assert.Equal(t, foo, returned) assert.Equal(t, foo, returned)
@ -230,10 +230,10 @@ func (suite *ModelStoreIntegrationSuite) TestPutGet_WithTags() {
require.NoError(t, suite.m.Put(suite.ctx, theModelType, foo)) require.NoError(t, suite.m.Put(suite.ctx, theModelType, foo))
require.NotEmpty(t, foo.ModelStoreID) require.NotEmpty(t, foo.ModelStoreID)
require.NotEmpty(t, foo.StableID) require.NotEmpty(t, foo.ID)
returned := &fooModel{} returned := &fooModel{}
err := suite.m.Get(suite.ctx, theModelType, foo.StableID, returned) err := suite.m.Get(suite.ctx, theModelType, foo.ID, returned)
require.NoError(t, err) require.NoError(t, err)
assert.Equal(t, foo, returned) assert.Equal(t, foo, returned)
@ -336,12 +336,12 @@ func (suite *ModelStoreIntegrationSuite) TestPutUpdate() {
require.NoError(t, m.Put(ctx, theModelType, foo)) require.NoError(t, m.Put(ctx, theModelType, foo))
oldModelID := foo.ModelStoreID oldModelID := foo.ModelStoreID
oldStableID := foo.StableID oldStableID := foo.ID
test.mutator(foo) test.mutator(foo)
require.NoError(t, m.Update(ctx, theModelType, foo)) require.NoError(t, m.Update(ctx, theModelType, foo))
assert.Equal(t, oldStableID, foo.StableID) assert.Equal(t, oldStableID, foo.ID)
returned := &fooModel{} returned := &fooModel{}
require.NoError( require.NoError(
@ -415,7 +415,7 @@ func (suite *ModelStoreIntegrationSuite) TestPutDelete() {
require.NoError(t, suite.m.Put(suite.ctx, theModelType, foo)) require.NoError(t, suite.m.Put(suite.ctx, theModelType, foo))
require.NoError(t, suite.m.Delete(suite.ctx, theModelType, foo.StableID)) require.NoError(t, suite.m.Delete(suite.ctx, theModelType, foo.ID))
returned := &fooModel{} returned := &fooModel{}
err := suite.m.GetWithModelStoreID(suite.ctx, theModelType, foo.ModelStoreID, returned) err := suite.m.GetWithModelStoreID(suite.ctx, theModelType, foo.ModelStoreID, returned)
@ -467,7 +467,7 @@ func (suite *ModelStoreRegressionSuite) TestFailDuringWriteSessionHasNoVisibleEf
}() }()
foo := &fooModel{Bar: uuid.NewString()} foo := &fooModel{Bar: uuid.NewString()}
foo.StableID = model.ID(uuid.NewString()) foo.ID = model.StableID(uuid.NewString())
foo.ModelStoreID = manifest.ID(uuid.NewString()) foo.ModelStoreID = manifest.ID(uuid.NewString())
// Avoid some silly test errors from comparing nil to empty map. // Avoid some silly test errors from comparing nil to empty map.
foo.Tags = map[string]string{} foo.Tags = map[string]string{}

View File

@ -5,8 +5,10 @@ import (
) )
type ( type (
ID string // StableID is used by BaseModel.ID to uniquely identify objects
Schema int // stored in the modelStore.
StableID string
Schema int
) )
//go:generate go run golang.org/x/tools/cmd/stringer -type=Schema //go:generate go run golang.org/x/tools/cmd/stringer -type=Schema
@ -32,11 +34,11 @@ type Model interface {
// that wish to be stored should embed this struct. This struct also represents // that wish to be stored should embed this struct. This struct also represents
// the common metadata the ModelStore will fill out/use. // the common metadata the ModelStore will fill out/use.
type BaseModel struct { type BaseModel struct {
// StableID is an identifier that other objects can use to refer to this // ID is an identifier that other objects can use to refer to this
// object in the ModelStore. // object in the ModelStore.
// Once generated (during Put), it is guaranteed not to change. This field // Once generated (during Put), it is guaranteed not to change. This field
// should be treated as read-only by users. // should be treated as read-only by users.
StableID ID `json:"stableID,omitempty"` ID StableID `json:"ID,omitempty"`
// ModelStoreID is an internal ID for the model in the store. If present it // ModelStoreID is an internal ID for the model in the store. If present it
// can be used for efficient lookups, but should not be used by other models // can be used for efficient lookups, but should not be used by other models
// to refer to this one. This field may change if the model is updated. This // to refer to this one. This field may change if the model is updated. This

View File

@ -34,7 +34,7 @@ type BackupOperation struct {
type BackupResults struct { type BackupResults struct {
stats.ReadWrites stats.ReadWrites
stats.StartAndEndTime stats.StartAndEndTime
BackupID model.ID `json:"backupID"` BackupID model.StableID `json:"backupID"`
} }
// NewBackupOperation constructs and validates a backup operation. // NewBackupOperation constructs and validates a backup operation.
@ -159,7 +159,7 @@ func (op *BackupOperation) createBackupModels(ctx context.Context, snapID string
if err != nil { if err != nil {
return errors.Wrap(err, "creating backup model") return errors.Wrap(err, "creating backup model")
} }
op.Results.BackupID = b.StableID op.Results.BackupID = b.ID
return nil return nil
} }

View File

@ -22,7 +22,7 @@ import (
type RestoreOperation struct { type RestoreOperation struct {
operation operation
BackupID model.ID `json:"backupID"` BackupID model.StableID `json:"backupID"`
Results RestoreResults `json:"results"` Results RestoreResults `json:"results"`
Selectors selectors.Selector `json:"selectors"` // todo: replace with Selectors Selectors selectors.Selector `json:"selectors"` // todo: replace with Selectors
Version string `json:"version"` Version string `json:"version"`
@ -43,7 +43,7 @@ func NewRestoreOperation(
kw *kopia.Wrapper, kw *kopia.Wrapper,
sw *store.Wrapper, sw *store.Wrapper,
acct account.Account, acct account.Account,
backupID model.ID, backupID model.StableID,
sel selectors.Selector, sel selectors.Selector,
) (RestoreOperation, error) { ) (RestoreOperation, error) {
op := RestoreOperation{ op := RestoreOperation{

View File

@ -58,7 +58,7 @@ func (b Backup) Headers() []string {
func (b Backup) Values() []string { func (b Backup) Values() []string {
return []string{ return []string{
common.FormatTime(b.CreationTime), common.FormatTime(b.CreationTime),
string(b.StableID), string(b.ID),
b.SnapshotID, b.SnapshotID,
b.DetailsID, b.DetailsID,
b.Status, b.Status,

View File

@ -30,7 +30,7 @@ func (suite *BackupSuite) TestBackup_HeadersValues() {
b := backup.Backup{ b := backup.Backup{
BaseModel: model.BaseModel{ BaseModel: model.BaseModel{
StableID: model.ID("stable"), ID: model.StableID("stable"),
}, },
CreationTime: now, CreationTime: now,
SnapshotID: "snapshot", SnapshotID: "snapshot",

View File

@ -25,7 +25,7 @@ var (
detailsID = uuid.NewString() detailsID = uuid.NewString()
bu = backup.Backup{ bu = backup.Backup{
BaseModel: model.BaseModel{ BaseModel: model.BaseModel{
StableID: model.ID(uuid.NewString()), ID: model.StableID(uuid.NewString()),
ModelStoreID: manifest.ID(uuid.NewString()), ModelStoreID: manifest.ID(uuid.NewString()),
}, },
CreationTime: time.Now(), CreationTime: time.Now(),
@ -35,7 +35,7 @@ var (
deets = details.Details{ deets = details.Details{
DetailsModel: details.DetailsModel{ DetailsModel: details.DetailsModel{
BaseModel: model.BaseModel{ BaseModel: model.BaseModel{
StableID: model.ID(detailsID), ID: model.StableID(detailsID),
ModelStoreID: manifest.ID(uuid.NewString()), ModelStoreID: manifest.ID(uuid.NewString()),
}, },
}, },
@ -77,7 +77,7 @@ func (suite *StoreDetailsUnitSuite) TestGetDetails() {
if err != nil { if err != nil {
return return
} }
assert.Equal(t, deets.StableID, result.StableID) assert.Equal(t, deets.ID, result.ID)
}) })
} }
} }
@ -104,13 +104,13 @@ func (suite *StoreDetailsUnitSuite) TestGetDetailsFromBackupID() {
for _, test := range table { for _, test := range table {
suite.T().Run(test.name, func(t *testing.T) { suite.T().Run(test.name, func(t *testing.T) {
store := &store.Wrapper{Storer: test.mock} store := &store.Wrapper{Storer: test.mock}
dResult, bResult, err := store.GetDetailsFromBackupID(ctx, model.ID(uuid.NewString())) dResult, bResult, err := store.GetDetailsFromBackupID(ctx, model.StableID(uuid.NewString()))
test.expect(t, err) test.expect(t, err)
if err != nil { if err != nil {
return return
} }
assert.Equal(t, deets.StableID, dResult.StableID) assert.Equal(t, deets.ID, dResult.ID)
assert.Equal(t, bu.StableID, bResult.StableID) assert.Equal(t, bu.ID, bResult.ID)
}) })
} }
} }

View File

@ -152,7 +152,7 @@ func (r Repository) NewRestore(
r.dataLayer, r.dataLayer,
store.NewKopiaStore(r.modelStore), store.NewKopiaStore(r.modelStore),
r.Account, r.Account,
model.ID(backupID), model.StableID(backupID),
sel) sel)
} }
@ -165,5 +165,5 @@ func (r Repository) Backups(ctx context.Context) ([]backup.Backup, error) {
// BackupDetails returns the specified backup details object // BackupDetails returns the specified backup details object
func (r Repository) BackupDetails(ctx context.Context, backupID string) (*details.Details, *backup.Backup, error) { func (r Repository) BackupDetails(ctx context.Context, backupID string) (*details.Details, *backup.Backup, error) {
sw := store.NewKopiaStore(r.modelStore) sw := store.NewKopiaStore(r.modelStore)
return sw.GetDetailsFromBackupID(ctx, model.ID(backupID)) return sw.GetDetailsFromBackupID(ctx, model.StableID(backupID))
} }

View File

@ -12,7 +12,7 @@ import (
) )
// GetBackup gets a single backup by id. // GetBackup gets a single backup by id.
func (w Wrapper) GetBackup(ctx context.Context, backupID model.ID) (*backup.Backup, error) { func (w Wrapper) GetBackup(ctx context.Context, backupID model.StableID) (*backup.Backup, error) {
b := backup.Backup{} b := backup.Backup{}
err := w.Get(ctx, model.BackupSchema, backupID, &b) err := w.Get(ctx, model.BackupSchema, backupID, &b)
if err != nil { if err != nil {
@ -50,7 +50,7 @@ func (w Wrapper) GetDetails(ctx context.Context, detailsID manifest.ID) (*detail
} }
// GetDetailsFromBackupID retrieves the backup.Details within the specified backup. // GetDetailsFromBackupID retrieves the backup.Details within the specified backup.
func (w Wrapper) GetDetailsFromBackupID(ctx context.Context, backupID model.ID) (*details.Details, *backup.Backup, error) { func (w Wrapper) GetDetailsFromBackupID(ctx context.Context, backupID model.StableID) (*details.Details, *backup.Backup, error) {
b, err := w.GetBackup(ctx, backupID) b, err := w.GetBackup(ctx, backupID)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err

View File

@ -24,7 +24,7 @@ var (
detailsID = uuid.NewString() detailsID = uuid.NewString()
bu = backup.Backup{ bu = backup.Backup{
BaseModel: model.BaseModel{ BaseModel: model.BaseModel{
StableID: model.ID(uuid.NewString()), ID: model.StableID(uuid.NewString()),
ModelStoreID: manifest.ID(uuid.NewString()), ModelStoreID: manifest.ID(uuid.NewString()),
}, },
CreationTime: time.Now(), CreationTime: time.Now(),
@ -63,12 +63,12 @@ func (suite *StoreBackupUnitSuite) TestGetBackup() {
for _, test := range table { for _, test := range table {
suite.T().Run(test.name, func(t *testing.T) { suite.T().Run(test.name, func(t *testing.T) {
sm := &store.Wrapper{Storer: test.mock} sm := &store.Wrapper{Storer: test.mock}
result, err := sm.GetBackup(ctx, model.ID(uuid.NewString())) result, err := sm.GetBackup(ctx, model.StableID(uuid.NewString()))
test.expect(t, err) test.expect(t, err)
if err != nil { if err != nil {
return return
} }
assert.Equal(t, bu.StableID, result.StableID) assert.Equal(t, bu.ID, result.ID)
}) })
} }
} }
@ -101,7 +101,7 @@ func (suite *StoreBackupUnitSuite) TestGetBackups() {
return return
} }
assert.Equal(t, 1, len(result)) assert.Equal(t, 1, len(result))
assert.Equal(t, bu.StableID, result[0].StableID) assert.Equal(t, bu.ID, result[0].ID)
}) })
} }
} }

View File

@ -44,7 +44,7 @@ func unmarshal(b []byte, a any) {
// deleter iface // deleter iface
// ------------------------------------------------------------ // ------------------------------------------------------------
func (mms *MockModelStore) Delete(ctx context.Context, s model.Schema, id model.ID) error { func (mms *MockModelStore) Delete(ctx context.Context, s model.Schema, id model.StableID) error {
return mms.err return mms.err
} }
@ -59,7 +59,7 @@ func (mms *MockModelStore) DeleteWithModelStoreID(ctx context.Context, id manife
func (mms *MockModelStore) Get( func (mms *MockModelStore) Get(
ctx context.Context, ctx context.Context,
s model.Schema, s model.Schema,
id model.ID, id model.StableID,
data model.Model, data model.Model,
) error { ) error {
if mms.err != nil { if mms.err != nil {

View File

@ -13,9 +13,9 @@ var _ Storer = &kopia.ModelStore{}
type ( type (
Storer interface { Storer interface {
Delete(ctx context.Context, s model.Schema, id model.ID) error Delete(ctx context.Context, s model.Schema, id model.StableID) error
DeleteWithModelStoreID(ctx context.Context, id manifest.ID) error DeleteWithModelStoreID(ctx context.Context, id manifest.ID) error
Get(ctx context.Context, s model.Schema, id model.ID, data model.Model) error Get(ctx context.Context, s model.Schema, id model.StableID, data model.Model) error
GetIDsForType(ctx context.Context, s model.Schema, tags map[string]string) ([]*model.BaseModel, error) GetIDsForType(ctx context.Context, s model.Schema, tags map[string]string) ([]*model.BaseModel, error)
GetWithModelStoreID(ctx context.Context, s model.Schema, id manifest.ID, data model.Model) error GetWithModelStoreID(ctx context.Context, s model.Schema, id manifest.ID, data model.Model) error
Put(ctx context.Context, s model.Schema, m model.Model) error Put(ctx context.Context, s model.Schema, m model.Model) error