diff --git a/client/db.go b/client/db.go index ba4dd0b89d..3ec9b9a366 100644 --- a/client/db.go +++ b/client/db.go @@ -111,6 +111,8 @@ type Store interface { // [FieldKindStringToEnumMapping]. PatchSchema(context.Context, string) error + SetDefaultSchemaVersion(context.Context, string) error + // SetMigration sets the migration for the given source-destination schema version IDs. Is equivilent to // calling `LensRegistry().SetMigration(ctx, cfg)`. // diff --git a/client/mocks/db.go b/client/mocks/db.go index cb0af26193..afaa1503bf 100644 --- a/client/mocks/db.go +++ b/client/mocks/db.go @@ -1163,6 +1163,49 @@ func (_c *DB_Root_Call) RunAndReturn(run func() datastore.RootStore) *DB_Root_Ca return _c } +// SetDefaultSchemaVersion provides a mock function with given fields: _a0, _a1 +func (_m *DB) SetDefaultSchemaVersion(_a0 context.Context, _a1 string) error { + ret := _m.Called(_a0, _a1) + + var r0 error + if rf, ok := ret.Get(0).(func(context.Context, string) error); ok { + r0 = rf(_a0, _a1) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// DB_SetDefaultSchemaVersion_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'SetDefaultSchemaVersion' +type DB_SetDefaultSchemaVersion_Call struct { + *mock.Call +} + +// SetDefaultSchemaVersion is a helper method to define mock.On call +// - _a0 context.Context +// - _a1 string +func (_e *DB_Expecter) SetDefaultSchemaVersion(_a0 interface{}, _a1 interface{}) *DB_SetDefaultSchemaVersion_Call { + return &DB_SetDefaultSchemaVersion_Call{Call: _e.mock.On("SetDefaultSchemaVersion", _a0, _a1)} +} + +func (_c *DB_SetDefaultSchemaVersion_Call) Run(run func(_a0 context.Context, _a1 string)) *DB_SetDefaultSchemaVersion_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(string)) + }) + return _c +} + +func (_c *DB_SetDefaultSchemaVersion_Call) Return(_a0 error) *DB_SetDefaultSchemaVersion_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *DB_SetDefaultSchemaVersion_Call) RunAndReturn(run func(context.Context, string) error) *DB_SetDefaultSchemaVersion_Call { + _c.Call.Return(run) + return _c +} + // SetMigration provides a mock function with given fields: _a0, _a1 func (_m *DB) SetMigration(_a0 context.Context, _a1 client.LensConfig) error { ret := _m.Called(_a0, _a1) diff --git a/db/collection.go b/db/collection.go index 82ff083842..439f4d33ee 100644 --- a/db/collection.go +++ b/db/collection.go @@ -300,7 +300,7 @@ func (db *db) updateCollection( return nil, err } - err = db.setDefaultSchemaVersion(ctx, txn, desc.Name, desc.Schema.SchemaID, schemaVersionID) + err = db.setDefaultSchemaVersionExplicit(ctx, txn, desc.Name, desc.Schema.SchemaID, schemaVersionID) if err != nil { return nil, err } @@ -585,6 +585,20 @@ func validateUpdateCollectionIndexes( } func (db *db) setDefaultSchemaVersion( + ctx context.Context, + txn datastore.Txn, + schemaVersionID string, +) error { + col, err := db.getCollectionByVersionID(ctx, txn, schemaVersionID) + if err != nil { + return err + } + + desc := col.Description() + return db.setDefaultSchemaVersionExplicit(ctx, txn, desc.Name, desc.Schema.SchemaID, schemaVersionID) +} + +func (db *db) setDefaultSchemaVersionExplicit( ctx context.Context, txn datastore.Txn, collectionName string, diff --git a/db/fetcher/mocks/fetcher.go b/db/fetcher/mocks/fetcher.go index 12bb386024..79eefefc2b 100644 --- a/db/fetcher/mocks/fetcher.go +++ b/db/fetcher/mocks/fetcher.go @@ -133,136 +133,6 @@ func (_c *Fetcher_FetchNext_Call) RunAndReturn(run func(context.Context) (fetche return _c } -// FetchNextDecoded provides a mock function with given fields: ctx -func (_m *Fetcher) FetchNextDecoded(ctx context.Context) (*client.Document, fetcher.ExecInfo, error) { - ret := _m.Called(ctx) - - var r0 *client.Document - var r1 fetcher.ExecInfo - var r2 error - if rf, ok := ret.Get(0).(func(context.Context) (*client.Document, fetcher.ExecInfo, error)); ok { - return rf(ctx) - } - if rf, ok := ret.Get(0).(func(context.Context) *client.Document); ok { - r0 = rf(ctx) - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).(*client.Document) - } - } - - if rf, ok := ret.Get(1).(func(context.Context) fetcher.ExecInfo); ok { - r1 = rf(ctx) - } else { - r1 = ret.Get(1).(fetcher.ExecInfo) - } - - if rf, ok := ret.Get(2).(func(context.Context) error); ok { - r2 = rf(ctx) - } else { - r2 = ret.Error(2) - } - - return r0, r1, r2 -} - -// Fetcher_FetchNextDecoded_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'FetchNextDecoded' -type Fetcher_FetchNextDecoded_Call struct { - *mock.Call -} - -// FetchNextDecoded is a helper method to define mock.On call -// - ctx context.Context -func (_e *Fetcher_Expecter) FetchNextDecoded(ctx interface{}) *Fetcher_FetchNextDecoded_Call { - return &Fetcher_FetchNextDecoded_Call{Call: _e.mock.On("FetchNextDecoded", ctx)} -} - -func (_c *Fetcher_FetchNextDecoded_Call) Run(run func(ctx context.Context)) *Fetcher_FetchNextDecoded_Call { - _c.Call.Run(func(args mock.Arguments) { - run(args[0].(context.Context)) - }) - return _c -} - -func (_c *Fetcher_FetchNextDecoded_Call) Return(_a0 *client.Document, _a1 fetcher.ExecInfo, _a2 error) *Fetcher_FetchNextDecoded_Call { - _c.Call.Return(_a0, _a1, _a2) - return _c -} - -func (_c *Fetcher_FetchNextDecoded_Call) RunAndReturn(run func(context.Context) (*client.Document, fetcher.ExecInfo, error)) *Fetcher_FetchNextDecoded_Call { - _c.Call.Return(run) - return _c -} - -// FetchNextDoc provides a mock function with given fields: ctx, mapping -func (_m *Fetcher) FetchNextDoc(ctx context.Context, mapping *core.DocumentMapping) ([]byte, core.Doc, fetcher.ExecInfo, error) { - ret := _m.Called(ctx, mapping) - - var r0 []byte - var r1 core.Doc - var r2 fetcher.ExecInfo - var r3 error - if rf, ok := ret.Get(0).(func(context.Context, *core.DocumentMapping) ([]byte, core.Doc, fetcher.ExecInfo, error)); ok { - return rf(ctx, mapping) - } - if rf, ok := ret.Get(0).(func(context.Context, *core.DocumentMapping) []byte); ok { - r0 = rf(ctx, mapping) - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).([]byte) - } - } - - if rf, ok := ret.Get(1).(func(context.Context, *core.DocumentMapping) core.Doc); ok { - r1 = rf(ctx, mapping) - } else { - r1 = ret.Get(1).(core.Doc) - } - - if rf, ok := ret.Get(2).(func(context.Context, *core.DocumentMapping) fetcher.ExecInfo); ok { - r2 = rf(ctx, mapping) - } else { - r2 = ret.Get(2).(fetcher.ExecInfo) - } - - if rf, ok := ret.Get(3).(func(context.Context, *core.DocumentMapping) error); ok { - r3 = rf(ctx, mapping) - } else { - r3 = ret.Error(3) - } - - return r0, r1, r2, r3 -} - -// Fetcher_FetchNextDoc_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'FetchNextDoc' -type Fetcher_FetchNextDoc_Call struct { - *mock.Call -} - -// FetchNextDoc is a helper method to define mock.On call -// - ctx context.Context -// - mapping *core.DocumentMapping -func (_e *Fetcher_Expecter) FetchNextDoc(ctx interface{}, mapping interface{}) *Fetcher_FetchNextDoc_Call { - return &Fetcher_FetchNextDoc_Call{Call: _e.mock.On("FetchNextDoc", ctx, mapping)} -} - -func (_c *Fetcher_FetchNextDoc_Call) Run(run func(ctx context.Context, mapping *core.DocumentMapping)) *Fetcher_FetchNextDoc_Call { - _c.Call.Run(func(args mock.Arguments) { - run(args[0].(context.Context), args[1].(*core.DocumentMapping)) - }) - return _c -} - -func (_c *Fetcher_FetchNextDoc_Call) Return(_a0 []byte, _a1 core.Doc, _a2 fetcher.ExecInfo, _a3 error) *Fetcher_FetchNextDoc_Call { - _c.Call.Return(_a0, _a1, _a2, _a3) - return _c -} - -func (_c *Fetcher_FetchNextDoc_Call) RunAndReturn(run func(context.Context, *core.DocumentMapping) ([]byte, core.Doc, fetcher.ExecInfo, error)) *Fetcher_FetchNextDoc_Call { - _c.Call.Return(run) - return _c -} - // Init provides a mock function with given fields: ctx, txn, col, fields, filter, docmapper, reverse, showDeleted func (_m *Fetcher) Init(ctx context.Context, txn datastore.Txn, col *client.CollectionDescription, fields []client.FieldDescription, filter *mapper.Filter, docmapper *core.DocumentMapping, reverse bool, showDeleted bool) error { ret := _m.Called(ctx, txn, col, fields, filter, docmapper, reverse, showDeleted) diff --git a/db/txn_db.go b/db/txn_db.go index b307d96e35..03afaeb34a 100644 --- a/db/txn_db.go +++ b/db/txn_db.go @@ -280,6 +280,25 @@ func (db *explicitTxnDB) PatchSchema(ctx context.Context, patchString string) er return db.patchSchema(ctx, db.txn, patchString) } +func (db *implicitTxnDB) SetDefaultSchemaVersion(ctx context.Context, schemaVersionID string) error { + txn, err := db.NewTxn(ctx, false) + if err != nil { + return err + } + defer txn.Discard(ctx) + + err = db.setDefaultSchemaVersion(ctx, txn, schemaVersionID) + if err != nil { + return err + } + + return txn.Commit(ctx) +} + +func (db *explicitTxnDB) SetDefaultSchemaVersion(ctx context.Context, schemaVersionID string) error { + return db.setDefaultSchemaVersion(ctx, db.txn, schemaVersionID) +} + func (db *implicitTxnDB) SetMigration(ctx context.Context, cfg client.LensConfig) error { txn, err := db.NewTxn(ctx, false) if err != nil { diff --git a/http/client.go b/http/client.go index 16a8924a65..d670586320 100644 --- a/http/client.go +++ b/http/client.go @@ -223,6 +223,18 @@ func (c *Client) PatchSchema(ctx context.Context, patch string) error { return err } +func (c *Client) SetDefaultSchemaVersion(ctx context.Context, schemaVersionID string) error { + methodURL := c.http.baseURL.JoinPath("schema") + panic("todo") + + req, err := http.NewRequestWithContext(ctx, http.MethodPatch, methodURL.String(), strings.NewReader(schemaVersionID)) + if err != nil { + return err + } + _, err = c.http.request(req) + return err +} + func (c *Client) SetMigration(ctx context.Context, config client.LensConfig) error { return c.LensRegistry().SetMigration(ctx, config) } diff --git a/http/wrapper.go b/http/wrapper.go index 558dc79474..31dc37a74f 100644 --- a/http/wrapper.go +++ b/http/wrapper.go @@ -90,6 +90,10 @@ func (w *Wrapper) PatchSchema(ctx context.Context, patch string) error { return w.client.PatchSchema(ctx, patch) } +func (w *Wrapper) SetDefaultSchemaVersion(ctx context.Context, schemaVersionID string) error { + return w.client.SetDefaultSchemaVersion(ctx, schemaVersionID) +} + func (w *Wrapper) SetMigration(ctx context.Context, config client.LensConfig) error { return w.client.SetMigration(ctx, config) }